This article is currently in the process of being translated into Spanish (~25% done).
If you are fluent in Spanish, then please help us - just point to any untranslated element (highlighted with a yellow left border - remember that images should have their titles translated as well!) inside the article and click the translation button to get started. Or have a look at the current translation status for the Spanish language.
If you see a translation that you think looks wrong, then please consult the original article to make sure and then use the vote button to let us know about it.
Please help us by translating the following metadata for the article/chapter, if they are not already translated.
If you are not satisfied with the translation of a specific metadata item, you may vote it down - when it reaches a certain negative threshold, it will be removed. Please only submit an altered translation of a metadata item if you have good reasons to do so!
Reading XML with the XmlDocument class
Se ha descrito en el capitulo anterior, el XmlDocument consume mas memoria y posiblemente sea un poco mas lento que XmlReader. A menudo, para muchos propósitos, el XmlDocument es mas fácil de trabajar y requiere menos código. Una vez que leíste el contenido XML, puedes trabajar los datos en forma jerárquica, con la estructura del XML, con elementos "root" y elementos "child" que tienen elementos "child", asi sucesivamente. anteriormente, analizamos elementos del "Banco Central Europeo" que nos informa de tipos de cambio de monedas, ahora haremos lo mismo usando XmlDocument
The XML can be found at the current URL (http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml) and the data we need is in the <cube> elements. In a tree structure, it looks something like this:
[other child nodes]
<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:
static void Main(string args)
XmlDocument xmlDoc = new XmlDocument();
foreach(XmlNode xmlNode in xmlDoc.DocumentElement.ChildNodes.ChildNodes.ChildNodes)
Console.WriteLine(xmlNode.Attributes["currency"].Value + ": " + xmlNode.Attributes["rate"].Value);
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.