3

I'm having a problem when reading some XML into a Windows 8 store app.

I've got some XML with multiple parent and child nodes in which I need to only read one of the nodes and concatenate it all into a single string. I've tried many different ways to get it to loop through both parent nodes and extract the data from the single child node that the data needs to be read from. I'm using the XmlReader Class as that's the simplest way for me to be able to read it without too many problems.

This is the XML I'm trying to read:

<platforms>
<platform>
<api_detail_url>
<![CDATA[ http://www.giantbomb.com/api/platform/3045-20/ ]]>
</api_detail_url>
<id>20</id>
<name>
<![CDATA[ Xbox 360 ]]>
</name>
<site_detail_url>
<![CDATA[ http://www.giantbomb.com/xbox-360/3045-20/ ]]>
</site_detail_url>
<abbreviation>
<![CDATA[ X360 ]]>
</abbreviation>
</platform>
<platform>
<api_detail_url>
<![CDATA[ http://www.giantbomb.com/api/platform/3045-86/ ]]>
</api_detail_url>
<id>86</id>
<name>
<![CDATA[ Xbox Live Marketplace ]]>
</name>
<site_detail_url>
<![CDATA[
http://www.giantbomb.com/xbox-live-marketplace/3045-86/
]]>
</site_detail_url>
<abbreviation>
<![CDATA[ XBLM ]]>
</abbreviation>
</platform>
</platforms>

And this is my current attempt at reading it:

if(reader.ReadToDescendant("platform"))
{
    do
    {
        i++;
        reader.ReadToDescendant("name");
        reader.ReadStartElement("name");
        gPlat = gPlat + reader.Value + Environment.NewLine;
        reader.ReadToFollowing("platform");
    }
    while (reader.ReadToNextSibling("platform"));
}

I can't seem to get it too loop through both "platform" nodes if it has any kind of other reader functions/methods inside the do/while loop, if I leave it blank it loops fine without any trouble (the i variable was used to test this)

All I'm aiming for is a concatenated string with the two name child nodes as the value, but it only loops once through, skipping the second "name" node altogether. I am unable to change the XML as it's being returned by a web API.

Any help is much appreciated.

chue x
  • 18,573
  • 7
  • 56
  • 70
AlexEve
  • 77
  • 2
  • 10

2 Answers2

4

Why don't you use linq?

First Item:

XDocument doc = XDocument.Parse(xml);
var name = doc.Root
           .Elements("platform")
           .Select(x => x.Element("name").Value)
           .First();

All Items:

XDocument doc = XDocument.Parse(xml);
var platforms = doc.Root
           .Elements("platform")
           .Select(x => x.Element("name").Value)
           .ToList();
Davi Ruiz
  • 120
  • 3
1

You had an extra read that I commented out:

if (reader.ReadToDescendant("platform"))
{
    do
    {
        i++;
        reader.ReadToDescendant("name");
        reader.ReadStartElement("name");
        gPlat = gPlat + reader.Value + Environment.NewLine;
        //reader.ReadToFollowing("platform");
    }
    while (reader.ReadToFollowing("platform"));
}            

Results:

Xbox 360 
Xbox Live Marketplace 
chue x
  • 18,573
  • 7
  • 56
  • 70
  • 1
    That works, but it's reading everything with a name child node, there are other nodes with the same name child node containing different data, is there a way to stop it from reading past the end of the "platforms" node? – AlexEve May 16 '13 at 18:31
  • @AlexEve - I changed my answer to resolve **your** implementation. – chue x May 16 '13 at 19:21
  • @AlexEve - don't forget to accept an answer to close out the question. (you can only accept one answer). if you have enough reputation, you can also upvote any useful answers. – chue x May 16 '13 at 20:05