159-read-xml-file-and-write-xml-file-in-c

To read XML file in C# or to write XML file in C# it is best to use System.Xml namespace. Namespace System.Xml.Linq; is also useful for this purpose. Now, to write XML file, we need one object of XmlWriterSettings class.

XmlWriterSettings xml_WS = new XmlWriterSettings();
xml_WS.Indent = true;

Then we need one XmlWriter object.
XmlWriter xml_Wr = XmlWriter.Create(saveFileDialog1.FileName, xml_WS);

Then we can start new document and add a comment.

xml_Wr.WriteStartDocument();
xml_Wr.WriteComment(“Just a comment”);

Further, we can add an element and attribute string
xml_Wr.WriteStartElement(“KmUpload”);
xml_Wr.WriteAttributeString(“xsi”, “noNamespaceSchemaLocation”, “http://www.w3.org/2001/XMLSchema-instance”, “http://www.yoursite.com/xml/”);

and later, we can add more elements and strings and write end elements.

xml_Wr.WriteStartElement(“KmRegi”);
xml_Wr.WriteStartElement(“Chassis”);
xml_Wr.WriteString(“WF701KFXY3T955543”);
xml_Wr.WriteEndElement();

At the end, we must close all of the elements by writing end element, write end of the document, flushing document and closing the document
xml_Wr.WriteEndElement();
xml_Wr.WriteEndElement();
xml_Wr.WriteEndDocument();
xml_Wr.Flush();
xml_Wr.Close();

Whole code is here:
XmlWriterSettings xml_WS = new XmlWriterSettings();
xml_WS.Indent = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{

XmlWriter xml_Wr = XmlWriter.Create(saveFileDialog1.FileName, xml_WS);
xml_Wr.WriteStartDocument();
xml_Wr.WriteComment(“Just a comment”);
xml_Wr.WriteStartElement(“KmUpload”);
xml_Wr.WriteAttributeString(“xsi”, “noNamespaceSchemaLocation”, “http://www.w3.org/2001/XMLSchema-instance”, “http://www.yoursite.com/xml/”);
xml_Wr.WriteStartElement(“KmRegi”);
xml_Wr.WriteStartElement(“Chassis”);
xml_Wr.WriteString(“WF701KFXY3T955543”);
xml_Wr.WriteEndElement();
xml_Wr.WriteStartElement(“Km”);
xml_Wr.WriteString(“45000”);
xml_Wr.WriteEndElement();
xml_Wr.WriteEndElement();
xml_Wr.WriteEndElement();
xml_Wr.WriteEndElement();
xml_Wr.WriteEndDocument();
xml_Wr.Flush();
xml_Wr.Close();

}

For reading XML data, we can use XmlReader object.

XmlReader xml_Read = XmlReader.Create(str_xmlFile);
while (xml_Read.Read())
{

if (xml_Read.NodeType == XmlNodeType.Element && xml_Read.Name == “Chassis”)
{

textBox1.Text += “Atribute (Chassir): ” + xml_Read.ReadElementContentAsString();
textBox1.Text += System.Environment.NewLine;

}

}

However, by using System.Xml.Linq; namespace, we can simply read the XML data by using LINQ. First, we need object of XDocument class and then to load it from the file.
XDocument xdoc = XDocument.Load(str_file_to_open);

Then we need to put LINQ query by using method Descendants and giving node name to it.

var query = from c in xdoc.Descendants(“Earning”)
select c.Element(“Name”).Value + “;” +
c.Element(“Surname”).Value;

foreach (string employee in query)
{

MessageBox.Show(employee);

}

Here is the whole code:

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{

string str_file_to_open;
str_file_to_open = openFileDialog1.FileName;
this.Text = str_file_to_open;

var query = from c in xdoc.Descendants(“Earning”)
select c.Element(“Name”).Value + “;” +
c.Element(“Surname”).Value;

foreach (string employee in query)
{

MessageBox.Show(employee);

}

}

External links:

Read XML File in C# on Stackoverflow
Read XML File in C# on Microsoft
Read XML File in C# on csharp.net-informations.com
Write XML File in C# on Dotnetperls
Write XML File in C# on csharp.net-tutorials.com
Write XML File in C# on csharp.net-informations.com

Posted in c-sharp-code-examples

Leave a Reply

Your email address will not be published. Required fields are marked *

*