LINQ to XML CRUD Operations


CRUD (Create, Read, Update & Delete) four operations for adding, displaying, modifying and deleting data.  This articles explains how CRUD operation can be achieved using  LINQ to XML. (You can download the entire article from here).

For example you want to store  Members information in an XML file and you may need to implement  add /  edit / delete functionality through a  UI.

So I am going to create a XML file with four nodes like firstName,lastName,email and phone.

<?xml version="1.0" encoding="utf-8"?>
<Members>
 <member id="f02c78b4956f4b2f9ecfb7a5bfbcfbf6">
 <firstName>Deepu</firstName>
 <lastName>M.I</lastName>
 <email>me@gmail.com</email>
 <phone>123-456-7890</phone>
 </member>
</Members>

Screen shots for CRUD Operations (Create Screen)

Using the following method we can Create a new member information to the XML file.


XDocument doc = XDocument.Load(FilePath);  //load the xml file.
 IEnumerable<XElement> oMemberList = doc.Element("Members").Elements("member");
var oMember = new XElement("member",
 new XAttribute("id", Guid.NewGuid().ToString().Replace("-", "")),
 new XElement("firstName", txtFirstName.Value),
 new XElement("lastName", txtLastName.Value),
 new XElement("email", txtEmail.Value),
 new XElement("phone", txtPhone.Value)
 );
oMemberList.Last().AddAfterSelf(oMember);  //add node to the last element.
doc.Save(FilePath);

XDocument class will load the xml file and will find the last member node and save the information to the file.

The next screen will show all the members in the xml file in Grid format using ListView control.

XElement element = XElement.Load(FilePath);  //replace with xml file path
if (element != null)
{
var query = from member in element.Descendants("member")
select new
{
MemberId = (string)member.Attribute("id"),
FirstName = member.Element("firstName").Value,
LastName = member.Element("lastName").Value,
Email = member.Element("email").Value,
Phone = member.Element("phone").Value
};
if (query != null && query.Count() > 0)
{
lvMemberList.DataSource = query.ToList();
lvMemberList.DataBind();
}
}

Update method is similar to create method like load a xml file and find the corresponding member node by Id or email and finally it will update node values using SetElementValue method some thing like below.


XDocument doc = XDocument.Load(FilePath); //replace with xml file path
IEnumerable<XElement> oMemberList = doc.Element("Members").Elements("member"); //get the member node.
var oMember = (from member in oMemberList
 where
 member.Attribute("id").Value == 2
 select member).SingleOrDefault(); //replace memberId by querystring value.
 oMember.SetElementValue("firstName", firstName);
 oMember.SetElementValue("lastName", lastName);
 oMember.SetElementValue("email", email);
 oMember.SetElementValue("phone", phone);
 doc.Save(FilePath);

Finally the Delete will be deleteing a node element from the xml file.


XElement element = XElement.Load(FilePath);
 if (element != null)
 {
 var xml = (from member in element.Descendants("member")
 where
 member.Attribute("id").Value == 2
 select member).SingleOrDefault();

 if (xml != null)
 {
 xml.Remove();
 element.Save(FilePath);
 }
 }

Note : This post does not have the complete source code. please download the full working source code from the download link.

You can download the entire article from here copy paste this URL

http://www.4shared.com/file/236175264/b5df239/CRUD.html

Hope this help and If you have any comments, please feel free to write your feedback.

Thanks
Deepu

9 thoughts on “LINQ to XML CRUD Operations

  1. Pingback: LINQ to XML operaciones CRUD

Leave a comment