XML namespace-xmlnamespace manager and nametable

Source: Internet
Author: User
How to Use xmlnamespacemanager and nametable

This example shows how to select a namespace-specific XML data from xmldocument. It illustrates the usage of the xmlnamespacemanager class. This class encapsulates a namespace set and provides various namespace management tasks, such as parsing a namespace, adding a namespace to the set, or removing a namespace from the set, and provides range management for namespaces.

Xmlnametable provides the management of string names in XML streams. It is managed by creating an atomic String object table. This table provides an efficient method for the XML analyzer to use the same string object for all repeated elements and attribute names in the XML document.

This example uses an XML Path Language (XPath) expression to query namespaces. For more information about XPath expressions, see how to use XPath expressions to query XML

 

VB xmlnamespace. aspx

[Running example] | [View Source code]

First, this example creates an xmldocument and loads the orders. xml file to the xmldocument.


// Create a new xmldocument for the specified source file and load it. xmldocument myxmldocument = new xmldocument (); myxmldocument. Load ("orders. xml ");
'Create a new xmldocument for the specified source file and load it. Dim myxmldocument as xmldocument = new xmldocument () myxmldocument. Load ("orders. xml ")
C # VB  

To obtain all the item elements in the yourns1 namespace, this example uses the following XPath expression: // yourns1: item. This example also creates an xmlnamespacemanager that contains the prefix and namespace in the XML document.

The orders. XML document defines two prefixes: myns for the http://tempuri.org/myUSorderprocessornamespace and yourns1 for the http://tempuri.org/USvendor1namespace. This document also has the default namespace (empty prefix) for the http://tempuri.org/myUSordersnamespace ).

 
// Create a string containing the XPath expression to evaluate. sexpr = string. format ("// {0}: item", "yourns1"); // create an xmlnamespacemanager and add the namespaces for the document. xmlnamespacemanager nsmanager = new xmlnamespacemanager (Doc. nametable); // set default namespace -- first Param is null. nsmanager. addnamespace (string. empty, "http://tempuri.org/myUSordersnamespace"); nsmanager. addnamespace ("myns", "http://tempuri.org/myUSorderprocessornamespace"); nsmanager. addnamespace ("yourns1", "http://tempuri.org/USvendor1namespace"); nsmanager. addnamespace ("yourns2." http://tempuri.org/USvendor2namespace ");
'Create a string containing the XPath expression to evaluate. sexpr = string. format ("// {0}: item", "yourns1") 'create an xmlnamespacemanager and add the namespaces for the document. dim nsmanager as xmlnamespacemanager = new xmlnamespacemanager (Doc. nametable) 'set default namespace -- first Param is null. nsmanager. addnamespace (string. empty, "http: 'tempuri. org/myusordersnamespace ") nsmanager. addnamespace ("myns", "http: 'tempuri. org/myusorderprocessornamespace ") nsmanager. addnamespace ("yourns1", "http: 'tempuri. org/usvendor1namespace ") nsmanager. addnamespace ("yourns2." http: 'tempuri. org/usvendor2namespace ")
C # VB  

Then, this example calls the selectnodes Method on xmldocument. In this case, the selectnodes method finds all the item nodes with the yourns1 prefix. The prefix is resolved to the namespace http://tempuri.org/usvendor1namespace.

The selectnodes method returns an xmlnodelist. The node list is displayed in this example.

Xmlnodelist nodelist = myxmldocument. selectnodes (exprstring, nsmanager); foreach (xmlnode myxmlnode in nodelist) {displaytree (myxmlnode );}
Dim nodelist as xmlnodelist = myxmldocument. selectnodes (exprstring, nsmanager) dim myxmlnode as xmlnode for each myxmlnode in nodelist displaytree (myxmlnode) Next
C # VB  
Summary
    1. the xmlnamespacemanager class parses the namespace, adds a namespace to the set, and removes the namespace from the set.
    2. the Program can use xmlnametable to improve the performance of the XML analyzer.

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.