反序列化嵌套元素的序列

时间:2016-02-26 02:17:15

标签: .net xml-serialization

我对如何将以下XML反序列化为我已创建的实体感到困惑:

 <values totalcount="576">
      <version>3</version>
      <item>
         <datetime>2/22/2016 8:35:00 PM - 8:40:00 PM</datetime>
         <value channel="Outside" channelid="4">10.0000</value>
      </item>
      <item>
         <datetime>2/22/2016 8:40:00 PM - 8:45:00 PM</datetime>
         <value channel="Inside" channelid="2"/>
      </item>
   </values>

这些是我使用过的课程。当我反序列化时,ValueItems列表使用正确的项目数正确创建,我得到正确的TotalCount和Version值但是每个ValueItem都有其成员的默认值而不是预期的值:

public class Values
    {
        [XmlAttribute(AttributeName = "totalcount")]
        public int TotalCount { get; set; }

        [XmlElement(ElementName = "version")]
        public string Version { get; set; }

        [XmlElement(ElementName ="item")]
        public List<ValueItem> ValueItems { get; set; }
    }

  public class ValueItem
    {
        [XmlElement(ElementName = "datetime")]
        public string DateTime { get; set; }

        [XmlElement(ElementName="value")]
        public SensorValue Value { get; set; }
    }

public class SensorValue
    {
        [XmlAttribute(AttributeName = "channel")]
        public string Channel { get; set; }

        [XmlAttribute(AttributeName = "channelid")]
        public string ChannelId { get; set; }

        public string Value { get; set; }
    }

我尝试使用XmlArrayItem(&#34; item&#34;)来装饰ValueItems。我已经尝试过XmlArrayItem和XmlArray。我尝试用XmlType装饰ValueItem类(&#34; item&#34;)。

有什么想法吗?关于使用XmlAttributes的MSDN文档并不完全全面。

2 个答案:

答案 0 :(得分:0)

您可以将Visual Studio内置的XML用于类,以便从XML中获取此信息:

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class values {

    private byte versionField;

    private valuesItem[] itemField;

    private ushort totalcountField;

    /// <remarks/>
    public byte version {
        get {
            return this.versionField;
        }
        set {
            this.versionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("item")]
    public valuesItem[] item {
        get {
            return this.itemField;
        }
        set {
            this.itemField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public ushort totalcount {
        get {
            return this.totalcountField;
        }
        set {
            this.totalcountField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class valuesItem {

    private string datetimeField;

    private valuesItemValue valueField;

    /// <remarks/>
    public string datetime {
        get {
            return this.datetimeField;
        }
        set {
            this.datetimeField = value;
        }
    }

    /// <remarks/>
    public valuesItemValue value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class valuesItemValue {

    private string channelField;

    private byte channelidField;

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string channel {
        get {
            return this.channelField;
        }
        set {
            this.channelField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte channelid {
        get {
            return this.channelidField;
        }
        set {
            this.channelidField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

答案 1 :(得分:0)

您唯一的问题就像SensorValue calss中的Value属性一样,其余的工作正常。

要获取元素值,您需要使用 XmlText 属性。所以,如果你只是添加它应该可以正常工作。

[XmlText]
public string Value { get; set; }

下面的代码段显示了示例XML中的值:

string xml = File.ReadAllText("XMLFile1.xml");
using (StringReader reader = new StringReader(xml))
{
    var serializer = new XmlSerializer(typeof(Values), new XmlRootAttribute("values") );
    var result = serializer.Deserialize(reader) as Values;

    result.ValueItems.ForEach(v => Console.WriteLine(v.Value.Value));
}

输出:

10.0000

(第二个值为null,因为它没有在XML中提供)