Blocks4.NET .net components for .net developers
Interested in getting reliable email hosting or server web hosting for your business? Check up out today!

More on LINQ to XML. Creating and modifying XML documents

August 23, 2007 11:10 by blocks4

LINQ to XML also provides great usability when it comes to creating and modifying XML documents. The feature called “functional construction” allows us to create XML documents in a simplified and more efficient manner.

Here is a sequence for creating from scratch an XML document (comp is an object holding the necessary information):

XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),

new XElement(".net_components", new XElement("component", new XAttribute("Name", comp.Name), new XAttribute("Producer", comp.Description), 

new XAttribute("Price", comp.Price), new XAttribute("Available", comp.Available.ToString())))); 

doc.Save(@"C:\MyDocs\ComponentList.xml"); 

You can easily notice the top-down approach used for the creation of this small XML document. The XDocument object is used for specifying the XML Declaration Attributes: XML version, the XML document encoding and if the XML is standalone or requires external entities to be resolved. The main role in this contruction approach is played by the XElement class’s constructor:

public XElement(XName name, params object[] contents).

The contents parameter accepts an array of generic object types, so it’s worth checking into the list of different types of content that can be loaded. The Save method it’s easy to use, only by specifying a string argument which is the path for saving the XML file.Let’s say we want to modify this XML file by inserting a new element. After loading the document we can do something like this:

XElement component = doc.Descendants("component").Last(); 

component.Add(new XElement("component ", new XAttribute("Name", comp.Name), new XAttribute("Producer", comp.Description), 

new XAttribute("Price", comp.Price),  new XAttribute("Available", comp.Available.ToString())));

So, we used Descendats to navigate to the desired place in the document and then we added the new element. In a similar manner we could add the new element in the first position, right after the root, <.net_components>, by using the AddFirst method.

Another operation that we can think of is modifying an element’s value or its attributes. For example, we locate the first component node and change the availability status. Like this:

XElement component = logXML.Descendants("component").First(); 

component.SetAttributeValue("Available", "False");

Finally, if we want to delete an element, there are two ways we can accomplish this. Having the same element selected like in the previous code snippet, let’s call the Remove method:

component.Remove();

The other way implies the usage of the RemoveContent method. This method removes the content and leaves instead an empty element (<component />).

I hope you enjoyed this follow up post on LINQ to XML. There still are many to talk about LINQ to XML so there might be future posts to cover it.

 


Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

September 22. 2008 14:53

custom net development

Very nice post... thanks for your efforts...

custom net development

Add comment


(Will show your Gravatar icon)  

  Country flag