313188 How to: Specify a fully qualified element name (from MSDN) in the XPath query using Visual C # .NET

xiaoxiao2021-03-06  74

The release number of this article has been CHS313188

For Microsoft Visual Basic .NET versions of this article, see

308062.

This task content

summary

Create an XML file to create a Visual C # .NET project reference

Summary This article describes how to pass the designated

Named Space Prefix: The fully qualified element name in the element name format is

Select nodes in the XMLDocument object.

Back to top

Create an XML file

On the Windows Start menu, point to Notepad.exe, type notepad.exe, and then click OK to open Notepad. Copy the following code and paste it into Notepad:

JUST XML

Professional XML

Xml Step by Step

XML by Example

Copy the following code and paste it into Notepad: On the File menu, click Save. Copy the code below and paste it into Notepad: In Save As Save Type Text box, type all files. In the File Name text box, type Books.xml, and then click OK.

Back to top

Create a Visual C # .NET project The following code examples use the following objects and classes:

Xpathnavigator class: XPathnavigator Based on the XML Path Language (XPATH) data model, this class provides the method you need to implement XPath queries for any data storage area. XPATHEXPRESSION Class: This class package has been compiled XPath expressions, which will return this class when calling Compile. SELECT, EVALUATE and MATCHES methods use this class. XMLNameSpaceManager class: XMLNameSpaceManager is used to parse namespaces, add namespaces to a collection, and delete namespaces from a collection. XMLNameSpaceManager also provides range management for these namespaces. Because Books.xml uses the "BK" namespace below in your code, you must use XMLNameSpaceManager. XPathNodeTeiterator class: This object provides an iterative program for a set of selected nodes. To create and run the Visual Basic .NET project, follow these steps:

Create a Windows application project in Visual C # .NET. The Form1 is added to the project by default. Place a Button control and a TextBox control on Form1. Set the multiline property of the TextBox control to True. Click to expand the TextBox control to view four to five lines of data. Add the following code to the Code window: use system.xml;

Using system.xml.xpath; To load the Books.xml file file into the XMLDocument object, add the following code to the Click event of the Button object. XmlDocument oxmldoc = new xmldocument (); oxmldoc.load (@ "c: /books.xml"); make sure the Books.xml path in the front code points to the correct path on your computer. Create an XPathnavigator object using the CreateNavigator method for the XMLDocument object to run XPath query: xpathnavigator oxpathnav;

OXPathnav = oxmldoc.createnavigator (); Create an XPATHEXPIRESSION class using XPathnavigator, then delivering XPath query as parameters: xpatHexpression Expr;

EXPR = OXPathnav.compile ("// BK: Book [position ()> = 2]"); add "BK" namespace to the XMLNameSpaceManager object using the AddNameSpace method: XMLNameSpaceManager (OXPathnav.nameTable);

oxmlNSManager.AddNamespace ( "bk", "http: // myserver / myschemas / Books"); the use of XPathExpression SetContext method XPathExpression context to XmlNamespaceManager: Expr.SetContext (oxmlNSManager); To run the XPath query and return the selected The node, pass the expression to the SelectEterator of XPathNodeEiterator: XpathnodeEiterator Iterator = OXPathnav.select (expr);

While (item.movenext ())

{

This.TextBox1.text = this.TextBox1.text "/ r / n" item.current.value;

} The code in the Button1_Click event should be displayed as follows: xmldocument oxmldoc = new xmldocument ();

Try

{

OXMLDoc.Load ("C: //Books.xml");

Xpathnavigator oxpathnav;

OXPathnav = oxmldoc.createnavigator ();

XPATHEXPRESSION EXPR;

EXPR = OXPATHNAV.Compile ("// bk: Book"; Position ()> = 2] ");

XMLNameSpaceManager oxmlnsmanager = new xmlnamespacemanager (OXPATHNAV.NAMETABLE);

OxmlnsManager.adDNamespace ("BK", "http:// myserver / myschemas / books");

Exp.setContext (OXMLnsManager);

XpathnodeTeiterator Iterator = OXPathnav.select (expr);

While (item.movenext ())

{

This.TextBox1.text = this.textbox1.text "/ r / n" item.current.value;}

OXMLNSManager = NULL;

OXPathnav = NULL;

oxmldoc = null;

}

Catch (Exception EXC)

{

Messagebox.show (Exc.Message);

} Generate and run the project. Click Button1. Note that a book list will appear in the text box, which is greater than or equal to 2.

Back to top

Refer to additional information, click the article number below to see the article in the Microsoft Knowledge Base:

280457 PRB: SPECIFYING FULLY QUALIFIED Element Names in xpath queries (specified in the XPath query)

Back to top

The information in this article applies to:

Microsoft Visual C # .NET (2002)

Recent Updated: 2002-2-24 (1.0) Keyword Kbhowto KbhowTomaster KBMSXML KB313188

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

New Post(0)