4

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>
Regis Silva
  • 59
  • 2
  • 7

1 Answers1

1

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.

Achim
  • 15,415
  • 15
  • 80
  • 144
  • I have added another example, is anithing wrong with this definition? – Regis Silva Nov 22 '11 at 19:27
  • The xmlns attribute of the NFe tag is redundant. The namespace for the Signature tag is required. So I'm quite sure the XmlSerializer will not output the xmlns attribute for NFe, but for Signature. Did you gave it a try? – Achim Nov 22 '11 at 19:42
  • Problem solved! The Webservice were rejeitig the document because of tag indentation characters. Thanks! – Regis Silva Nov 22 '11 at 20:04