我试图从google API位置读取一个XML文件并添加到一个结构中,但是我在使用C#时遇到了一些问题,因为我是新的... 我有一个像这样的XML文件:
<PlaceSearchResponse>
<status>OK</status>
<result>
<name>Williamsburg</name>
<type>locality</type>
<type>political</type>
<icon>http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png</icon>
<reference>CkRAAAAAUhZG...Yy0b4-sd1zCUu9P8</reference>
</result>
<result>
<name>Greenpoint</name>
<vicinity>New York</vicinity>
<type>neighborhood</type>
<type>political</type>
<icon>http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png</icon>
<reference>CkQ-AAAAHIDo...nYmSR8l52FmkMH6c</reference>
<name>Peter Luger Steakhouse</name>
<vicinity>Broadway, Brooklyn</vicinity>
<type>restaurant</type>
<type>food</type>
<type>establishment</type>
<icon>http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png</icon>
<reference>ClRBAAAATIpR...mHSxoyiRcr_FVuww</reference>
</result>
...additional results...
</PlaceSearchResponse>
我需要循环所有节点并添加到列表中。像这样:
while (nodetype == "type")
{
PlaceType t = new PlaceType();
t.name = x.Element("type").Value;
place.types.Add(t);
}
另外,我的班级地点:
public class Place
{
public string name { get; set; }
public List<PlaceType> types { get; set; }
public string vicinity { get; set; }
public string icon { get; set; }
public string reference { get; set; }
}
答案 0 :(得分:1)
以下内容将所有类型转换为字符串数组。
string[] valuesOfType = myXElement.Elements()
.Where(e => e.Name.LocalName == "type")
.Select(e => e.Value).ToArray();