xml内容如下:
<promotion_coupons_get_response>
<tot_results>
200
</tot_results>
<coupons list="true">
<coupon>
<coupon_id>
123456
</coupon_id>
<denominations>
5
</denominations>
<creat_time>
2000-01-01 00:00:00
</creat_time>
<end_time>
2000-01-01 00:00:00
</end_time>
<condition>
500
</condition>
</coupon>
</coupons>
</promotion_coupons_get_response>
加载这个xml文件:
XMLDocument xmlDoc = new XmlDocument();
然后可以通过xmlDoc .Load(…)或xmlDoc.LoadXml(…)方法加载XML文档。
加载完这个xml文档后,我们可以通过下面的方法快速读取节点内的内容。
快速获取xml节点内容的方法为:
1 /// <summary>
2 /// 获取XML结点值
3 /// </summary>
4 /// <param name="Str">xml,如:XmlNodeList[0].InnerXml</param>
5 /// <param name="xPath">结点,如:time这个结点</param>
6 /// <returns>值</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 }
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 }
可以把获取节点值的方法放在共通类里面
手机扫一扫
移动阅读更方便
你可能感兴趣的文章