开发者

Counting elements and reading attributes with .net2.0?

开发者 https://www.devze.com 2023-02-14 02:04 出处:网络
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.

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 i开发者_高级运维t ...
}

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.


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:

  • http://msdn.microsoft.com/en-us/library/hcebdtae.aspx
  • http://msdn.microsoft.com/en-us/library/d271ytdx.aspx

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?


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;
}


EDIT:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消