Mark Redman
Mark Redman

Reputation: 24515

C# XML Object Serialization: setting xmlns root attribute

I am serializing an object to xml and would like to set an xmlns attribute to the root node.

eg:

...
<root xmlns="[specified url]">
...
</root>

I cant seem to have an xmlns property/attribute on the member or seem to add the namespace when serializing without a prefix?

Any ideas?

Upvotes: 0

Views: 3647

Answers (1)

particle
particle

Reputation: 3350

This can do it as following. For top level use XmlRoot and for Properties use XmlElement

[System.Xml.Serialization.XmlRoot(Namespace="http://topLevelNS")]
class MyClass
{
    [System.Xml.Serialization.XmlElement(Namespace = "http://SomeOtherNS")]
    public int MyVar { get; set; }
}

Upvotes: 3

Related Questions