2016-02-26 202 views
1

我難倒如何反序列化下面的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和版本值每個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(「item」)裝飾ValueItems。我已經嘗試XmlArrayItem與XmlArray。我嘗試用XmlType(「item」)裝飾ValueItem類。

任何想法?有關使用XmlAttributes的MSDN文檔不完全全面。

回答

0

你可以使用內置的XML Visual Studio的上課從您的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; 
     } 
    } 
} 
+0

反序列化時拋出異常:當我添加XmlTy時不能包含匿名類型'Test.Values' pe屬性與Anonymous = true。 – Emmanuel

+0

@Canoehead你如何反序列化它?它爲我工作。 – Tyress

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

(因爲它沒有在XML提供第二值是空)