Reading and Writing XML File in C#

The following code is used for writing XML file and also reading values from XML file using ASP.NET with C#.
Here I provide the code for read the values from XML Node Element.
And also Create a new XML file.



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

/// <summary>
/// This XmlRead method retrieves the values from XML file
/// </summary>
public void XmlRead()
{
string document = "D:\\dev\\practice\\ReadWriteXML\\XMLEmployee.xml";
XmlDataDocument myxmlDocument = new XmlDataDocument();
myxmlDocument.Load(document);
XmlTextReader xmlReader = new XmlTextReader(document);

while (xmlReader.Read())
{
switch (xmlReader.NodeType)
{
case XmlNodeType.Element:
{
if (xmlReader.Name == "connectionString")
{
txtConnectionString.Text = xmlReader.ReadString();
}
if (xmlReader.Name == "AuditServer")
{
txtAuditServer.Text = xmlReader.ReadString();
}
if (xmlReader.Name == "AuditPort")
{
txtAuditPort.Text = xmlReader.ReadString();
}
if (xmlReader.Name == "DateFormat")
{
txtDateFormat.Text = xmlReader.ReadString();
}
if (xmlReader.Name == "TimeFormat")
{
txtTimeFormat.Text = xmlReader.ReadString();
}
break;
}
}
}
}

protected void btnRead_Click(object sender, EventArgs e)
{
XmlRead();
}
protected void btnWrite_Click(object sender, EventArgs e)
{
XmlWrite();
}

/// <summary>
/// This XmlWrite method creates a new XML File
/// </summary>
private void XmlWrite()
{
XmlTextWriter xmlWriter = new XmlTextWriter("D:\\XMLEmployee.xml", null);

xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("tool");

xmlWriter.WriteStartElement("connectionString", "");
xmlWriter.WriteString(txtConnectionString.Text);
xmlWriter.WriteEndElement();

xmlWriter.WriteStartElement("AuditServer", "");
xmlWriter.WriteString(txtAuditServer.Text);
xmlWriter.WriteEndElement();

xmlWriter.WriteStartElement("AuditPort", "");
xmlWriter.WriteString(txtAuditPort.Text);
xmlWriter.WriteEndElement();

xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();

// close writer
xmlWriter.Close();
}
}


Comments

Author: Phagu Mahato01 Jan 2014 Member Level: Gold   Points : 8

Reading XML Documents

In my sample application, I am using Example.xml to read and display its data through XmlTextReader. This file comes with VS.NET samples. You can search this on your machine and change the path of the file in the following line:

XmlTextReader textReader = new XmlTextReader("C:\\Example.xml");

Or you can use this given code nippet to reading any XML file.


using System;

using System.Xml;

using System.Data;

using System.Data.OleDb;

namespace ReadingXML

{

class readxml

{

static void Main(string[] args)

{

OleDbConnection con = new OleDbConnection();

con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Example.mdb";

OleDbDataAdapter data1 = new OleDbDataAdapter("Select * from Student", con);

DataSet ds = new DataSet();

data1.Fill(ds, "Student");

dset.WriteXml("C:\\Example.xml");

}

}

}


Sample code snippet for how to writing Writing XML File in C#


using System.Xml;

public class Program
{
public static void Main()
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

XmlWriter writer = XmlWriter.Create("Products.xml", settings);
writer.WriteStartDocument();
writer.WriteComment("This file is generated by the program.");
writer.WriteStartElement("Product");
writer.WriteAttributeString("StudentID", "001");
writer.WriteAttributeString("StudentName", "Aman");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: