开发者

how to find out whats the nodeType?

开发者 https://www.devze.com 2023-04-10 17:39 出处:网络
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...

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消