0

I am looking to loop thru an element based off one attribute, "sequence" , to retrieve another, "strokes".

My xml is as follows:

    <tournament>
  <leaderboard>
    <player first_name="Jimmy" last_name="Walker" country="UNITED STATES" id="2db60f6e-7b0a-4daf-97d9-01a057f44f1d" position="1" money="900000.0" points="500.0" score="-17" strokes="267">
      <rounds>
        <round score="-1" strokes="70" thru="18" eagles="0" birdies="5" pars="9" bogeys="4" double_bogeys="0" other_scores="0" sequence="1"/>
        <round score="-2" strokes="69" thru="18" eagles="0" birdies="3" pars="14" bogeys="1" double_bogeys="0" other_scores="0" sequence="2"/>
        <round score="-9" strokes="62" thru="18" eagles="0" birdies="10" pars="7" bogeys="1" double_bogeys="0" other_scores="0" sequence="3"/>
        <round score="-5" strokes="66" thru="18" eagles="0" birdies="6" pars="11" bogeys="1" double_bogeys="0" other_scores="0" sequence="4"/>
      </rounds>
    </player>
</leaderboard>
</tournament>

I am able to retrieve individual round elements based on the following code:

// EDITED TO REFLECT SOLUTION

foreach (XmlNode player in doc.GetElementsByTagName("player"))
            {
                string strokes;
                dtAttributeList.Rows.Add(
                    player.Attributes["last_name"].Value,
                    player.Attributes["first_name"].Value,
                    player.Attributes["position"].Value,
                    player.Attributes["score"].Value);
                if (player.HasChildNodes)
                {
                    foreach (XmlNode round in player.LastChild)
                    {
                        strokes = round.Attributes["strokes"].Value;
                        dtAttributeList.Rows.Add(strokes);
                    }
                }
            }

however in doing this I am only able to retrieve the first element and its values.

please help me find a solution to loop thru the "round" elements either via a filter on sequence or a loop of some sort.

1 Answer 1

1

It's much easier to use XPath for this than the approach you've tried above. You're also creating a huge amount of duplicate code by using a for loop instead of foreach:

foreach (XmlNode player in doc.GetElementsByTagName("player"))
{
    string strokes;
    dtAttributeList.Rows.Add(
        player.Attributes["last_name"].Value,
        player.Attributes["first_name"].Value,
        player.Attributes["position"].Value, 
        player.Attributes["score"].Value);

    foreach (XmlNode round in player.SelectNodes("rounds/round"))
    {
        strokes = round.Attributes["strokes"].Value;
        dtAttributeList.Rows.Add(strokes);
    }
}

If you need to iterate throug them based on the order of sequence (and they're not already in order), you can do this:

var rounds = player.SelectNodes("rounds/round")
                   .OfType<XmlNode>()
                   .OrderBy(n => int.Parse(n.Attributes["sequence"].Value));
foreach (XmlNode round in rounds)
{
    // ...
}
Sign up to request clarification or add additional context in comments.

7 Comments

I appologize. The code in question is also nested in another for loop causing more complexity. Please see the edited block. The sequence is in order however this may be a nice alternative if they are ever out of order. Based off of the first block that you sent, I am still only able to retrieve the first element, skipping the ladder 3.
This solution doesn't seem to be working. Am I able to use XPath if I am using XmlDocument instead of XDocument? As of now I have not gotten any results in the rounds variable.
Yes, you can use XPath with XmlDocument. Did you try my whole code sample above as-is, or did you modify it in some way (or use only part of it)? Please try the upper sample before attempting the lower one.
I tried the above code block. player.SelectNodes("rounds/round") yeilds no results at this point. doc.GetElementsByTagName("player") yeilds 132 results.
Do all the players in your XML document have rounds, or are there some without? In particular, does the first player (or the first few) have rounds? Is the XML snippet you provided an accurate representation of the XML format you're working with? Are there perhaps any namespace declarations that you have omitted?
|

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.