Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Amir Khan 1287 posts 2744 karma points
    Jun 25, 2013 @ 23:04
    Amir Khan
    0

    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!

    @using System.Xml.XPath;
    @using System.Xml;
    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
       
    //Fetch RSS XML
       
    XmlTextReader udBrudRSS = new XmlTextReader(path to feed");
       
       
    //Create new XML document
       
    XmlDocument doc = new XmlDocument();
       
       
    //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>
                   
               
    </li>
                            i++;
                            if (i == 5){ break; }
                            }
                   
        }
    </
    ul>
  • Andreas I. 12 posts 34 karma points
    Jun 25, 2013 @ 23:09
    Andreas I.
    1

    Hi Amir

    You can simply call

    @node.Attributes["link"]

    This will return you the value of the "link" attribute.

  • Amir Khan 1287 posts 2744 karma points
    Jun 25, 2013 @ 23:14
    Amir Khan
    100

    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.

    <Player ID="82866" WwgrID="975" FirstName="Inbee" LastName="Park" Country="KOR" IsAmateur="False" Rank="1" RankTied="False" StartRank="5" StartRankTied="True" Thru="18" StartTee="1" StartTime="01:26 PM" RoundToPar="-4" CumulativeTotal="201" CumulativeToPar="-12" Money="300000" TournamentStatus="WNR" RoundStatus="FIN">
    <Round ID="1" Score="69"/>
    <Round ID="2" Score="65"/>
    <Round ID="3" Score="67"/>
    </Player>

     

  • Andreas I. 12 posts 34 karma points
    Jun 25, 2013 @ 23:18
    Andreas I.
    1

    Well you can do the following:

    @node.SelectSingleNode("Round").Attributes["ID"]

    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

    @node.SelectNodes("Round")

    You may better put that to a variable and the iterate the found rounds.

  • Amir Khan 1287 posts 2744 karma points
    Jun 25, 2013 @ 23:25
    Amir Khan
    0

    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?)

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jun 25, 2013 @ 23:25
    Chriztian Steinmeier
    1

    Hi all,

    For the record, as long as you're already using XPath, you *should* be able to just do this:

    @node.SelectSingleNode("Round/@ID")

    - to select the ID attribute of the first Round child. Assuming Razor hasn't fiddled with these methods, of course :-)

    /Chriztian

  • Amir Khan 1287 posts 2744 karma points
    Jun 25, 2013 @ 23:26
    Amir Khan
    0

    Also, I apologize, I think I accidentally marked myself as the correct answer!

  • Amir Khan 1287 posts 2744 karma points
    Jun 25, 2013 @ 23:32
    Amir Khan
    0

    Chriztian, that definitely works also. How would I get the following round?

  • Andreas Iseli 150 posts 427 karma points
    Jun 27, 2013 @ 10:02
    Andreas Iseli
    0

    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.

  • Amir Khan 1287 posts 2744 karma points
    Jul 01, 2013 @ 21:17
    Amir Khan
    0

    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
  • Amir Khan 1287 posts 2744 karma points
    Jul 01, 2013 @ 21:36
    Amir Khan
    0

    Alright, so this is working, but seems messy.

    <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()



    @{var roundNode = @node.SelectNodes("Round");}

    @foreach (XmlNode roundInfo in roundNode)
    {

    <p>@roundInfo.Attributes["ID"].Value.ToString()</p>
    <p>@roundInfo.Attributes["Score"].Value.ToString()</p>

    }
    </li>
    i++;
    if (i == 5){ break; }
    }

    }
    </ul>
Please Sign in or register to post replies

Write your reply to:

Draft