Microsoft Knowledge Base Email Alertz

KBAlertz.com: This sample illustrates how to update and save XML with the

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 301233 - Last Review: May 12, 2007 - Revision: 6.5

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

This article was previously published under Q301233
This article refers to the following .NET Framework Class Library namespace:
  • System.XML

On This Page

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)

How to Use the XmlDocument Class to Save XML

  1. Create a new Visual Basic or C# Console Application in Visual Studio .NET.
  2. Make sure that the project references the System.Xml namespace.
  3. 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;
    
  4. 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 ("books.xml")
    
    C# Code
    XmlDocument myXmlDocument = new XmlDocument();
    myXmlDocument.Load ("books.xml");
    
    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).
  5. 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;
    
  6. Iterate through the children of the document element, and find all the "price" 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 "price". 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 = "price" 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("#.00")
             Console.WriteLine("Old Price = " & node2.InnerText & Strings.Chr(9) & "New price = " & 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 == "price")
             {
                Decimal price = Decimal.Parse(node2.InnerText);
                // Increase all the book prices by 20%
                String newprice = ((Decimal)price*(new Decimal(1.20))).ToString("#.00");
                Console.WriteLine("Old Price = " + node2.InnerText + "\tNew price = " + newprice);
                node2.InnerText = newprice;
              }
    
  7. 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("InflatedPriceBooks.xml")
    
    C# Code
    myXmlDocument.Save("InflatedPriceBooks.xml");
    
  8. Build and run your project.

REFERENCES

For more information, visit the following Web sites:
XML in .NET: .NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation
http://msdn.microsoft.com/msdnmag/issues/01/01/xml/default.aspx (http://msdn.microsoft.com/msdnmag/issues/01/01/xml/default.aspx)

Document Object Model (Core) Level 1
http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html (http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html)

Document Object Model Core
http://www.w3.org/TR/DOM-Level-2-Core/core.html (http://www.w3.org/TR/DOM-Level-2-Core/core.html)

XmlDocument Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlDocumentClassTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlDocumentClassTopic.asp)

XmlNode Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlNodeClassTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlNodeClassTopic.asp)

XML Document Object Model (DOM)
http://msdn2.microsoft.com/en-us/library/hf9hbf87(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/hf9hbf87(vs.71).aspx)

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
Keywords: 
kbhowtomaster KB301233
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please