How to open and read XML file in C#

           In the previous section, we have learnt how to create XML file in C#.  Today we are going to learn how to open and read the XML file in C#. Reading an XML file means that we are reading the information embedded in XML tags.

           The following C# read the file with the name Product.xml and extract the content inside the XML tags. 

 public static void Main(string[] args)
             XmlDataDocument xmldoc = new XmlDataDocument();
            XmlNodeList xmlnode ;
            int i = 0;
            string str = null;
            FileStream fs = new FileStream("product.xml", FileMode.Open, FileAccess.Read);
            xmlnode = xmldoc.GetElementsByTagName("Product");
            for (i = 0; i <= xmlnode.Count - 1; i++)
                str = xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + "  " + xmlnode[i].ChildNodes.Item(1).InnerText.Trim() + "  " + xmlnode[i].ChildNodes.Item(2).InnerText.Trim();
class to read the XML file. . Here we are using XmlDataDocument Class to read the XML file. We can read the XML file in several ways. In this program it search the Node and its child Nodes and extract the data in child nodes.

How to open and read XML file in C# How to open and read XML file in C# Reviewed by Vijay Vardhan on 20:03:00 Rating: 5

No comments: