开发者

Accessing Child nodes during Xml Serialization

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&g开发者_如何学Ct;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;

    ...


    [XmlArray("Person")]
    [XmlArrayItem("Name", typeof(Name))]
    public List<Name> Name{ get; set; }


You need to create an intermediary class:

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

and then use this class inside Person:

[XmlRootAttribute("Person", IsNullable= false)]
public class Person
{
    public Name Name;

    [XmlElement(ElementName = "Gender", DataType = "string")]
    public string gender;

    ...
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