在 Xml 序列化期间访问子节点 [英] Accessing Child nodes during Xml Serialization
本文介绍了在 Xml 序列化期间访问子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在序列化期间访问 Name 元素的子元素?
How can I access the children of the Name element during Serialization?
<Person>
<Name>
<First>John</First>
<Middle>Adam</Middle>
<Last>Smith</Last>
<Madian></Madian>
</Name>
<Gender>M</Gender>
</Person>
[XmlRootAttribute("Person", IsNullable= false)]
public class Person
{
[XmlElement(ElementName = "Name/First")]
public string firstName;
[XmlElement(ElementName = "Name/Middle", IsNullable = true)]
public string middleName;
[XmlElement(ElementName = "Name/Last")]
public string lastName;
[XmlElement(ElementName = "Name/Madian", IsNullable = true)]
public string madianName;
[XmlElement(ElementName = "Gender", DataType = "string")]
public string gender;
...
推荐答案
需要创建一个中介类:
public class Name
{
[XmlElement(ElementName = "First")]
public string firstName;
[XmlElement(ElementName = "Middle", IsNullable = true)]
public string middleName;
[XmlElement(ElementName = "Last")]
public string lastName;
[XmlElement(ElementName = "Madian", IsNullable = true)]
public string madianName;
}
然后在Person
中使用这个类:
[XmlRootAttribute("Person", IsNullable= false)]
public class Person
{
public Name Name;
[XmlElement(ElementName = "Gender", DataType = "string")]
public string gender;
...
}
这篇关于在 Xml 序列化期间访问子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文