序列化后,我在结果列表中得到的结果为零。我实际上是想获得结果的几何结构,但由于某种原因,所有结果都没有显示,当我做一个计数时,我得到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; }
}
答案 0 :(得分:1)
您的课程与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; }
}