Update nodes and attributes in a Xml document

Posted by Techie Cocktail | 11:09 PM | , | 5 comments »

Introduction
Xml is widely used in application systems to store and receive data, feeds or for some configuration details. There may be times when you need to update some node or element value or update an attribute value. In this article how you can update node or attributes values in an Xml document.

Approach
To update the value of a node or an attribute, you need to first create a pointer or a reference to that node containing the element or attribute that requires an update using XPath query. And once you get the pointer to that node, you can access the node or the attribute and set its value to the new value.

Example
Lets take a sample Xml file and try to update one of the elements and attributes value using C#.

Xml file: books.xml
<?xml version="1.0"?>
<books>
<book name="asp.net">
<author>John</author>
<price>40</price>
</book>
</books>

Lets see how to update an element and an attribute value as in the below sections. In C# the XML Classes can be found in System.Xml namespace.

Updating an element's value
Lets say you need to update the price of the book named asp.net, as in the above Xml document, to a promotional rate from 40 to 35. The below example shows it.


static void Main(string[] args)
{
string fileName = @"d:\books.xml";

//read the Xml file
XmlTextReader _xmlTextReader = new XmlTextReader(fileName);

//load the Xml file into the XmlDocument object
XmlDocument _xmlDocument = new XmlDocument();
_xmlDocument.Load(_xmlTextReader);

//Note: Close the reader object to release the xml file. Else while saving you will get an error that it is
//being used by another process.
_xmlTextReader.Close();

//update the element (price of the book - asp.net)
updateElement(fileName, _xmlDocument);
}

static void updateElement(string fileName, XmlDocument xmlDoc)
{
//get the root element of the document
XmlElement rootElement = xmlDoc.DocumentElement;

//create a pointer/reference to the price node for the book named 'asp.net'
XmlNode priceNode = rootElement.SelectSingleNode("/books/book[@name='asp.net']/price");
priceNode.InnerText = "35";

//save the Xml
xmlDoc.Save(fileName);
}

In the above example, as in the UpdateElement() method, first the pointer is created to the price node of the book named 'asp.net'. Using that pointer its value is updated by accessing its InnerText (get/set) property.

Updating an attribute's value
Lets say the book name has been changed from 'asp.net' to 'Asp.Net Made Easy'. Lets see how to update the name attribute to the new name in the above Xml.

static void Main(string[] args)
{
string fileName = @"d:\books.xml";

//read the Xml file
XmlTextReader _xmlTextReader = new XmlTextReader(fileName);

//load the Xml file into the XmlDocument object
XmlDocument _xmlDocument = new XmlDocument();
_xmlDocument.Load(_xmlTextReader);

//Note: Close the reader object to release the xml file. Else while saving you will get an error that it is
//being used by another process.
_xmlTextReader.Close();

//update the element (book name)
updateAttribute(fileName, _xmlDocument);
}

static void updateAttribute(string fileName, XmlDocument xmlDoc)
{
//get the root element of the document
XmlElement rootElement = xmlDoc.DocumentElement;

//Update the name attribute's value
XmlNode bookNode = rootElement.SelectSingleNode("/books/book[@name='asp.net']");
if (bookNode.Attributes["name"].Value.Equals("asp.net"))
{
bookNode.Attributes["name"].Value = "Asp.Net Made Easy";
}

//save the Xml
xmlDoc.Save(fileName);
}

5 comments

  1. Unknown // March 20, 2013 at 2:38 AM  
  2. Investor Mart // June 28, 2013 at 11:03 PM  
  3. Unknown // April 16, 2014 at 10:43 PM  
    This comment has been removed by the author.
  4. Anonymous // December 12, 2014 at 4:09 AM  

    Thanks Useful to me

  5. Unknown // December 28, 2015 at 4:11 AM  

    hello , it is so helpful.thanks but please there is an example in java?