How to Use xmldocument (W3C DOM) to save XML
This example illustrates how to use the xmldocument class to update and save XML, which is an implementation of the Object Model (DOM) of the WWW Federation (W3C) documentation.
This example shows how to load and use the xmldocument topic.
|
[Running example] | [View Source Code] |
The Save method of the xmldocument class enables xmldocument to save XML data to files, streams, and xmlwriter.
The sample application (as shown in the following code) browses in the file books. XML to increase the price of all books by 2%. The application then saves the XML to a new file named updatebooks. xml.
public void Run(String[] args) { try { // Create XmlDocument and load the XML from file XmlDocument myXmlDocument = new XmlDocument(); myXmlDocument.Load (new XmlTextReader (args[0])); Console.WriteLine ("XmlDocument loaded with XML data successfully ..."); Console.WriteLine(); IncreasePrice(myXmlDocument.DocumentElement); // Write out data as XML myXmlDocument.Save(args[1]); Console.WriteLine(); Console.WriteLine("Updated prices saved in file {0}", args[1]); } catch (Exception e) { Console.WriteLine ("Exception: {0}", e.ToString()); } } public sub Run(args As String()) try ' Create an XmlDocument Dim myXmlDocument as XmlDocument = new XmlDocument() myXmlDocument.Load (args(0)) Console.WriteLine ("XmlDocument loaded with XML data successfully ...") Console.WriteLine() IncreasePrice(myXmlDocument.DocumentElement) ' Write out data as XML myXmlDocument.Save(args(1)) Console.WriteLine() Console.WriteLine("Updated prices saved in file {0}", args(1)) catch e as Exception Console.WriteLine ("Exception: {0}", e.ToString()) end try end sub |
C # |
VB |
|
The increaseprice method uses the firstchild method of the xmlnode class to recursively repeat the XML document to move down the tree. If no subnode exists, this method returns a null value. The nextsibling attribute is moved to the node next to the current node. If no node is moved, a null value is returned. When an application finds a node named Price, It updates the price. The following code shows how the increaseprice method works.
public void IncreasePrice(XmlNode node) { if (node.Name == "price") { node = node.FirstChild; Decimal price = Decimal.Parse(node.Value); // Increase all the book prices by 2% String newprice = ((Decimal)price*(new Decimal(1.02))).ToString("#.00"); Console.WriteLine("Old Price = " + node.Value + "\tNew price = " + newprice); node.Value = newprice; } node = node.FirstChild; while (node != null) { IncreasePrice(node); node = node.NextSibling; } } public sub IncreasePrice(node as XmlNode) if (node.Name = "price") node = node.FirstChild Dim price as Decimal price = System.Decimal.Parse(node.Value) ' Increase all the book prices by 2% Dim newprice as String newprice = CType(price*(new Decimal(1.02)), Decimal).ToString("#.00") Console.WriteLine("Old Price = " & node.Value & Strings.chr(9) & "New price = " & newprice) node.Value = newprice end if node = node.FirstChild While Not node Is Nothing IncreasePrice(node) node = node.NextSibling end while end sub |
C # |
VB |
|
XmlDocument loaded with XML data successfully ...Old Price = 8.99 New price = 9.17Old Price = 11.99 New price = 12.23Old Price = 9.99 New price = 10.19Updated prices saved in file updatebooks.xml
Summary
- The xmldocument class can save XML to a file, stream, or xmlwriter.
- The xmlnode class allows you to browse and modify nodes in the XML document.