簡體   English   中英

xml到C#類無法正確序列化

[英]xml to c# classes not serializing correctly

序列化后,結果列表中的結果為零。 我實際上正在嘗試獲取結果的幾何形狀,但由於某種原因,所有結果均未顯示,當我進行計數時,我得到0。

這是xml的代碼段

<?xml version="1.0" encoding="UTF-8"?>
<PlaceSearchResponse>
 <status>OK</status>
 <result>
  <name>Premier Inn Manchester Deansgate Locks</name>
  <vicinity>Medlock Street, Manchester</vicinity>
  <type>lodging</type>
  <type>restaurant</type>
  <type>food</type>
  <type>point_of_interest</type>
  <type>establishment</type>
  <geometry>
   <location>
    <lat>53.4713048</lat>
    <lng>-2.2474693</lng>
   </location>
   <viewport>
    <southwest>
     <lat>53.4711143</lat>
     <lng>-2.2475661</lng>
    </southwest>
    <northeast>
     <lat>53.4718764</lat>
     <lng>-2.2473777</lng>
    </northeast>
   </viewport>
  </geometry>

C#

    if (webResponse.error == null)
    {
        print(webResponse.text);
        PlacesApiQueryResponse placesObject = LoadFromText(webResponse.text);
        print(placesObject.results.Count);

        foreach(var entity in placesObject.results)
        {
            print(entity.geometry.location.lat + " | " + entity.geometry.location.lng);
        }

    }
    else
    {
        print(webResponse.error);
    }
}

public static PlacesApiQueryResponse LoadFromText(string text)
{
    var serializer = new XmlSerializer(typeof(PlacesApiQueryResponse), new XmlRootAttribute("PlaceSearchResponse"));
    return serializer.Deserialize(new StringReader(text)) as PlacesApiQueryResponse;
}
}

public class Location
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Geometry
{
    public Location location { get; set; }
}

public class OpeningHours
{
    public bool open_now { get; set; }
}

public class Photo
{
    public int height { get; set; }
    public List<object> html_attributions { get; set; }
    public string photo_reference { get; set; }
    public int width { get; set; }
}

public class AltId
{
    public string place_id { get; set; }
    public string scope { get; set; }
}

public class Result
{
    public Geometry geometry { get; set; }
    public string icon { get; set; }
    public string id { get; set; }
    public string name { get; set; }
    public OpeningHours opening_hours { get; set; }
    public List<Photo> photos { get; set; }
    public string place_id { get; set; }
    public string scope { get; set; }
    public List<AltId> alt_ids { get; set; }
    public string reference { get; set; }
    public List<string> types { get; set; }
    public string vicinity { get; set; }
    public string rating { get; set; }
}

public class PlacesApiQueryResponse
{
    public List<object> html_attributions { get; set; }
    public List<Result> results { get; set; }
    public string status { get; set; }
}

XmlSerializer工作方式而言,您的類僅與輸入XML不對應。 問題與List<T>序列化有關,其中需要兩級XML,如下所示:

<list-element>
   <item-element>
      … content goes here …
   </item-element>
   …
</list-element>

您必須同時更改XML和相應的類,例如:

<results> ← new element groupping all <result> elements
   <result>…</result>
</result>

和班級:

public class PlacesApiQueryResponse
{
    public List<object> html_attributions { get; set; }

    // attribute to tell XmlSerializer how are the item-elements named
    [XmlArrayItem("result")]
    public List<Result> results { get; set; }
    public string status { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM