了解我?
Java语言
后台开发4年+
坐标北京
CSDN博客专家
耕耘6年+
喜欢运动~热爱生活
联系我?
CSDN博客主页:
联系方式:
WeChat:che8520
Email:chelongliang0428@163.com
背景:
xml内容如下:
/**
*
* @ClassName
* @Description
* @author
* @version 1.0
* @since 12月 8, 2021
*/
<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 }
ps:可以把获取节点值的方法放在共通类里面
参考文章