Net-informations.com
SiteMap  | About    

How to open and read XML file in C#

XML is a self describing language and it gives the data as well as the rules to extract what data it contains. Reading an XML file means that we are reading the information embedded in XML tags in an XML file.

C# XML Parser

how to load xml in C#

In the previous program we create an XML file and named it as products.xml. The following C# program read that file and extract the contents inside the XML tag. We can read an XML file in several ways depends on our requirement. This program read the content in Node wise . Here we are using XmlDataDocument Class to read the XML file . In this program it search the Node and its child Nodes and extract the data in child nodes.

Click here to download the input file : product.xml

How to read XML from a file

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); } } } }

Reading Xml with XmlReader

XmlReader opens and parses XML files. It is a faster and less memory consuming alternative. It lets you run through the XML string one element at a time, while allowing you to look at the value, and then moves on to the next XML element. It provides a lower-level abstraction over the XML file structure.

C# XML Handling

XmlReader xReader = XmlReader.Create(new StringReader(xmlNode)); while (xReader.Read()) { switch (xReader.NodeType) { case XmlNodeType.Element: listBox1.Items.Add("<" + xReader.Name + ">"); break; case XmlNodeType.Text: listBox1.Items.Add(xReader.Value); break; case XmlNodeType.EndElement: listBox1.Items.Add(""); break; } }

Full Source : Reading Xml with XmlReader

Reading XML with XmlTextReader

C# XML Handling

XmlTextReader provides direct parsing and tokenizing of XML and implements the XML specification as well as the namespaces in the XML specification from the W3C. XmlTextReader Class provides forward-only, read-only access to a stream of XML data.

XmlTextReader xmlReader = new XmlTextReader("d:\\product.xml"); while (xmlReader.Read()) { switch (xmlReader.NodeType) { case XmlNodeType.Element: listBox1.Items.Add("<" + xmlReader.Name + ">"); break; case XmlNodeType.Text: listBox1.Items.Add(xmlReader.Value); break; case XmlNodeType.EndElement: listBox1.Items.Add(""); break; } }

Full Source : Reading Xml with XmlReader

Using XPath with XmlDocument

Introduction to XML with C#

XmlDocument reads the entire XML content into memory and then allow you to navigate back and forward in it or even query the XML document using the XPath technology.

Click here to download the input file : product.xml




using System;
using System.Data;
using System.Xml;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("d:\\product.xml");
            XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/Table/Product");
            string proID = "", proName = "", price="";
            foreach (XmlNode node in nodeList)
            {
                proID = node.SelectSingleNode("Product_id").InnerText;
                proName = node.SelectSingleNode("Product_name").InnerText;
                price = node.SelectSingleNode("Product_price").InnerText;
                MessageBox.Show(proID + " " + proName + " " + price);
            }
        }
    }
}

 C# XML - Related Contents