Use Visual C #. Net to specify the fully qualified element name (from msdn) in the XPath query)

Source: Internet
Author: User

The release number of this article was chs313188.
For the Microsoft Visual Basic. Net version in this article, see 308062.
Content of this task
• Overview

• Create an XML file
• Create a Visual C #. Net project
• Reference
Summary
This article describes how to specify the namespace Prefix: Fully Qualified element names in the element name format select nodes in the xmldocument object.

Back to Top
Create an XML file
1. on the Windows Start Menu, point to run, type notepad.exe, and click OK to open notepad. Copy the following Code And paste it into notepad:
<? XML version = '1. 0' encoding = 'utf-16'?>
<BK: Books xmlns: BK = 'HTTP: // myserver/myschemas/Book'>
<BK: Book>
<BK: title> just XML </BK: title>
</BK: Book>
<BK: Book>
<BK: title> professional XML </BK: title>
</BK: Book>
<BK: Book>
<BK: title> XML step by step </BK: title>
</BK: Book>
<BK: Book>
<BK: title> XML by example </BK: title>
</BK: Book>
</BK: Books> 2. copy and paste the following code into notepad: On the File menu, click Save.
3. Copy the following code and paste it into notepad: In the Save type text box of the Save As dialog box, type all files. In the file name text box, type books. xml and click OK.
Back to Top
Create a Visual C #. Net project
The following code example uses the following objects and classes:
• Xpathnavigator class: xpathnavigator is based on the XML Path Language (XPath) data model. This class provides the methods required to implement XPath queries for any data storage area.
• Xpathexpression class: This class encapsulates compiled XPath expressions and will return this class when compile is called. Select, evaluate, and matches methods use this class.
• Xmlnamespacemanager class: xmlnamespacemanager is used to parse the namespace, add a namespace to the set, and delete a namespace from the set. Xmlnamespacemanager also provides range management for these namespaces. Because books. XML uses the following "BK" namespace in the code, you must use xmlnamespacemanager.
• Xpathnodeiterator class: This object provides Iteration for a group of selected nodes Program .
To create and run a Visual Basic. Net project, follow these steps:
1. Create a new windows application project in Visual C #. net. By default, form1 is added to the project.
2. Place a button control and a Textbox Control on form1.
3. Set the multiline attribute of the Textbox Control to true.
4. Click to expand the Textbox Control to view the four to five rows of data.
5. Add the following code to the top of the "code" window:
Using system. xml;
Using system. xml. XPath; 6. to load the books. xml file to an xmldocument object, add the following code to the Click Event of the button object.
Xmldocument oxmldoc = new xmldocument ();
Oxmldoc. Load (@ "C: \ books. xml"); 7. Make sure that the books. XML Path in the previous code points to the correct path on the computer.
8. Create an xpathnavigator object using the createnavigator method of the xmldocument object to run the XPath query:
Xpathnavigator oxpathnav;
Oxpathnav = oxmldoc. createnavigator (); 9. Use the compile method of xpathnavigator to create the xpathexpression class, and then pass the XPath query as the parameter:
Xpathexpression expr;
Expr = oxpathnav. Compile ("// BK: Book [position ()> = 2]"); 10. Use the addnamespace method to add a "BK" namespace to the xmlnamespacemanager object:
Xmlnamespacemanager (oxpathnav. nametable );
Oxmlnsmanager. addnamespace ("BK", "http: // myserver/myschemas/books"); 11. Use the setcontext method of xpathexpression to set the xpathexpression context to xmlnamespacemanager:
Expr. setcontext (oxmlnsmanager); 12. To run the XPath query and return the selected node, pass the expression to the select method of xpathnodeiterator:
Xpathnodeiterator iterator = oxpathnav. Select (expr );
While (iterator. movenext ())
{
This. textbox1.text = This. textbox1.text + "\ r \ n" + iterator. Current. value;
} 13. The code in the button#click event should be shown 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 ");
Expr. setcontext (oxmlnsmanager );

Xpathnodeiterator iterator = oxpathnav. Select (expr );

While (iterator. movenext ())
{
This. textbox1.text = This. textbox1.text + "\ r \ n" + iterator. Current. value;
}

Oxmlnsmanager = NULL;
Oxpathnav = NULL;
Oxmldoc = NULL;
}
Catch (exception exc)
{
MessageBox. Show (EXC. Message );
} 14. Generate and run the project.
15. Click button1. Note that a list of books appears in the text box. These books are located at a position greater than or equal to 2.
Back to Top
Reference
For more information, clickArticleTo view the articles in the Microsoft Knowledge Base:
280457 PRB: specifying fully qualified element names in XPath queries (specify the fully qualified element name in the XPath query)

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/wukong777/archive/2004/10/09/129415.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.