0

I'm trying to get the attributes value of this yahoo weather XML element:

<yweather:wind chill="24" direction="340" speed="28.97" /> 

like this:

XDocument XResult = XDocument.Parse(e.Result);

XElement location = XResult.Elements(XName.Get("wind", "yweather")).FirstOrDefault();

XAttribute city = location.Attributes(XName.Get("chill")).FirstOrDefault();
XAttribute direction = location.Attributes(XName.Get("direction")).FirstOrDefault();
XAttribute speed = location.Attributes(XName.Get("speed")).FirstOrDefault();

but it tells me object not set to an instance. How can I fix this?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
v.g.
  • 1,076
  • 5
  • 19
  • 38

2 Answers2

0

You should use the namespace uri instead of namespace name, for example :

XElement location = XResult.Elements(XName.Get("wind", "http://xml.weather.yahoo.com/ns/rss/1.0"))
                           .FirstOrDefault();

if the element is direct child of root node, this simplified one should work as well :

XElement location = XResult.Element(XName.Get("wind", "http://xml.weather.yahoo.com/ns/rss/1.0"));

otherwise you need to use Descendants() instead of Elements() :

XElement location = XResult.Descendants(XName.Get("wind", "http://xml.weather.yahoo.com/ns/rss/1.0"))
                           .FirstOrDefault();
har07
  • 88,338
  • 12
  • 84
  • 137
0

Its like this

XDocument XResult = XDocument.Parse(e.Result);

XElement location = XResult.Descendants(XName.Get("wind", "http://xml.weather.yahoo.com/ns/rss/1.0")).FirstOrDefault();

XAttribute city = location.Attributes(XName.Get("chill")).FirstOrDefault();
XAttribute direction = location.Attributes(XName.Get("direction")).FirstOrDefault();
XAttribute speed = location.Attributes(XName.Get("speed")).FirstOrDefault();
v.g.
  • 1,076
  • 5
  • 19
  • 38