简体   繁体   English

C#XML对象序列化:设置xmlns根属性

[英]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. 我正在将一个对象序列化为xml,并想将xmlns属性设置为根节点。

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? 我似乎无法在成员上具有xmlns属性/属性,或者在无前缀序列化时似乎没有添加名称空间?

Any ideas? 有任何想法吗?

This can do it as following. 这可以做到如下。 For top level use XmlRoot and for Properties use XmlElement 对于顶级使用XmlRoot,对于属性使用XmlElement

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM