Now we will take the second one:- LINQ to XML. Every programmer has had to parse an XML file.LINQ to XML makes it easy for you to query and modify XML content.NET includes a library to manage XML (System.XML), LINQ to XML extends this namespace bringing the standard query operators and integrating them with XML documents (System.XML.Linq). We can load an XML document into memory and then use LINQ syntax to query it. For a developer accustomed with the SQL queries, LINQ syntax may seem like an up-side down language, but the elements of this syntax have a very comprehensive logic and the query develops naturally. Let’s go through the CURD operations with simple examples so that we will be clearer.Let’s take a sample xml file. Something like:
Now we are going to make a LINQ Query to this xml file. Please see the query below.
private void ReadFromXml()
private void ReadFromXml()
{
try
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("TestFile.xml"));
var MyFriends = from friend in xmlDoc.Descendants("Friend")
select new
{
Name = friend.Element("Name").Value,
City = friend.Element("City").Value,
Dob = DateTime.Parse(friend.Element("Dob").Value)
};
foreach (var friend in MyFriends)
{
Console.WriteLine("{0} {1}, {2}",
friend.Name, friend.City, friend.Dob);
}
}
catch (Exception)
{
throw;
}
}
This method makes use of LINQ to select all the data from the XML file, and then display it in the Literal control by looping through each item. Now if needed we can have some custom collection something like this.This will also help us to maintain a interface between xml and Linq for complex data representations.Also will make maintanance easy.But afcource for our simple code sample this is an optional.
public class Friends
{
public string Name { get; set; }
public string City { get; set; }
public DateTime Dob { get; set; }
}
And we can just attach this to data source.
_contactDataGrid.ItemsSource = MyFriends.ToList<Friends>();
Simple right? Now we will go ahead and see how we can add a node to this xml file using LINQ. Using the classes from the System.Xml.Linq namespace, and the features available in .NET 3.5, constructing an XML document is very easy and very readable.
private void AddtoXML()
{
try
{
XDocument xdocumnet = XDocument.Load(Server.MapPath("TestFile.xml"));
xdocumnet.Element("Friends").Add(new XElement("Friend",
new XElement("name", "Geetha"),
new XElement("City", "World bank"),
new XElement("Dob", "Project Manager")));
xdocumnet.Save(Server.MapPath("TestFile.xml"));
ReadXml();
}
catch{}
}
Constructing a document in this way is possible because of the functional construction feature in LINQ to XML. Functional construction is simply a means of create an entire document tree in a single statement. Just try to recollect what you were doing in place of this simple 2 line of code!
Interesting right? Now come with me we can try to go ahead and update one.
private void UpdateXMLUsingLINQ()
{
try
{
XDocument xdocument = XDocument.Load(Server.MapPath("TestFile.xml"));
var MyFriends = (from person in xdocument.Descendants("Friend")
where person.Element("Name").Value.Equals("Nelson")
select person).Single();
MyFriends.Element("City").Value = "Cochin";
xdocument.Save(Server.MapPath("TestFile.xml"));
}
catch{}
}
Nothing but just simple and interesting! Now we will delete one.
private void DeleteXMLbyLINQ()
{
try
{
XDocument xdocument = XDocument.Load(Server.MapPath("TestFile.xml"));
var persondata = (from person in xdocument.Descendants("Friend")
where person.Element("Name").Value.Equals("Nelson")
select person).Single();
persondata.Remove();
xdocument.Save(Server.MapPath("TestFile.xml"));
}
catch{}
}
I hope that these short examples offer a clear view on some LINQ on XML capabilities and that you will have now the curiosity to further explore the ways in which you can read data from an XML document, and, even more, to create, modify or transform XML files using the power of LINQ.We will take the LINQ to Dataset in Next blog.
No comments:
Post a Comment