Lesson 34: Learning to read XML files with the XmlNodes class in #C

The XMLDocuments class was used to read the XML file in the previous lecture. Enroll in a new class at The class XmlNodes, which is required for parsing and reading XML files, was introduced in the previous lesson’s example.

XmlNodes

In generally, an XML file is parsed into an XmlNodes, which is the file’s root element, and then into the childNodes property, which allows you to access the main element’s child elements.


Also, the XmlNodes class allows access to many other information such as name tags or name tag properties Or attributes, the text inside the tags or text inside also gives us the existence of the XML structure.


In this lesson, we are going to give a brief overview of some of the interesting aspects of the XmlNode class, because Having information about XmlNode as one of the key aspects when reading XML files by The XMLDocument class is very important.

In the examples in this lesson, we will use the DocumentElement element a lot, and since this element It is of the XMLElement type and XMLElement itself is inherited from the XmlNode class, in fact this type Friendships are almost the same.
The Property Name property simply declares the name of the element or node. For example, the code output is below the value “User” will be:

XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml (” A user node “);
Console.WriteLine (xmlDoc.DocumentElement.Name);
Console.ReadKey ();


The Property InnerText property also extracts the text between the open and closed tags of each tag, as in the following code:


XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml (” InnerText is here “);
Console.WriteLine (xmlDoc.DocumentElement.InnerText);
Console.ReadKey ();

The InnerXml property is slightly similar to the InnerText property, but while the InnerText property does not Removes the XML that is inside, property InnerXml explicitly does not do this.

Example The following shows the difference:


XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml (” InnerText / InnerXml is here “);
Console.WriteLine (“InnerXml:” + xmlDoc.DocumentElement.InnerXml);
Console.WriteLine (“InnerText:” + xmlDoc.DocumentElement.InnerText);
Console.ReadKey ():

The Property OuterXml property works the same as InnerText, except that in addition to the XML code Their text also shows.

The following code can show the difference between the two properties:
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml (” InnerText / InnerXml is here “);
Console.WriteLine (“InnerXml:” + xmlDoc.DocumentElement.InnerXml);
Console.WriteLine (“OuterXml:” + xmlDoc.DocumentElement.OuterXml);
Console.ReadKey ();


In the following example code, we also worked with the properties or attributes of the tags:


XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml (” “);
if (xmlDoc.DocumentElement.Attributes [“name”]! = null)
Console.WriteLine (xmlDoc.DocumentElement.Attributes [“name”]. Value);
if (xmlDoc.DocumentElement.Attributes [“age”]! = null)
Console.WriteLine (xmlDoc.DocumentElement.Attributes [“age”]. Value);
Console.ReadKey ();

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.