2016-06-09 94 views
0

我試圖反序列化API響應中返回的一些XML,但反序列化後,對象中的所有值都是NULL。C#:反序列化XML後的對象值爲空

下面是我正在嘗試反序列化的響應XML。

<?xml version="1.0" encoding="utf-8"?> 
<ctatt> 
    <tmst>20160609 11:50:03</tmst> 
    <errCd>0</errCd> 
    <errNm /> 
    <eta> 
     <staId>41300</staId> 
     <stpId>30252</stpId> 
     <staNm>Loyola</staNm> 
     <stpDe>Service toward 95th/Dan Ryan</stpDe> 
     <rn>803</rn> 
     <rt>Red</rt> 
     <destSt>30089</destSt> 
     <destNm>95th/Dan Ryan</destNm> 
     <trDr>5</trDr> 
     <prdt>20160609 11:48:45</prdt> 
     <arrT>20160609 11:51:45</arrT> 
     <isApp>0</isApp> 
     <isSch>0</isSch> 
     <isDly>0</isDly> 
     <isFlt>0</isFlt> 
     <flags /> 
     <lat>42.01906</lat> 
     <lon>-87.67289</lon> 
     <heading>130</heading> 
    </eta> 
</ctatt> 

這裏是我的類:

[Serializable, XmlRoot("ctatt")] 
     public class trainData 
     { 
      [XmlElement(ElementName ="tmst")] 
      public string timeStamp { get; set; } 

      [XmlElement(ElementName = "errCd")] 
      public byte errorCode { get; set; } 

      [XmlElement(ElementName = "staId")] 
      public ushort stationId { get; set; } 

      [XmlElement(ElementName = "stpId")] 
      public ushort stopId { get; set; } 

      [XmlElement(ElementName = "staNm")] 
      public string stationName { get; set; } 

      [XmlElement(ElementName = "stpDe")] 
      public string stopDesc { get; set; } 

      [XmlElement(ElementName = "rn")] 
      public ushort runNum { get; set; } 

      [XmlElement(ElementName = "rt")] 
      public string routeName { get; set; } 

      [XmlElement(ElementName = "destSt")] 
      public ushort destStation { get; set; } 

      [XmlElement(ElementName = "destNm")] 
      public string destName { get; set; } 

      [XmlElement(ElementName = "trDr")] 
      public byte trainDir { get; set; } 

      [XmlElement(ElementName = "prdt")] 
      public string prdTime {get; set;} 

      [XmlElement(ElementName = "arrT")] 
      public string arrTime { get; set; } 

      [XmlElement(ElementName = "isApp")] 
      public ushort isApp { get; set; } 

      [XmlElement(ElementName = "isSch")] 
      public ushort isSch { get; set; } 

      [XmlElement(ElementName = "isDly")] 
      public ushort isDly { get; set; } 

      [XmlElement(ElementName = "isFlt")] 
      public ushort isFlt { get; set; } 

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

      [XmlElement(ElementName = "lat")] 
      public double lat { get; set; } 

      [XmlElement(ElementName = "lon")] 
      public double lon { get; set; } 

      [XmlElement(ElementName = "heading")] 
      public ushort heading { get; set; } 
     } 

這裏是我使用的反序列化的代碼:

var response = await client.GetAsync(urlParameters); 
       if (response.IsSuccessStatusCode) 
       { 
        var xml = await response.Content.ReadAsStringAsync(); 
        XmlSerializer deserializer = new XmlSerializer(typeof(trainData)); 
        using (StringReader reader = new StringReader(xml)) 
        { 
         using (XmlReader xr = XmlReader.Create(reader)) 
         { 
          trainData result = (trainData)deserializer.Deserialize(xr); 
          Console.WriteLine("Station Name: {0}\nRoute Name: {1}\nArrival Time: {2}", result.stationName, result.routeName, result.arrTime); 
         } 
        } 
       } 
       else 
       { 
        Console.WriteLine("There was an error!"); 
       } 

