[英]c# xml serialization extra root node
I have a collection that I want to serialize to an xml document. 我有一个集合,我想序列化为xml文档。 The class is:
这堂课是:
public class Contacts{
public List<PendingContactDTO> contacts { get; set; }
}
My main problem is that now my xml looks 我的主要问题是现在我的xml看起来
<Contacts>
<contacts>
<..... all contacts>
</contacts>
</Contacts>
The thing is, I want to look it like this: 问题是,我想看起来像这样:
<contacts>
<..... all contacts>
</contacts>
Is there a way to this? 有办法吗?
[XmlRoot("contacts")]
public class Contacts{
[XmlElement("contact")]
public List<PendingContactDTO> contacts { get; set; }
}
should give you: 应该给你:
<contacts>
<contact...>...</contact>
...
<contact...>...</contact>
</contacts>
(the XmlRootAttribute
renames the Contacts
to contacts
; the XmlElementAttribute
tells it to remove the extra layer for the collection node, naming each contact
) (
XmlRootAttribute
将Contacts
重命名为contacts
; XmlElementAttribute
告诉它删除集合节点的额外层,命名每个contact
)
load your xml in to XmlDocument
将xml加载到
XmlDocument
xmlDoc.LoadXml(StrXML);
xmlDoc.SelectSingleNode("/Contacts/contacts")
I hope this will help you 我希望这能帮到您
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.