Open and Read XML file in C#

Total Post:135

Points:947
C# 
C#
 1767  View(s)
Ratings:
Rate this:
Hi Developers,

How can I open and read the XML file using C#?


  1. Post:378

    Points:2694
    Re: Open and Read XML file in C#

    Hi Chintoo,

    You can use this code:

    using System;
    using System.Data;
    using System.Windows.Forms;
    using System.Xml;
    using System.IO;
     
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                XmlDataDocument xmldoc = new XmlDataDocument();
                XmlNodeList xmlnode;
                int i = 0;
                string str = null;
                FileStream fs = new FileStream("product.xml", FileMode.Open, FileAccess.Read);
                xmldoc.Load(fs);
                xmlnode = xmldoc.GetElementsByTagName("Product");
                for (i = 0; i <= xmlnode.Count - 1; i++)
                {
                    xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
                    str = xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + " | " + xmlnode[i].ChildNodes.Item(1).InnerText.Trim() + " | " + xmlnode[i].ChildNodes.Item(2).InnerText.Trim();
                    MessageBox.Show(str);
                }
            }
        }
    }

      Modified On Mar-30-2018 04:09:54 AM

Answer