【发布时间】:2016-08-09 19:12:06
【问题描述】:
当它被序列化时,我在结果列表中得到零个结果。我实际上是在尝试获取结果的几何形状,但由于某种原因,所有结果都没有显示,当我进行计数时,我得到 0。
这是一个xml的sn-p
<?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; }
}
【问题讨论】:
-
在原始 C# 代码中
if (webResponse.error == null)之前出现了什么?另外,你在序列化什么,你在哪里序列化它? -
将“PlacesApiQueryResponse”类重命名为“PlaceSearchResponse”或为该类添加 XMLElement 装饰。
-
@Telans 这不是问题。
-
Xml 序列化使驴子空白。请改用 NetDataContractSerializer 或 xaml 序列化。
-
@EdPlunkett 这是统一 www 进行 url 调用。我正在将 google places api 拉入类中使用。
标签: c# xml xmlserializer