Lets Talk
2   04/07/2012Looping through an XML document.


There are many ways and solutions to iterate/loop through an XML document, but for a simple loop I found the following extremely easy.

    First count how many sets of records are in the XML file.

Our XML is set like thus:

<?xml version="1.0" encoding="utf-8"?>
<blog xmlns:XML="http://www.w3.org/XML/1998/namespace" XML:space="preserve">
    <details>
        <id>1</id>
        <date>01/07/2012</date>
        <title>Welcome to ClickWorXs.</title>
        <blogText>Test</blogText>
        <writenBy>marK</writenBy>
    </details>
</blog>

So if we added another record it would look like thus:
<?xml version="1.0" encoding="utf-8"?>
<blog xmlns:XML="http://www.w3.org/XML/1998/namespace" XML:space="preserve">
    <details>
        <id>1</id>
        <date>01/07/2012</date>
        <title>Welcome to ClickWorXs.</title>
        <blogText>Test</blogText>
        <writenBy>marK</writenBy>
    </details>
    <details>
        <id>1</id>
        <date>01/07/2012</date>
        <title>Welcome to ClickWorXs.</title>
        <blogText>Test</blogText>
        <writenBy>marK</writenBy>
    </details>
</blog>

See how it only adds <details> and below in the nodes.
        So, we count how many records sets are in the file :
    XmlDocument readDoc = new XmlDocument();
    readDoc.Load(MapPath("~/Blog/blog.xml"));
    int count = readDoc.SelectNodes("blog/details/id").Count;

Line 1 sets a new instance so we can manipulate it.
Line 2 Load the document, the MapPath allows us to refer to the path as a http type path instead of C:/blah/blah
Line 3 counts how many <id> entries we have. So the above result would be 2.
Next we create a loop..
    n=1; n <count+1; n++
Now in our instance, because a record is appended to the XML file, if you want the newest entry first, just reverse the loop.
In our case, high was the count and low was the lowest entry we wanted on a page. In the above example, high would be 2, and low would be 1. When many more entries are added, lets say you had 30 records, and that you wanted to display 4 records, high would = 30 and low would = high - 4.

    for (int n = high; n > low-1; n = n - 1)

The next we use is simply the POSITION tag.
The first loop goes through, and it's on 2
            XmlDocument readDoc = new XmlDocument();

            readDoc.Load(MapPath("~/Blog/blog.xml"));

            XmlNode xId = readDoc.SelectSingleNode("blog/details[position() = " + n.ToString() + "]/id");
            XmlNode xDate = readDoc.SelectSingleNode("blog/details[position() = " + n.ToString() + "]/date");
            XmlNode xTitle = readDoc.SelectSingleNode("blog/details[position() = " + n.ToString() + "]/title");
            XmlNode xBlog = readDoc.SelectSingleNode("blog/details[position() = " + n.ToString() + "]/blogText");
            XmlNode xWrittenBy = readDoc.SelectSingleNode("blog/details[position() = " + n.ToString() + "]/writenBy");

Line 1 we start a new instance.
Line 2 we read the file as before.
From then on, we only ready the entry that the loop is on by using the POSITION tag..
    ("blog/details[position() = " + n.ToString() + "]/id");

Read the select node in this position [n] which on the first pass is 2.


Mark
1   01/07/2012Welcome to ClickWorXs.

Test

marK

Black Heath | Brierley Hill | Cradley Heath | Dudley | Kingswinford | Halseowen | Lye | Merry Hill | Oldbury | Stourbridge | West Bromwich | Wolverhampton