ASP. NET uses xpath to get the operation of a single (SelectSingleNode) or multiple (SelectNodes) xml nodes

Source: Internet
Author: User
Tags xml parser

The hypothetical xml file contains the following content ..

 

 

<? Xml version = "1.0" encoding = "UTF-8"?>
<A>
<B>
<C c1 = ""> JAPAN </C>
</B>
<B>
<C c1 = "Taipei"> TAIWAN </C>
<C c1 = "Tainan"> TAIWAN </C>
</B>
<B>
<C c1 = "Beijing"> CHINA </C>
</B>
</A>

How to retrieve the internal or external content at a specific xml vertex... there is a simple example below... using xpath...

Part of asp.net (c #) Programs

Using System;
Using System. Collections;
Using System. Configuration;
Using System. Data;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml;

Public partial class XPATH: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc. Load (Server. MapPath ("XML. xml"); // loads the xml Parser

String xPathExpression1 = "/A/B/C [. = 'Taiwan ']"; // find 'Taiwan' in the node'
XmlNodeList nodelist = xmldoc. SelectNodes (xPathExpression1); // multi node

String xPathExpression2 = "/A/B/C [@ c1 = 'taibei ']"; // find the node with 'taibei'
XmlNode node = xmldoc. SelectSingleNode (xPathExpression2); // single node

// Outputs the result: TAIWAN
Response. Write (node. OuterXml );

Response. Write ("<p> ");

// Result: Tainan, Taipei
Foreach (XmlNode item in nodelist)
{
Response. Write (item. Attributes ["c1"]. Value );
Response. Write ("<br/" + "> ");
}
}
}

   

 

The result of the rows:

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.