Quick reading of XML node content

Source: Internet
Author: User
The XML content is as follows: view code

 1 <promotion_coupons_get_response>
2 <tot_results>
3 200
4 </tot_results>
5 <coupons list="true">
6 <coupon>
7 <coupon_id>
8 123456
9 </coupon_id>
10 <denominations>
11 5
12 </denominations>
13 <creat_time>
14 2000-01-01 00:00:00
15 </creat_time>
16 <end_time>
17 2000-01-01 00:00:00
18 </end_time>
19 <condition>
20 500
21 </condition>
22 </coupon>
23 </coupons>
24 </promotion_coupons_get_response>

Load the XML file:

Xmldocument xmldoc = new xmldocument ();

Then, you can use xmldoc. Load (...) or xmldoc. loadxml (...) to load the XML document.

After loading this XML document, we can quickly read the content in the node using the following method.

You can quickly obtain the content of an XML node by using the following methods:

View code

1 /// <summary>
2 // get the XML node Value
3 /// </Summary>
4 // <Param name = "str"> XML, for example, xmlnodelist [0]. innerxml </param>
5 // <Param name = "XPath"> node, for example, time </param>
6 /// <returns> value </returns>
7 public static string get_str_nodes (string STR, string XPath)
8 {
9 int x = 0, y = 0, Z = 0;
10 x = Str. indexof ("<" + XPath + "> ");
11 y = Str. indexof ("</" + XPath + "> ");
12 z = XPath. Length + 2;
13 if (Y> X)
14 {
15 return Str. substring (x + Z, Y-X-z). Trim ();
16}
17 else
18 {
19 Return "";
20}
21}

The above method is not explained. The specific application example is as follows:

View code

 1 XmlNodeList couponNodes = CouponXml.SelectNodes("//coupon"); 
2
3 if (couponNodes != null)
4 {
5 foreach(XmlNode couponNode in couponNodes)
6 {
7 string coupon_Id = get_Str_Nodes(couponNode.InnerXml, "coupon_Id");
8 string denominations = get_Str_Nodes(couponNode.InnerXml, "denominations");
9 }
10 }

You can put the method to get the node value in the common class, and it has been tested.

----------------------------------------------------------------

 

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.