0

I have an application that is on .net 2.0 and I am having some difficult with it as I am more use to linq.

The xml file look like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<updates>
    <files>
        <file url="files/filename.ext" checksum="06B9EEA618EEFF53D0E9B97C33C4D3DE3492E086" folder="bin" system="0" size="40448" />
        <file url="files/filename.ext" checksum="CA8078D1FDCBD589D3769D293014154B8854D6A9" folder="" system="0" size="216" />
        <file url="files/filename.ext" checksum="CA8078D1FDCBD589D3769D293014154B8854D6A9" folder="" system="0" size="216" />
    </files>
</updates>

The file is downloaded and readed on the fly:

XmlDocument readXML = new XmlDocument();
readXML.LoadXml(xmlData);

Initially i was thinking it would go with something like this:

XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//files");

foreach (XmlNode node in nodes)
{
 ... im reading it ...
}

But before reading them I need to know how many they are to use on my progress bar and I am also clueless on how to grab the attribute of the file element in this case.

  • How could I count how many "file" ELEMENTS I have (count them before entering the foreach ofc) and read their attributes ?

I need the count because it will be used to update the progress bar.

Overall it is not reading my xml very well.

Prix
  • 19,417
  • 15
  • 73
  • 132

3 Answers3

2

before reading them I need to know how many they are to use on my progress bar

Use the XmlNodeList.Count property. Code example below.

Overall it is not reading my xml very well

Here's some tips on reading Xml with the older Xml library.

First, XPath is your friend. It lets you query elements pretty quickly, in a way that is (very) vaguely similar to Linq. In this case, you should change your XPath to get the list of child "file" elements, rather than the parent "files" element.

XmlNodeList nodes = root.SelectNodes("//files");

Becomes

XmlNodeList files = root.SelectNodes("//file");

The //ElementName searches recursively for all elements with that name. XPath is pretty cool, and you should read up on a bit. Here are some links:

Once you have those elements, you can use the XmlElement.Attributes property, coupled with the XmlAttribute.Value property (file.Attributes["url"].Value).

Or you can use the GetAttribute method.

Click this link to the documentation on XmlElement for more info. Remember to switch the .Net Framework version to 2.0 on that page.

XmlElement root = doc.DocumentElement;
XmlNodeList files = root.SelectNodes("//file"); // file element, not files element

int numberOfFiles = files.Count;
// Todo: Update progress bar here

foreach (XmlElement file in files) // These are elements, so this cast is safe-ish
{
    string url = file.GetAttribute("url");
    string folder = file.GetAttribute("folder");

    // If not an integer, will throw.  Could use int.TryParse instead
    int system = int.Parse(file.GetAttribute("system"));
    int size = int.Parse(file.GetAttribute("size"));

    // convert this to a byte array later
    string checksum = file.GetAttribute("checksum");
}

For how to convert your checksum into a byte array, see this question:

How can I convert a hex string to a byte array?

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • I really appreciate your reply and time, it covers all my doubts and more over with easy to understand examples and pointing to several interesting documentations, you even took the time to show that I could use int.Parse or int.TryParse, thank you very much. – Prix Mar 02 '11 at 05:59
  • @Prix: No problem. You could also look into Xml serialization to do this, which is the solution I would recommend, though that's even more complicated :) – Merlyn Morgan-Graham Mar 02 '11 at 23:16
  • I am actually looking into it, making it working was the first step hehe, I was actually aiming to serialize it from the begin but since I was not doing well I came to request some backup ehhehe ;) – Prix Mar 03 '11 at 00:16
0

EDIT:

you should be able to use nodes[0].ChildNodes.Count;.

Bala R
  • 107,317
  • 23
  • 199
  • 210
0

You can count a number of elements by getting a length of your collection:

int ElementsCount = nodes.Count;

You can read attributes as following:

foreach(XmlNode node in nodes) {
    Console.WriteLine("Value: " + node.Attributes["name_of_attribute"].Value;
}
Laserson
  • 535
  • 1
  • 8
  • 21