tags:

views:

437

answers:

3

I can read one record from Xml just fine with some code I have, but how do you read multiple in an xml file such as so:

<?xml version="1.0" encoding="UTF-8"?>

<XXX_XXX_response xmlns="http://XXX.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://XXX.com/1.0/ http://XXX.com/1.0/XXX.xsd" list="true">

  <album>

    <id>7031990242326938677</id>

    <pid>7031990241327042549</pid>

    <owner>1642262814</owner>

    <location>CA</location>

  </album>

  <album>

    <id>7031994332086936240</id>

    <pid>703199322241087005994</pid>

    <owner>1633262814</owner>

    <location/>

  </album>

  <album>

    <id>7031990241086937544</id>

    <pid>7031990241087026027</pid>

    <location/>

  </album>

  <album>

    <id>70319902431086936188</id>

    <pid>7033490241087005114</pid>

    <owner>1633262814</owner>

    <location/>

  </album>


</XXX_XXX_response>

How do you iterate through each Album and what's a good container for holding that data (what kind of list object, etc.) afterwards? And no, I'm not talking DataSet (puke)

I'm not looking for the exact code necessarily (sure if you have it thanks) but an article that relates or shows an example of reading multiple records in .NET.

I'm trying to think of the most ideal lightweight object to store each record in so that I can iterate through it later.

A: 

XPathDocument (CreateNavigator) & XPathNavigator are the classes you can use.

Also, XPathNavigator has Select method which takes XPath expression (e.g. //album)
Above XPath expression means - get me all the nodes named album anywhere in the document.

Hope this gives you pointers on where to look for

shahkalpesh
+1  A: 

You can read the xml like this and store objects of class Album in a generic list.

List<Album> albums = new List<Album>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("MyXml.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/album"); 
foreach (XmlNode node in nodes)
{   
    Album album = new Album();
    album.id = node["id"].InnerText;
    albums.Add(album);
}

Here's how to search the list search-for-object-in-generic-list

Here's how to sort the list How to sort a generic List

Phaedrus
A: 

When i need to do things fast, i always use XmlTextReader, which is fastest (forward only) method to parse the XML document.