Read Write XML Data with C# in file

Tags:

 

 

Requirements
Microsoft Visual studio 2008
http://www.microsoft.com/visualstudio/en-us/default.mspx
 
 
Creating a XML document
To create a XML document
 

using

System.Xml.Linq;
..

#region

CreateCustomerXMLfile

public

static void CreateCustomerXMLfile(string filename)
{
  XElement connections = 
  new XElement("Customers"
  new XElement("Customer",
  n
ew XElement("Id", "0")
  new XElement("Name", "default")));
  connections.Save(filename);
}

#endregion

 

To call the function and create the XML file in c:\temp\customer.xml

CreateCustomerXMLfile("c:\\temp\\customer.xml");
 

 
Add a record to the Customer XML document
To add a record to the customer xml document we create the following function
 

#region

AddCustomerToXMLfile
public static string AddCustomerToXMLfile(string XMLfile, string Id, string Name)
{
  XDocument XMLDoc = XDocument.Load(XMLfile);
  XElement root = XMLDoc.Root;
  root.Add(
new XElement("Customer",
  new XElement("Id", Id),
  new XElement("Name", Name)));
  XMLDoc.Save(XMLfile);
  return Id;
}
#endregion
 
Call the function a new record will be created with id 1 name Mark

AddCustomerToXMLfile("c:\\temp\\Customer.xml","1","Mark");

 
Update a record in a XML document
 
First we create a customer class
 

#region

Customer Class
public class Customer
{
public string ID;
public string NAME;
}
#endregion
 
Next we create the function to update the specific record we will call this function UpdateCustomerInXMLfile.
The function wil try to find the Id in the XML document and update it with the Name parameter.
 

#region UpdateCustomerInXMLfile
string
UpdateCustomerInXMLfile(string XMLfile, string Id, string Name)
{
Customer customer = new Customer();
XDocument XMLDoc = XDocument.Load(XMLfile);

IEnumerable<XElement> elem_list = from elem in XMLDoc.Descendants("Customer")
                                                     where elem.Element("Id").Value == customer.ID
                                                     select elem;

XElement[] elem_array = elem_list.ToArray();
foreach (XElement elem in elem_array)
{
  elem.Element("Id").Value = customer.ID.ToString(); 
  elem.Element(
"Name").Value = customer.NAME.ToString();
}
return Id;
}

#endregion

 
Now we are ready to call the update function

UpdateCustomerInXMLfile(

"c:\\temp\\Customer.xml", "1", "Johny");
The name "Mark" will have changed in  the XML document to "Johny"
 
www.programmingsystems.eu

 

Comments

How to read and write XML in C#

Very nice article. I really enjoyed it reading. And it also cleared lot of my doubts about reading and writing XML in C#.Net, Well done job! Check this link too.... How to read and write XML in C# It helped me lot in completing my task. Thanks Everyone for precious post!

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.