0

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?

1
  • I can do this by adding the namespace url to the constructor and then adding again when serializing, ommiting the prefix. Is this the best solution? Commented Jan 19, 2010 at 12:36

1 Answer 1

3

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; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this does add the namespace references but not in the way I need to support the XML format I am trying to duplicate.
Is there any solution to ignore the Namespace instead?
When MyVar is array this method is failed. We gets the first element without xmlns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.