LINQ to XML is a built-in LINQ data provider that provides a clean programming model to read, construct and write XML data. We can use LINQ to XML to perform LINQ queries over XML. It provides much richer querying support than the low-level XmlReader/XmlWriter API in .NET. It uses less memory and more efficient than the XmlDocument, which is built on Document Object Model(DOM).
LINQ to XML allows to write Query Expressions over XML and can be combined with any of the other LINQ providers to create or use XML data as a source or destination format.
LINQ to XML uses very simple model for constructing XML documents. Whether it be XML sources from a stream or file, or XML created dynamically in the code. To use LINQ to XML concepts you should include namespace called using System.Xml.Linq;
To show demonstrated I have created small windows application in which you can know how to create, manipulate and delete the xml nodes through using LINQ to XML
To create XML file using LINQ
if (!File.Exists(@"d:\Sample.xml"))
{
XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Books",
new XElement("Book",
new XElement("Id", txtID.Text),
new XElement("Title", txtTitle.Text),
new XElement("ISBN", txtISBN.Text),
new XElement("Author", txtAuthor.Text))));
doc.Save(@"d:\Test.xml");
MessageBox.Show("XML File is Created");
clear();
}
XDeclaration, XComment, XProcessingInstruction classes are used to define xml declaration, xml comments and xml processing instructions respectively. XDocument class is used to represent the xml as an XMLDocument.
Your xml structure will be like this
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Books>
<Book>
<Id>100</Id>
<Title>C</Title>
<ISBN>19002892</ISBN>
<Author>Bala</Author>
</Book>
</Books>
To add nodes in Existing xml File
Adding XML to the existing XML document is very simple, we need only construct our XML using a mixture of XElement and XAttribute types (there are other ways also...) and then add them to the document.
The following adds a new book:
XDocument doc = XDocument.Load(@"d:\test.xml");
doc.Element("Books").Add(
new XElement("Book",
new XElement("Id", txtID.Text),
new XElement("Title", txtTitle.Text),
new XElement("ISBN", txtISBN.Text),
new XElement("Author", txtAuthor.Text)));
doc.Save(@"d:\test.xml");
MessageBox.Show("Data are Added");
clear();
Once you run this code you can notice the new node are added in the existing file.
Traversing XML File
To Get First Node
XDocument doc = XDocument.Load(@"d:\test.xml");
var node = doc.Element("Books").Descendants("Book").FirstOrDefault();
txtID.Text = node.Element("Id").Value;
txtTitle.Text = node.Element("Title").Value;
txtISBN.Text = node.Element("ISBN").Value;
txtAuthor.Text = node.Element("Author").Value;
To Get Last Node
XDocument doc = XDocument.Load(@"d:\test.xml");
var node = doc.Element("Books").Descendants("Book").LastOrDefault();
txtID.Text = node.Element("Id").Value;
txtTitle.Text = node.Element("Title").Value;
txtISBN.Text = node.Element("ISBN").Value;
txtAuthor.Text = node.Element("Author").Value;
To Get Next Node of current node
XDocument doc = XDocument.Load(@"d:\test.xml");
int id = Convert.ToInt16(txtID.Text) + 1;
var nodes = (from a in doc.Element("Books").Descendants("Book")
where a.Element("Id").Value == id.ToString()
select new
{
Id = a.Element("Id").Value,
Title = a.Element("Title").Value,
ISBN = a.Element("ISBN").Value,
Author = a.Element("Author").Value
}).ToList();
if (nodes.Count > 0)
{
txtID.Text = nodes.ElementAt(0).Id;
txtTitle.Text = nodes.ElementAt(0).Title;
txtISBN.Text = nodes.ElementAt(0).ISBN;
txtAuthor.Text = nodes.ElementAt(0).Author;
}
else
{
MessageBox.Show("This is Last record");
}
To Get Previous Node of current node
XDocument doc = XDocument.Load(@"d:\test.xml");
int id = Convert.ToInt16(txtID.Text) - 1;
var nodes = (from a in doc.Element("Books").Descendants("Book")
where a.Element("Id").Value == id.ToString()
select new
{
Id = a.Element("Id").Value,
Title = a.Element("Title").Value,
ISBN = a.Element("ISBN").Value,
Author = a.Element("Author").Value
}).ToList();
if (nodes.Count > 0)
{
txtID.Text = nodes.ElementAt(0).Id;
txtTitle.Text = nodes.ElementAt(0).Title;
txtISBN.Text = nodes.ElementAt(0).ISBN;
txtAuthor.Text = nodes.ElementAt(0).Author;
}
else
{
MessageBox.Show("This is First record");
}
To update the current Node
Updating XML data is also very simple; Just pick the element/attribute you wish to update and then set its new value.
XDocument Doc= XDocument.Load(@"D:\Test.xml");
XElement element = (from a in Doc.Element("Books").Descendants("Book")
where a.Element("Id").Value == txtID.Text
select a).FirstOrDefault();
if (element != null)
{
element.Element("Title").Value = txtTitle.Text;
element.Element("ISBN").Value = txtISBN.Text;
element.Element("Author").Value = txtAuthor.Text;
Doc.Save(@"D:\Test.xml");
}
To Remove the current Node
We simply have to get the object we want to delete and then delete it using the Remove() method.
XDocument Doc = XDocument.Load(@"D:\Test.xml");
XElement element = (from a in Doc.Element("Books").Descendants("Book")
where a.Element("Id").Value == txtID.Text
select a).FirstOrDefault();
if (element != null)
{
if (MessageBox.Show("Are you Sure to Delete?","", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
element.Remove();
}
}
Doc.Save(@"d:\Test.xml");
clear();
This is a sample application only but you can have more fun with Linq to Xml concepts.
Thanks for reading this article
No comments:
Post a Comment