TOC

This article is currently in the process of being translated into Polish (~32% done).

XML:

Reading XML with the XmlDocument class

Jak opisano w poprzednim rozdziale, XmlDocument jest bardziej pamięciożerny i prawdopodobnie trochę wolniejszy, niż XmlReader, jednakże, w wielu wypadkach, XmlDocument jest łatwiejszy w obsłudze i często wymaga mniej kodu. Raz wczytana treść XML pozwala odczytać dane zgodnie z hierarchią struktury XML - z elementami nadrzędnymi, które posiadają w sobie elementy podrzędne, które to posiadają swoje elementy podrzędne itd. W poprzednim rozdziale analizowaliśmy dane XML z Europejskiego Banku Centralnego zawierające aktualne notowania kursów walut a teraz zrobimy to samo, tylko użyjemy do tego celu klasy XmlDocument.

Wspomniany plik XML można odszukać pod adresem URL (http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml), podczas gdy potrzebne nam dane znajdują się w elementach Cube. W strukturze drzewiastej wygląda to mniej więcej tak:

<gesmes:Envelope>
    [other child nodes]
    <Cube>
<Cube time="2011-04-12">
    <Cube currency="USD" rate="1.4470"/>
    <Cube currency="JPY" rate="121.87"/>
    …

The gesmes:Envelope is our root element, which we can access using the DocumentElement property. We will then be able to access children of this node by using the ChildNodes collection property. In our example, we want the child nodes three levels below the root/document element. We can do that using the following code, which essentially does the same as the XmlReader based code in the previous chapter:

using System;
using System.Text;
using System.Xml;

namespace ParsingXml
{
    class Program
    {
static void Main(string[] args)
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
    foreach(XmlNode xmlNode in xmlDoc.DocumentElement.ChildNodes[2].ChildNodes[0].ChildNodes)
Console.WriteLine(xmlNode.Attributes["currency"].Value + ": " + xmlNode.Attributes["rate"].Value);
    Console.ReadKey();
}
    }
}

As you can see, we access the Cube nodes by going down the ChildNodes hierarchy. From the DocumentElement (the root element), we ask for the third child node (zero-index based), then we ask for the first child node of that, and then we ask for the entire collection of child nodes. Obviously this is only possible because we know the structure of the XML document, and it's definitely not very flexible, pretty or easy to change later on. However, the way you navigate an XML document very much depends on the XML source and the data you need. For this example, the above will work just fine and even with a very limited amount of code, but for other purposes, you may want to use a bit more code to increase the readability.

Once we have a node with a currency rate, we access the two properties we're interested in and then output them to the console, just like the example in the previous chapter.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!