301233 How TO: Modify and save XML (from mkba) with XMLDocument class

xiaoxiao2021-03-06  72

The release number of this article has been CHS301233

This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.

For this Beta product, Microsoft does not provide formal product support. For information on getting beta support, see the documentation included in the beta product file, or view the site you download this version.

This task content

Summary

Require how to save XML reference using the XMLDocument class

Summary This example demonstrates how to use

XMLDocument class updates and saves XML.

Back to top

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)

Back to top

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 of the Node object. 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"); please click on the following article number to view Microsoft The corresponding article in the Knowledge Base: 301101 How to: Work with Use Xmldocument Class To Load and use XML (How to: Load and use XMLDocument class and use XML) to generate and run your project.

Back to top

Refer to more information, please visit the following Web site:

XML: .NET Framework XML class and C # provide simple, scalable data operation http://msdn.microsoft.com/msdnmag/issues/01/01/xml/xml.asp document object model (core ) The first layer 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 XmlDocument class http://msdn.microsoft.com/library/dotnet/cpref/frlrfsystemxmlxmldocumentclasstopic.htm XmlNode class http://msdn.microsoft.com/library/dotnet/cpref/frlrfsystemxmlxmlnodeclasstopic.htm XML Document Object Model (DOM) http://msdn.microsoft.com/library/dotnet/cpguidnf/cpconxmldocumentObjectModelDom.htm Back to Top

The information in this article applies to:

Microsoft .Net Developments Platform (NDP) Beta 2

Reserved: 2001-11-2 (1.0) Keyword Kbhowto KbhowTomaster Kbxml Tslic_tslic KB301233 KBAUDDEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-108972.html

New Post(0)