NotABot
NotABot

Reputation: 526

How to find specific value of the node in xml file

I am making windows phone 8 app based the webservices. This is my xml code:

- <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. I tried same method for Imageurl, I don't know what's going wrong.

Upvotes: 0

Views: 339

Answers (1)

Chris
Chris

Reputation: 118

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.

Edited

Ok, it is probably because the <"url"> element is missing from one of the paths. 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",

Upvotes: 1

Related Questions