Microsoft KB Archive/301233

= How To Modify and Save XML with the XmlDocument Class in the .NET Framework SDK =

Article ID: 301233

Article Last Modified on 5/12/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework Class Libraries 1.0
 * Microsoft .NET Framework Class Libraries 1.1

-



This article was previously published under Q301233



This article refers to the following .NET Framework Class Library namespace:
 * System.XML



IN THIS TASK
SUMMARY
 * Requirements
 * How to Use the XmlDocument Class to Save XML

REFERENCES



SUMMARY
This sample illustrates how to update and save XML with the XmlDocument class.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:
 * Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * XML terminology
 * Creating and reading an XML file
 * The Document Object Model (DOM)

back to the top

How to Use the XmlDocument Class to Save XML
 Create a new Visual Basic or C# Console Application in Visual Studio .NET. Make sure that the project references the System.Xml namespace.  Use the Imports statement on the Xml namespace so that you are not required to qualify XmlTextReader declarations later in your code. You must use the Imports statement prior to any other declarations.

Visual Basic .NET Code Imports System.Xml C# Code using System.Xml;   Create a new XmlDocument class, and use the Load method to load it.

The XmlDocument class represents the XML document and has a Load method to load the document from a file, stream, or an XmlReader.

Visual Basic .NET Code Dim myXmlDocument as XmlDocument = new XmlDocument myXmlDocument.Load (&quot;books.xml&quot;) C# Code XmlDocument myXmlDocument = new XmlDocument; myXmlDocument.Load (&quot;books.xml&quot;); Note that, although the Books.xml file is used here, you can create your own Books.xml file. A sample Books.xml file is also included with Visual Studio .NET and .NET Framework Software Development Kit (SDK).   The XmlNode object provides methods and properties to manipulate a node. Use the XmlNode object that the DocumentElement property of the XmlDocument returns to manipulate an XML node.

Visual Basic .NET Code Dim node as XmlNode node = myXmlDocument.DocumentElement C# Code XmlNode node; node = myXmlDocument.DocumentElement;   Iterate through the children of the document element, and find all the &quot;price&quot; nodes. Use the For Each looping construct with the ChildNodes property of the Node object to find all nodes that have a Name property that is equal to &quot;price&quot;. Double the price of the book.

Visual Basic .NET Code Dim node2 As XmlNode 'Used for internal loop. Dim nodePriceText As XmlNode For Each node In node.ChildNodes 'Find the price child node. For Each node2 In node.ChildNodes If node2.Name = &quot;price&quot; Then '                   nodePriceText = node2.InnerText Dim price As Decimal price = System.Decimal.Parse(node2.InnerText)

' Double the price. Dim newprice As String newprice = CType(price * 2, Decimal).ToString(&quot;#.00&quot;) Console.WriteLine(&quot;Old Price = &quot; & node2.InnerText & Strings.Chr(9) & &quot;New price = &quot; & newprice) node2.InnerText = newprice End If  Next Next C# Code foreach(XmlNode node1 in node.ChildNodes) foreach (XmlNode node2 in node1.ChildNodes) if (node2.Name == &quot;price&quot;) {           Decimal price = Decimal.Parse(node2.InnerText); // Increase all the book prices by 20% String newprice = ((Decimal)price*(new Decimal(1.20))).ToString(&quot;#.00&quot;); Console.WriteLine(&quot;Old Price = &quot; + node2.InnerText + &quot;\tNew price = &quot; + newprice); node2.InnerText = newprice; } </li>  Use the Save method of the XmlDocument class to save the altered XML to a new file that is named InflatedPriceBooks.xml.

You can use the Save method to save XML data to files, streams, and XmlWriters.

Visual Basic .NET Code myXmlDocument.Save(&quot;InflatedPriceBooks.xml&quot;) C# Code myXmlDocument.Save(&quot;InflatedPriceBooks.xml&quot;); </li> Build and run your project.</li></ol>

back to the top

<div class="references_section">