XML Document Object Model-save XML and xmldocument (W3C DOM) together

Source: Internet
Author: User
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.

 

VB savexmldocument. aspx

[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
  1. The xmldocument class can save XML to a file, stream, or xmlwriter.
  2. The xmlnode class allows you to browse and modify nodes in the XML document.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.