Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6843079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:12:10+00:00 2026-05-27T00:12:10+00:00

This is the scenario: I have nested classes and need to serialize then in

  • 0

This is the scenario:

I have nested classes and need to serialize then in an xml document

[XmlRoot(Namespace="http://www.foo.bar/myschema")]
public class root 
{
    [XmlAttribute]
    public string version { get; set; }

    [XmlElement]
    public child child { get; set; }

    ...
}

[XmlRoot(Namespace="http://www.foo.bar/myschema")]
public class child
{
    [XmlElement]
    public int elemA { get; set; }

    [XmlElement]
    public string elemB { get; set; }

    ...
}

I have created an method based in another example to remove additional namespaces and set a custom one:

public static void Save<T>(this T type, string path)
{
    System.Xml.Serialization.XmlSerializer xs = 
        new System.Xml.Serialization.XmlSerializer(type.GetType());

    System.Xml.Serialization.XmlSerializerNamespaces ns = 
        new System.Xml.Serialization.XmlSerializerNamespaces();
    ns.Add("", "http://www.foo.bar/myschema");

    using(XmlWriter file = XmlWriter.Create(path))
    {
        xs.Serialize(file, type, ns);
    }
}

And I get this code as result:

<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://www.foo.bar/myschema" version="2.00">
    <child>
        <elemA>1</elemA>
        ...
    </child>
</root>

But expected this:

<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://www.foo.bar/myschema" version="2.00">
    <child xmlns="http://www.foo.bar/myschema">
        <elemA>1</elemA>
        ...
    </child>
</root>

We must set the custom namespace declaration in both tags. Is this possible?

Edit:

Here is a real-world example:

<?xml version="1.0" encoding="UTF-8"?>
<enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.01">
    <idLote>200602220000001</idLote>
    <NFe xmlns="http://www.portalfiscal.inf.br/nfe">
        <infNFe Id="NFe31060243816719000108550000000010001234567890" versao="1.01">
        ...
        <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
        ...
    </NFe>
    <NFe xmlns="http://www.portalfiscal.inf.br/nfe">
        <infNFe Id="NFe31060243816719000108550000000010011234567900" versao="1.01">
        ...
        <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
        ...
    </NFe>
    <NFe xmlns="http://www.portalfiscal.inf.br/nfe">
        <infNFe Id="NFe31060243816719000108550000000010021234567916" versao="1.01">
        ...
        <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
        ...
    </NFe>
</enviNFe>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:12:11+00:00Added an answer on May 27, 2026 at 12:12 am

    From an XML point of view your examples are identical, so the first one is perfectly fine. If you have to use the second one, there is a serious problem with our XML understanding or your processing pipeline.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this scenario where I need data integrity in the physical database. For
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
I have this massive nested loop scenario that is calling the DB and making
Imagine this scenario: You have a desktop and a laptop. The desktop has a
This is a question for a WSS/SharePoint guru. Consider this scenario: I have an
we have this scenario: A server which contains needed data and client component which
Consider this scenario. I have my own website, that I use as my identifier,
The architecture for this scenario is as follows: I have a table of items
I was wondering how other people implemented this scenario. I have an internal rails
Maybe it's not worth worrying about in this scenario, but lets say you have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.