Reading data from an XML file using C#
This article describes that how to read the data from XML file using C# ...
Introducion:
This article describes that how to read the data from XML file using C#. Before entering this concept, first of all we should know that what is XML? XML (Extensible Markup Language) is a very popular format used to store and share the data. XML stores information in a tree-based text format that allows you and me as well as computers to easily read the data.
Using the following code snippet, we can easily read the XML file date.
XmlTextReader reader = new XmlTextReader("C:\\links.xml");
while (reader.Read())
{
XmlNodeType nodeType = reader.NodeType;
switch (nodeType)
{
case XmlNodeType.Element:
Console.WriteLine("Element name is {0}", reader.Name);
if (reader.HasAttributes)
{
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
Console.WriteLine("Attribute is {0} with Value {1}: ", reader.Name, reader.Value);
}
}
break;
case XmlNodeType.Text:
Console.WriteLine("Value is: " + reader.Value);
break;
}
}
The above code loads an XML file called links.xml, using the while loop to look at each node, checks whether the node is an element or text, and depending on whether the node is an element or text, does something such as printing something to our console.
Summary:
This article has described that how to read the data from XML file using C#.
That was VERY helpful - thanks!