如何反序列化可以是单个或数组的元素?

时间:2017-09-02 12:06:33

标签: c# xml serialization

获取我想要反序列化为Object的XML。 如果我只反序列化非多值元素没有问题,但我不知道如何处理value元素中的值元素数组。 始终使用LocalName =#text,

的UnknownNode

要反序列化的XML

   <?xml version="1.0" encoding="UTF-8"?>
    <issue>
        <custom_fields type="array">
            <custom_field id="11" name="Version">
                <value>7.9.18.31</value>
            </custom_field>
            <custom_field id="89" name="Tags" multiple="true">
                <value type="array">
                    <value>Tag1</value>
                    <value>Tag3</value>
                    <value>Tag6</value>
                </value>
            </custom_field>
            <custom_field id="90" name="started_on">
                <value>2017-08-25</value>
            </custom_field>
        </custom_fields>
    </issue>

我得到的是xsd.exe

using System.Xml.Serialization;

[XmlRoot(IsNullable=false)]
public partial class issue
{  
    private object[] itemsField;

    [XmlElement("custom_fields", typeof(issueCustom_fields))]
    [XmlElement("value", typeof(Value))]
    public object[] Items
    {
        get { return itemsField; }
        set { itemsField = value; }
    }
}

public class Value
{
    private Value[] valueField;
    private string typeField;

    [XmlElement("value")]
    public Value[] value
    {
        get { return valueField; }
        set { valueField = value; }
    }

    [XmlAttribute]
    public string type
    {
        get { return typeField; }
        set { typeField = value; }
    }
}

public class issueCustom_fields
{
    private issueCustom_fieldsCustom_field[] custom_fieldField;    
    private string typeField;

    [XmlElement("custom_field")]
    public issueCustom_fieldsCustom_field[] custom_field
    {
        get { return custom_fieldField; }
        set { custom_fieldField = value; }
    }

    [XmlAttribute]
    public string type
    {
        get { return typeField; }
        set { typeField = value; }
    }
}

public class issueCustom_fieldsCustom_field
{   
    private Value[] valueField;    
    private string idField;   
    private string nameField;    
    private string multipleField;

    [XmlElement("value")]
    public Value[] value
    {
        get { return valueField; }
        set { valueField = value; }
    }

    [XmlAttribute]
    public string id
    {
        get { return idField; }
        set { idField = value; }
    }

    [XmlAttribute]
    public string name
    {
        get { return nameField; }
        set { nameField = value; }
    }

    [XmlAttribute]
    public string multiple
    {
        get { return multipleField; }
        set { multipleField = value; }
    }
}

反序列化代码

    var ser = new XmlSerializer(typeof(issue));
    var issue = new issue();

    using (var sr = new StreamReader("testIssues.xml"))
        issue = (issue)ser.Deserialize(sr);

我可以使用以下方式获取数据:

ser.UnknownNode += (s, ea) =>
        {
            if (ea.LocalName == "#text" && ea.ObjectBeingDeserialized is Value)
            {
                Value val = (Value)ea.ObjectBeingDeserialized;

                if (val.value == null)
                {
                    val.Val = ea.Text;
                }
            }
        };

但有没有办法让它更顺畅地使用反序列化?

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用XmlArrayAttribute?

 [XmlArray("value")]
 public Value[] Items
 {
     get { return itemsField; }
     set { itemsField = value; }
 }

 [XmlType("Value")]
 public class Value
 {
    ....
 }

查看此question