Im trying to read a XML file from google API places and add to a structure, but im having some problems with C# because im new at it... I have a XML file like this:
<PlaceSearchResponse>
<status>OK</status>
<result>
<name>Williamsburg</name>
<type>locality</type>
<type>politica开发者_StackOverflow社区l</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>
And i need to loop all the nodes and add to a list. Something like this:
while (nodetype == "type")
{
PlaceType t = new PlaceType();
t.name = x.Element("type").Value;
place.types.Add(t);
}
Also, my class Place:
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; }
}
The following will pull out all types into a string array.
string[] valuesOfType = myXElement.Elements()
.Where(e => e.Name.LocalName == "type")
.Select(e => e.Value).ToArray();
精彩评论