如何找出nodeType是什么?

发布于 2024-12-08 11:56:31 字数 1752 浏览 0 评论 0原文

我正在尝试从 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; }
}

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>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>  

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; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-12-15 11:56:31

下面将把所有类型提取到一个字符串数组中。

string[] valuesOfType = myXElement.Elements()
   .Where(e => e.Name.LocalName == "type")
   .Select(e => e.Value).ToArray();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文