Introduction
I am a beginner as far as C# and especially XML are concerned. To learn about XML handling, I read many documents on the internet and on CodeProject. But they are too complex for beginners to understand at first. Here, I summarize whatever I learned about XML handling.
Background
I will show you how to create an XML file, read the value in the program and update the values back to XML file. I don't know much about LINQ, but it is used here to read from the XML file. To me, it seems to be the easiest way.
Using the Code
I wrote these four methods:
CreateXMLFile()
- If file does not exist, create it using XElement
and save it. It creates the following XML file:
="1.0"="utf-8"
<users>
<user>
<name>admin</name>
<password>password</password>
</user>
<user1>
<name>admin</name>
<password>password</password>
</user1>
</users>
public void CreateXMLFile()
{
string fileName = "XMLFile.xml";
string valuename = "admin", valuepassword = "password";
if (!File.Exists(fileName))
{
XElement users =
new XElement("users",
new XElement("user",
new XElement("name", valuename),
new XElement("password", valuepassword))
,new XElement("user1",
new XElement("name", valuename),
new XElement("password", valuepassword))
);
users.Save(fileName);
MessageBox.Show("XML File Created");
}
else
{
MessageBox.Show("XML File Already Exists");
}
}
LoadXMLFile()
- To read values from XML file, use XElement
and LINQ query. You can read more about LINQ here.
public void LoadXMLFile()
{
string fileName = "XMLFile.xml";
if (!File.Exists(fileName))
{
MessageBox.Show("Create XML File before Loading");
}
else
{
XElement dataElements = XElement.Load(fileName);
textBox1.Text = new XElement("users",
(from c in dataElements.Elements("user")
select new XElement("user", c.Element("name"))).Take(1)).Value;
textBox2.Text = new XElement("users",
(from c in dataElements.Elements("user")
select new XElement("user", c.Element("password"))).Take(1)).Value;
}
}
SaveXMLFile()
- To update a specific node, you need to trace it using Element
method of XElement
. To replace the value, use ReplaceNodes
method:
public void SaveXMLFile()
{
string fileName = "XMLFile.xml";
if (!File.Exists(fileName))
{
CreateXMLFile();
}
XElement dataElements = XElement.Load(fileName);
dataElements.Element("user").Element("name").ReplaceNodes(textBox1.Text);
dataElements.Element("user").Element("password").ReplaceNodes(textBox2.Text);
dataElements.Save(fileName);
}
DeleteXMLFile()
- This uses the basic System.IO
. If the file exists, delete it using File.Delete
:
public void DeleteXMLFile()
{
string fileName = "XMLFile.xml";
if (File.Exists(fileName))
{
File.Delete(fileName);
MessageBox.Show("XML File Deleted");
}
else
{
MessageBox.Show("Create XML File before Deleting");
}
}
History
- V1.0 April 12th 2009: Initial version
This is the first version of the article, and it is my first article on CodeProject, so suggestions on improving the style of writing and anything else are welcome.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.