JDIBO
JDIBO

Reputation: 67

XML can't write xmlns to child nodes

Hi I have 3 classes like that:

public abstract class XmlNs
{
    public const string XmlnsAttribute = "urn:ebay:apis:eBLBaseComponents";
}


[Serializable]
public class BulkDataExchangeRequests : XmlNs
{
    [XmlAttribute("xmlns")]
    public string XmlNs = XmlnsAttribute;
    [XmlElement("Header")]
    public Header Header { get; set; }
    [XmlElement("AddFixedPriceItemRequest")]
    public List<AddFixedPriceItemRequest> ListAddFixedPriceItemRequest { get; set; }
}

[Serializable]
public class AddFixedPriceItemRequest : XmlNs
{
    [XmlElement("ErrorLanguage")]
    public string ErrorLanguage { get; set; }
    [XmlElement("WarningLevel")]
    public string WarningLevel { get; set; }
    [XmlElement("Version")]
    public string Version { get; set; }
    [XmlElement("Item")]
    public ItemType Item { get; set; }
    [XmlAttribute("xmlns")]
    public string XmlNs = XmlnsAttribute;
}

The problem is that when I serialize the object I get a correct xml but with no xmlns attribute in the AddFixedPriceItemRequest item, while in the BulkDataExchangeRequests the xmlns is correctly written....

Any help will be very appreciated...

Upvotes: 3

Views: 1982

Answers (1)

thriqon
thriqon

Reputation: 2488

You are nesting elements, and nested elements are in the same namespace as their parent elements, if you don't specify anything else. So in fact your serializer is correct to not output the xmlns attribute again, as it is not needed.

See:

<root xmlns="my-namespace">
    <element>this is also in the namespace "my-namespace" without further declaration</element>
    <so><are><child><elements></elements></child></are></so>
</root>


EDIT: Even though eBay is obviously not conforming to standards here, there is a solution! You can declare namespaces for the .NET xml serializer in a very convenient manner, and these declarations are kept, even if they are repeated:

[Serializable]
public class BulkDataExchangeRequests : XmlNs
{
    [XmlElement("Header")]
    public Header Header { get; set; }
    [XmlElement("AddFixedPriceItemRequest")]
    public List<AddFixedPriceItemRequest> ListAddFixedPriceItemRequest { get; set; }

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(new System.Xml.XmlQualifiedName[] { new System.Xml.XmlQualifiedName("", XmlnsAttribute) });
}

[Serializable]
public class AddFixedPriceItemRequest : XmlNs
{
    [XmlElement("ErrorLanguage")]
    public string ErrorLanguage { get; set; }
    [XmlElement("WarningLevel")]
    public string WarningLevel { get; set; }
    [XmlElement("Version")]
    public string Version { get; set; }
    [XmlElement("Item")]
    public ItemType Item { get; set; }

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(new System.Xml.XmlQualifiedName[] { new System.Xml.XmlQualifiedName("", XmlnsAttribute) });
}

The output is as expected:

<?xml version="1.0" encoding="utf-16"?>
<BulkDataExchangeRequests xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:ebay:apis:eBLBaseComponents">
  <AddFixedPriceItemRequest xmlns="urn:ebay:apis:eBLBaseComponents" />
  <AddFixedPriceItemRequest xmlns="urn:ebay:apis:eBLBaseComponents" />
</BulkDataExchangeRequests>

Relevant documentation:

System.Xml.Serialization.XmlNamespaceDeclarations System.Xml.Serialization.XmlSerializerNamespaces

Upvotes: 3

Related Questions