301101 HOW TO: Load and use XML (from mkba) using XMLDocument classes

xiaoxiao2021-03-06  75

The release number of this article has been CHS301101

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 gain support for Beta versions, see the documentation included in the beta product file or view the site you downloaded.

This task content

summary

Require how to load and use XMLDocument reference

SUMMARY This article describes a class that implements the core and the second layer of the Layer 2 of the WWW Federation (W3C) document object model (DOM). The DOM is a representation of the tree of the Scalable Markup Language (XML) document (cache) tree, which can be used to navigate and edit documents.

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 load and use the XMLDocument class

Create a 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 an XML document and has a Load method that loads documents from files, streams, or 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"); 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). Use the XML node to operate the XMLNode object returned using the DocumentElement property in XMLDocument. The DisplayTree process is introduced in the next step. The XMLNode class provides the method and properties used by the operation node. Many other W3C classes are special examples of the XMLNode class. Visual Basic .NET Code DIM Node As XMLNode

Node = myxmldocument.documentelement

DisplayTree (Node) C # code XMLNode Node;

Node = myxmldocument.documentelement;

DisplayTree (Node); Newly built a process called DisplayTree, which uses the XMLNode class as a parameter. The Format process is described in next steps. The DisplayTree method uses the HaschildNodes and Firstchild properties to move downward along the tree to recursively. NextSibling property is moved to the next node next to the current node, if there is no node, it returns empty. Visual Basic .NET Code Public Sub DisplayTree (Node As Xmlnode) If ISNTHING (Node) Then Format (NODE)

If node.haschildnodes dam

Node = node.firstchild

While not isnothing (node)

DisplayTree (Node)

Node = node.nextsibling

End while

END IF

End Subc #code public static void displaytree (XMLNode Node)

{

IF (Node! = NULL)

Format (Node);

IF (node.haschildnodes)

{

Node = node.firstchild;

While (Node! = NULL)

{

DisplayTree (Node);

Node = node.nextsibling;

}

}

} Newly built a process called Format and use XMLNODE as a parameter. This process displays the contents of the document on the console. The Format method uses the Name and the Value property to display the details of the current node. For the XMLELEMENT node type, Attributes property provides a list of properties as an XMLNameDNodeMap class. This class contains the getNameDItem method and setNameDItem method. The former retrieves XMLNode specified by name, which uses its Name property to add XMLNode. The name attribute provides the name of the node, and the value attribute gives the value of the node based on the node type. Visual Basic .NET Code 'Format The Output.

Private Sub Format (Node as XMLNode)

If not node.haschildnodes the

Console.Writeline (Strings.chr (9) & node.name & "<" & node.value & ")

Else

Console.write (node.name)

IF xmlnodetype.ement = node.nodetype

DIM MAP As XMLNAMEDNODEMAP = Node.attributes

DIM Attrnode as Object

For Each AttrNode in Map

Console.write ("" & ctype (attrnode, xmlnode) .name & "<" & ctype (attrnode, xmlnode) .value & ">")

NEXT

END IF

Console.writeLine ()

END IF

End Subc #code private void format (XMLNode Node)

{

IF (! node.haschildnodes)

{

Console.writeline ("/ t" node.name "<" node.value ">")

Else

{

Console.write (node.name);

IF (xmlnodetype.element == node.nodetype)

{

XMLNameDNodeMap Map = Node.Attribute;

Foreach (XMLNode AttrNode In Map)

Console.write (" attrnode.name " < attrnode.value ">");

}

Console.writeLine ();

}

} Save and shut down the 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 floor 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

Recent Updated: 2001-11-2 (1.0) Keyword KBDsupport Kbhowto KbhowTomaster Kbxml KB301101 KBAUDDEVELOPER

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

New Post(0)