0

Currently I am able to select attributes in an XML document because they are uniquely identifiable, like this:

XmlDocument weatherData = new XmlDocument();
weatherData.Load(query);

XmlNode channel = weatherData.SelectSingleNode("rss").SelectSingleNode("channel");
XmlNamespaceManager man = new XmlNamespaceManager(weatherData.NameTable);
man.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");

town            = channel.SelectSingleNode("yweather:location", man).Attributes["city"].Value;

But how do I select the "text" attribute from a node of the same name (yweather:forecast)?

<yweather:forecast day="Sat" text="Sunny" code="32"/>
<yweather:forecast day="Sun" text="Partly Cloudy" code="30"/>
<yweather:forecast day="Mon" text="AM Showers" code="39"/>
<yweather:forecast day="Tue" text="Cloudy" code="26"/>
<yweather:forecast day="Wed" text="Cloudy/Wind" code="24"/>

Is there a conditional statement I can use to only select the text attribute where the day attribute is equal to "Mon"?

1
  • "yweather:location[@day = 'mon']/@city" Commented Jan 24, 2015 at 12:18

1 Answer 1

1

Something like this will work:

string xml = "YourXml";
XElement doc = XElement.Parse(xml);

var Result = from a in doc.Descendants("yweather:forecast")
             where a.Attribute("day").Value == "Mon"
             select a.Attribute("text").Value;

or lambda syntax:

var Result = doc.Descendants("yweather:forecast")
                .Where(x=> x.Attribute("day").Value == "Mon")
                .Select(x=> x.Attribute("text").Value);

you can also refer this SO post

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.