开发者

C# XML Object Serialization: setting xmlns root attribute

开发者 https://www.devze.com 2022-12-17 08:11 出处:网络
I am serializing an object to xml and would like to set an xmlns attribute to the root node. eg: ... <root xmlns=\"[specified url]\">

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 m开发者_如何学Goember or seem to add the namespace when serializing without a prefix?

Any ideas?


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; }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消