Hi, I'm trying to parse an XML feed, I can't figure out how to read an attribute of a node instead of the value of a node below it. Any help would be greatly appreciated!
@{ //Fetch RSS XML XmlTextReader udBrudRSS =newXmlTextReader(path to feed");
//Create new XML document XmlDocument doc =newXmlDocument();
//Load in our remote XML into our XML document doc.Load(udBrudRSS);
//Select our nodes we want with some xPath XmlNodeList rssItems = doc.SelectNodes("//item");
} <ul class="rss-feed"> @{ //For each item node we can then ouput what we want var i =0; foreach(XmlNode node in rssItems) { <li> <a href="@node["link"].InnerText">@node["title"].InnerText</a> <div class="date">@Html.Raw(@node["description"].InnerText)</div>
Any idea how i could also get the attribute of a node below? Here's an example of the XML I'm trying to read, I'd like to also get the score of the round, Id etc.
Hmm, SelectSingleNode works as expected, I get the following for "SelectNodes"
'System.Xml.XmlNode' does not contain a definition for 'SelectNode' and no extension method 'SelectNode' accepting a first argument of type 'System.Xml.XmlNode' could be found (are you missing a using directive or an assembly reference?)
Sorry for answering that late. You may have mistyped the method because the error says the method "SelectNode" does not exist, but it must be "SelectNodes".
And for sure Chriztians answer works as well. But for multiple values you have to go the other way.
So, I'm still struggling with this, when I use "@node.SelectNodes("Round")", I'm getting the following returned but I'm not sure how to actually retrieve any value from it.
<ul> @{ //For each item node we can then ouput what we want var i = 0; foreach (XmlNode node in feedItems) { <li> @node.Attributes["ID"].Value.ToString(), @node.Attributes["Country"].Value.ToString()
Read attribute of xml node
Hi, I'm trying to parse an XML feed, I can't figure out how to read an attribute of a node instead of the value of a node below it. Any help would be greatly appreciated!
Hi Amir
You can simply call
This will return you the value of the "link" attribute.
Yep! That worked!
Any idea how i could also get the attribute of a node below? Here's an example of the XML I'm trying to read, I'd like to also get the score of the round, Id etc.
Well you can do the following:
This will return the ID of the first "Round" child node. But will also throw an error if no child exists. You may add null checks for this purpose.
Getting all child nodes is simply
You may better put that to a variable and the iterate the found rounds.
Hmm, SelectSingleNode works as expected, I get the following for "SelectNodes"
'System.Xml.XmlNode' does not contain a definition for 'SelectNode' and no extension method 'SelectNode' accepting a first argument of type 'System.Xml.XmlNode' could be found (are you missing a using directive or an assembly reference?)
Hi all,
For the record, as long as you're already using XPath, you *should* be able to just do this:
- to select the ID attribute of the first Round child. Assuming Razor hasn't fiddled with these methods, of course :-)
/Chriztian
Also, I apologize, I think I accidentally marked myself as the correct answer!
Chriztian, that definitely works also. How would I get the following round?
Sorry for answering that late. You may have mistyped the method because the error says the method "SelectNode" does not exist, but it must be "SelectNodes".
And for sure Chriztians answer works as well. But for multiple values you have to go the other way.
So, I'm still struggling with this, when I use "@node.SelectNodes("Round")", I'm getting the following returned but I'm not sure how to actually retrieve any value from it.
System.Xml.XPathNodeList
Alright, so this is working, but seems messy.
is working on a reply...