This post shall deal with Inserting, Deleting and Modifying XML data using Linq to XML.
Here is how the XML will look like:
<?xml version="1.0" encoding="utf-8" ?> <access> <user id="149"> <name>Tarun</name> <age>22</age> <company>ABC</company> </user> <user id="13"> <name>Gagan</name> <age>23</age> <company>Birla</company> </user> <user id="45"> <name>Sachin</name> <age>24</age> <company>Steria</company> </user> </access>
Add this XML to your project or create one using the XML Editor and set its Build Action to “Content” instead of Resource and also set Copy to Output Directory to “Copy if Newer”. With its build action set to content, I can modify the XML.
The code to Load the XML using the XDocument class under the namespace System.Xml.Linq
XDocument xDoc = XDocument.Load("XMLAccess.xml"); XElement rootElem = xDoc.Root;
And here is the way to Insert a new Record(User) into the XML :
rootElem.Add(new XElement("user", new XAttribute("id", 90), new XElement("name", "Pulkit"), new XElement("age", 19), new XElement("company", "Infosys"))); xDoc.Save("XMLAccess.xml");
Its quite simple using Linq, and a lot messier in case of the standard XmlDocument class.
The code to delete a User:
XElement delElem = (from XElement elem in xDoc.Descendants("user") where int.Parse(elem.Attribute("id").Value) == 90 select elem).Single<XElement>(); delElem.Remove(); xDoc.Save("XMLAccess.xml");
So the above Linq expression will return a single XElement of type user of id=90 and gets removed using a simple Remove() function.
And the code to update the XML :
XElement updElem = (from XElement elem in xDoc.Descendants("age") where int.Parse(elem.Parent.Attribute("id").Value)==45 select elem).Single<XElement>(); updElem.SetValue(48); xDoc.Save("XMLAccess.xml");
That’s it for now. Happy coding.
This is a small post regarding the confusion that most XLINQ developers have when they start learning it, that what is the difference between Elements and Descendants?
Well basically both of them take the same arguments and also both return IEnumerable of type XElement, so what makes them different?
Lets take an Example :
here is the XML(it has a root node “users”) :
So the exercise is the return the collection of node “mname” :
First let us use Elements,
XDocument xDoc=XDocument.Load("C:\MyXml.xml"); IEnumerable<XElement> query = From XElement elem in xDoc.Element("users").Element("user").Element("model").Elements("mname") Select elem;
As you can see to get to that node we have to start from the top and reach mname node.
Not to forget that, the above code will return only the first user(ie. Tarun), it won’t go to the next user. I would suggest you not to use Elements.
But that’s not the case with Descendants. With Descendants, it returns all the node mname, irrespective of its parent node.
Like this :
IEnumerable<XElement> query=From XElement elem in xDoc.Descendants("mname") Select elem;
Hope you got the difference.
Have a nice day!
- How to convert bytes to BitmapImage WPF
- Auto-fixing height and width of Button in WPF DataGrid
- How to enlarge or re-size Calendar in WPF DatePicker
- Saving forms settings(location, height) using Serialization
- Give underline effect to a textbox in WPF using Adorners
- Tricky delegates
- How to dynamically load assemblies including unreferenced assemblies
- Using Extension methods – Convert Image to Byte or Base64
- Allow numbers or letters and disable right-click in textbox
- How to use Owner Drawn Controls
- WPF Tutorial : Drawing Visual