任何建議,將不勝感激...

+0

你還應該包括你正在獲取的堆棧跟蹤。 –

+1

'trainData'需要一個'eta'類型作爲屬性。這些嵌套在'eta'中的元素不能在'trainData'上反序列化。 – Crowcoder

回答

2

您提供的xml有2層ctatteta。然而你的模型只有一個層次。當然這不會起作用。

你需要改變你的模型保持佈局相同與XML:

[Serializable, XmlRoot("ctatt")] 
public class trainDataResult 
{ 
    [XmlElement(ElementName ="tmst")] 
    public string timeStamp { get; set; } 

    [XmlElement(ElementName = "errCd")] 
    public byte errorCode { get; set; } 

    // uncomment next lines to include `errNm` 
    //[XmlElement(ElementName = "errNm")] 
    //public string errorName { get; set; } 

    [XmlElement(ElementName = "eta")] 
    public TrainData eta { get; set; } 
} 

public class TrainData 
{ 
    [XmlElement(ElementName = "staId")] 
    public ushort stationId { get; set; } 

    [XmlElement(ElementName = "stpId")] 
    public ushort stopId { get; set; } 

    [XmlElement(ElementName = "staNm")] 
    public string stationName { get; set; } 

    [XmlElement(ElementName = "stpDe")] 
    public string stopDesc { get; set; } 

    [XmlElement(ElementName = "rn")] 
    public ushort runNum { get; set; } 

    [XmlElement(ElementName = "rt")] 
    public string routeName { get; set; } 

    [XmlElement(ElementName = "destSt")] 
    public ushort destStation { get; set; } 

    [XmlElement(ElementName = "destNm")] 
    public string destName { get; set; } 

    [XmlElement(ElementName = "trDr")] 
    public byte trainDir { get; set; } 

    [XmlElement(ElementName = "prdt")] 
    public string prdTime {get; set;} 

    [XmlElement(ElementName = "arrT")] 
    public string arrTime { get; set; } 

    [XmlElement(ElementName = "isApp")] 
    public ushort isApp { get; set; } 

    [XmlElement(ElementName = "isSch")] 
    public ushort isSch { get; set; } 

    [XmlElement(ElementName = "isDly")] 
    public ushort isDly { get; set; } 

    [XmlElement(ElementName = "isFlt")] 
    public ushort isFlt { get; set; } 

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

    [XmlElement(ElementName = "lat")] 
    public double lat { get; set; } 

    [XmlElement(ElementName = "lon")] 
    public double lon { get; set; } 

    [XmlElement(ElementName = "heading")] 
    public ushort heading { get; set; } 
} 
+0

您在元素的trainingDataResult中缺少一個屬性。否則,這個答案似乎是正確的。 –

+0

@RalphAllanRice確實如此,但在OP的來源中也沒有。 – Xiaoy312

+0

感謝所有的回覆。當我按照上面的方法更新模型時,我現在收到一個錯誤消息,說「不是我期望的。」我早些時候遇到了這個問題,並通過添加XmlRoot屬性來解決它。 – orajestad9

0

我只知道如何反序列化從一個文件中的元素列表,但也許它可以幫助...

List<trainData> result = new List<trainData>; 
    using (FileStream stream = new FileStream(Filepath,Filemode.Open) 
{ 
XmlSerializer Serializer =new XmlSerializer(typeof(List<trainData>)); 
result = Serializer.Deserialize(stream); 
} 
+0

提供的xml甚至不包含任何類似數組的東西...... – Xiaoy312

0

如果是complexType,我認爲它需要作爲類來表示整個元素。創建第二個類(例如「ETAData」)以包含該元素下的元素。您還需要在trainData類中具有該類型的屬性,並且還使用[XmlElementAttribute]標記該屬性。

編輯:@ Xiaoy312答案更簡潔,基本上說明了我所說的。