XPath usages in the selectSingleNode method in C # operations XML

Source: Internet
Author: User

      1. Common XML data types are: Element, Attribute,comment, Text.

Element, which refers to a node like <Name>Tom<Name>. It can include: Element, Text, Comment, ProcessingInstruction, CDATA, and EntityReference.

Attribute, refers to the bold part in <employee >.

Comment, a node that is shaped like:<!--my Comment.

Text, refers to the bold part of <Name>Tom<Name>.

In XML, you can use the XmlNode object to refer to various XML data types.

2.1 Querying the node (set) of the known absolute path

objNodeList = Objdoc.selectnodes ("Company/department/employees/employee")//or    


The above two methods can return a NodeList object, if you want to return a single node can use the selectSingleNode method, if the query to one or more nodes, the first node is returned, if no query of any node returns nothing. For example:

Objnodeobjnode = Objnode.selectsinglenode ("/company/department/employees/employee")    If (! ( Objnode) {   //do process}


2.2 Querying a known relative path node (set)

You can query XML data in a way that is similar to a relative path to a file path

Objnode = Objdoc.selectsinglenode ("company/department")    objnodeobjnodelist = Objnode.selectnodes (".. /department)    

2.3 Querying nodes (sets) for known element names

When using an irregular hierarchical document, you can use the//symbol to cross the middle node, querying its child, grandchild, or all other elements in the hierarchy, because you do not know the element name at the middle level. For example:

objNodeList = Objdoc.selectnodes ("Company//employee")

2.4 Query Properties (attribute) node

Each of the above methods returns the element node (set), which returns the attribute (attribute), only with the appropriate method, with an @ symbol in front of the property name, for example:

objNodeList = Objdoc.selectnodes (")

objNodeList = Objdoc.selectnodes (")

2.5 Querying the text node

Use text () to get the text node.

Objnode = Objdoc.selectsinglenode ("Company/department/deparmt_name/text ()")

2.6 Querying a node for a specific condition

Use the [] symbol to query a node for a specific condition. For example:

A. Returning an employee node with an ID number of 10102

Objnode = Objdoc.selectsinglenode ("company/department/employees/employee[@id = ' 10102 ']")

B. Returns the name node named Zhang Qi

Objnode = Objdoc.selectsinglenode ("company/department/employees/employee/name[text () = ' Zhang Qi ']")

C. Return department name node with staff 22345

Objnode = Objdoc.selectsinglenode ("company/department[employees/employee/@id = ' 22345 ']/department_name")

2.7 Querying multiple-mode nodes

Use | The symbol can get multiple patterns of nodes. For example:

objNodeList = Objdoc.selectnodes ("company/department/department_name | Company/department/manager ")

2.8 Querying any child nodes

Use the * symbol to return all child nodes of the current node.

objNodeList = Objdoc.selectnodes ("Company/*/manager")

Or

Objnodeobjnodelist = Objnode.childnodes



3 Editing of XML data

3.1 Adding an element's attribute (attribute) node

Dim objnodeattr as XmlNode    objnodeattr = objdoc.createattribute ("id", nothing)    objnodeattr.innerxml = "101"    ObjNode.Attributes.Append (objnodeattr)


3.2 Deleting an element's properties

ObjNode.Attributes.Remove (objnodeattr)

3.3 Adding a child element

Dim Objnodechild as XmlNode    objnodechild = objdoc.createelement (Nothing, "ID", nothing)    Objnodechild.innerxml = "101"    

3.4 Deleting a child element

Objnode.removechild (Objnodechild)

3.5 replacing a child element

Objnode.replacechild (Newchild,oldchild)

4 The maximum value in the query node.

The XML file is as follows:

<?xml version= "1.0" encoding= "Utf-8"?><documentelement>  <XmlTable>    <id>1</id >    <idx/>    <runtimes/>    <span/>    <startkey/>    <endkey/>    <filepath/>  </XmlTable>  <XmlTable>    <ID>2</ID>    <idx/>    <runtimes/>    <span/>    <startkey/>    <endkey/>    <filepath/>  </XmlTable></DocumentElement>

The following code can be used to remove the maximum ID:

XmlDocument XML = new XmlDocument ();                Imports the specified XML file                XML. Load (Xmlfilepath);                String XPath = "//id[not (text () <//id/text ())]";                String Idmax = XML. selectSingleNode (XPath). Childnodes[0]. InnerText;

XPath usages in the selectSingleNode method in C # operations XML

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.