Lesson 33 Tutorial on XML Files with XmlDocument in #C Language

The XmlDocument takes up more memory than the Xmlreader Class, as we discussed in the last session. It’s being devoured and moving more slowly than it should.

However, there are a few advantages to working with the XmlDocument Class. It is simpler and requires less coding in certain circumstances.

XmlDocument


By using a tree approach (Hierarchical (evaluated, just as in the structure of XML files where each element has a name and a value), you may access the information within the file as soon as you read the contents. It’s possible for each kid to have a number of children’s elements, and each child may have many children’s elements.


In the previous lesson, we read information from an XML file for the European Central Bank that about exchange rates and Their changes provide data. In this lesson we will do this, but by using Xmlreader Class instead of XMLreader.


The XML file information can be received from the +lesson address and the data we need in the element <cube <cube. The tree structure of the xml file is the same as the code below:

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

Element <Envelope: Gesmes is the file or Element root of the file, which can be used by property Documentele had access to it.


In the next step, we can use the Collection Childnodes property to the elements of the main element Get access.

In the example of this lesson, we intend to go to the elements of the child in three categories or Level below Get access to the main element.

This can be done using the following practical example code, exactly the same What we did in the previous lesson with the Xmlreader Card:

using System;
using System.Text;
using System.Xml;
namespace ParsingXml
{
 class Program
 {
 static void Main(string[] args)
 {
 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load("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();
 }
99
 }
}

As you can see in the example code, we to the nodes cube element by going lower than the tree structure Hierarchy Childnodes have access Of the original file element called docuceteloment, The third child of the Node Child’s child (with zero -based index (we’ve got it.

Then follow the first child This element or node child is made and finally all elements of this child’s nodes child of collection We have requested.


Obviously the above code is used because we are familiar with the XML file structure and for the steps Next in the future is not very flexible.


Anyway, the way you will use an XML file depends a lot on the XML file source
And the kind of information you are looking for.


The example of this lesson will only work if we have a limited amount of information but for the purposes The larger, we need to write a better code to increase the readability of the program.


Because we have a node or node called Rate Currency and we can go to two property or Properties access and print it in the output, the code is a good example like the previous lesson.

About Author:

My name is Wajahat Hussain Founder of bestinformativeblog. I welcome you to the Operating System of Blog – bestinformativeblog. I Am An Blogger By Passion. Well experienced for more than 4+ years in SEO, WordPress, Blogging and how to make Passive income, Explore about me by clicking below.