简体   繁体   English

如何在xml文件中查找节点的特定值

[英]How to find specific value of the node in xml file

I am making windows phone 8 app based the webservices. 我正在基于Web服务制作Windows Phone 8应用程序。 This is my xml code: 这是我的xml代码:

- <response>
      <timestamp>2013-10-31T08:30:56Z</timestamp> 
      <resultsOffset>0</resultsOffset> 
      <status>success</status> 
      <resultsLimit>8</resultsLimit> 
      <resultsCount>38</resultsCount> 
    - <headlines>
    - <headlinesItem>
      <headline>City edge past Toon</headline> 
      <keywords /> 
      <lastModified>2013-10-30T23:45:22Z</lastModified> 
      <audio /> 
      <premium>false</premium> 
    + <links>
    - <api>
    - <news>
      <href>http://api.espn.com/v1/sports/news/1600444?region=GB</href> 
      </news>
      </api>
    - <web>
      <href>http://espnfc.com/uk/en/report/381799/city-edge-toon?ex_cid=espnapi_public</href> 
      </web>
    - <mobile>
      <href>http://m.espn.go.com/soccer/gamecast?gameId=381799&lang=EN&ex_cid=espnapi_public</href> 
      </mobile>
      </links>
      <type>snReport</type> 
      <related /> 
      <id>1600444</id> 
      <story>Alvardo Negredo and Edin Dzeko struck in extra-time to book Manchester City's place in the last eight of the Capital One Cup, while Costel Pantilimon kept a clean sheet in the 2-0 win to keep the pressure on Joe Hart. </story> 
      <linkText>Newcastle 0-2 Man City</linkText> 
    - <images>
    - <imagesItem>
      <height>360</height> 
      <alt>Man City celebrate after Edin Dzeko scored their second extra-time goal at Newcastle.</alt> 
      <width>640</width> 
      <name>Man City celeb Edin Dzeko goal v nufc 20131030 [640x360]</name> 
      <caption>Man City celebrate after Edin Dzeko scored their second extra-time goal at Newcastle.</caption> 
      <type>inline</type> 
      <url>http://espnfc.com/design05/images/2013/1030/mancitycelebedindzekogoalvnufc20131030_640x360.jpg</url> 
      </imagesItem>
      </images>

Code behind: 后面的代码:

 myData = XDocument.Parse(e.Result, LoadOptions.None);
 var data = myData.Descendants("headlines").FirstOrDefault();
 var data1 = from query in myData.Descendants("headlinesItem")
             select new UpdataNews
             {
                 News = (string)query.Element("headline").Value,
                 Desc = (string)query.Element("description"),
                 Newsurl = (string)query.Element("links").Element("mobile").Element("href"),
                 Imageurl = (string)query.Element("images").Element("imagesItem").Element("url").Value,
            };
lstShow.ItemsSource = data1;

I am trying to get value from xml tags and assign them to News,Desc, etc. Everything works fine except Imageurl, it shows NullException. 我试图从xml标记中获取价值,并将其分配给News,Desc等。除Imageurl之外,其他所有功能都正常,它显示了NullException。 I tried same method for Imageurl, I don't know what's going wrong. 我为Imageurl尝试了相同的方法,但不知道出了什么问题。

You arelooking for: 您正在寻找:

  Imageurl=(string)query.Element("images").Element("imagesItem").Element("url").Value

but "imagesItem" is not a child element to "images", it's a sibling. 但是“ imagesItem”不是“ images”的子元素,而是同级元素。

Edited 已编辑

Ok, it is probably because the <"url"> element is missing from one of the paths. 好的,可能是因为其中一个路径缺少<“ url”>元素。 So, take that into account. 因此,请考虑到这一点。

Imageurl=query.Element("images").Element("imagesItem").Element("url") != null ? Imageurl=(string)query.Element("images").Element("imagesItem").Element("url").Value : "no image",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM