Modify and save XML with XMLDocument class
summary
This example demonstrates how to use
XMLDocument class updates and saves XML.
The following table summarizes the recommended hardware, software, network architecture, and service pack required:
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 Create and Read XML File Document Object Model (DOM)
How to save XML using the XMLDocument class
Create a new Visual Basic or C # console application in Visual Studio .NET. Make sure the item references the System.xml namespace. Use the Imports statement on the XML namespace, so that you don't need to limit the XMLTextReader declaration in your code. The imports statement must be before any other declaration. Visual Basic .NET Code Imports System.xmlc # Code Using System.xml; New XMLDocument class, then load it with the LOAD method. The XMLDocument class represents the XML document and loads documentation from files, streams, or XMLReader using the LOAD method. Visual Basic .NET Code DIM MyXMLDocument As XmLDocument = New XmLDocument ()
MYXMLDocument.Load ("Books.xml")) C # code XmLDocument myXmldocument = new xmldocument ();
MyXMLDocument.Load ("Books.xml"); Please note that although the books.xml file is used here, you can create your own books.xml file. The books.xml sample file is also included in the Visual Studio .NET and .NET Framework Software Development Kit (SDK). XMLNode objects provide methods and properties for operating nodes. Use the XMLNode object returned using the XMLDocument's DocumentElement property to operate the XML node. Visual Basic .NET Code DIM Node As XMLNode
Node = myxmldocument.documentelementc # code XMLNode Node;
Node = myXmldocument.documentElement; iterative document element's child elements and find all "Price" nodes. Use the for Each loop structure to find all NAME properties equal to the node of the "Price" using the ChildNodes property. Double the price of books. Visual Basic .NET code DIM node2 as xmlnode 'used for internal loop.
Dim NodePricext As XMLNode
For Each Node in Node.childNodes
'Find The Price Child Node.
For each node2 in node.childnodes
If node2.name = "price" THEN
'nodeprictext = 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
Nextc # code foreach (xmlnode node1 in node.childnodes)
Foreach (xmlnode node2 in node1.childnodes)
IF (node2.name == "price")
{
Decimal price = decimal.parse (node2.innertext);
// increable 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;
} The Save method using the XMLDocument class will save the modified XML to a new file named InflatedPriceBooks.xml. You can save XML data to files, streams, and xmlwriters using the Save method. Visual Basic .NET code MYXMLDocument.save ("InflatedPriceBooks.xml")) C # code MYXMLDocument.save ("InflatedPriceBooks.xml); Build and run your project