C#读取XML的CDATA节点内容实例详解
程序员文章站
2022-06-21 20:28:28
首先看实例代码:
昨天读取了以下xml, 之前没有读取过类似的格式,用一种比较笨的方法读取的,记录下
首先看实例代码:
昨天读取了以下xml, 之前没有读取过类似的格式,用一种比较笨的方法读取的,记录下 <?xml version="1.0"?> <response> <srvver>1.0</srvver> <srvcode>000</srvcode> <payload> <param type="xml" key="data"> <![cdata[ <response> <execution> <status code="0" sql_code="0" description="执行成功!"/> </execution> <responsecontent> <parameter/> <document> <recordset id="1"> <master name="m" node_id="1"> <record> <field name="mtrl_id" value="51722500h031"/> <field name="is_main" value="y"/> <field name="main_id" value="51722500h031"/> <field name="dosage" value="1.0"/> <field name="nn_qty" value="1000.0"/> <field name="tn_qty" value="1000.0"/> <field name="seq" value="70.0"/> <field name="point_str" value=""/> </record> </master> </recordset> <recordset id="2"> <master name="m" node_id="2"> <record> <field name="mtrl_id" value="51820590h001"/> <field name="is_main" value="n"/> <field name="main_id" value="51820590h001"/> <field name="dosage" value="1.0"/> <field name="nn_qty" value="1000.0"/> <field name="tn_qty" value="500.0"/> <field name="seq" value="30.0"/> <field name="point_str" value=""/> </record> </master> </recordset> <recordset id="3"> <master name="m" node_id="3"> <record> <field name="mtrl_id" value="51820710h002"/> <field name="is_main" value="n"/> <field name="main_id" value="51820510h002"/> <field name="dosage" value="0.0"/> <field name="nn_qty" value="0.0"/> <field name="tn_qty" value="1000.0"/> <field name="seq" value="20.0"/> <field name="point_str" value=""/> </record> </master> </recordset> </document> </responsecontent> </response> ]]> </param> </payload> </response>
以下是读取xml节点的过程
我的目的是想要读取cdata里面的 <recordset></recordset> 包裹的数据
1.我先建立了一个类
public class readxml { public string recordset; public string mtrl_id; public string is_main; public string main_id; public string dosage; public string nn_qty; public string tn_qty; public string seq; public string point_str; }
2. 接着我把cdata里面的内容提出来
xmldocument doc = new xmldocument(); doc.loadxml(strfile); var cdata = (xmlcdatasection)doc.selectsinglenode("/response/payload/param/text()");
3. 然后用xelement的linq方法找到 recordset 节点集合, 循环节点集合,赋值,以下是实现代码:
xelement xe = xelement.parse(cdata.innertext); ienumerable<xelement> elements = from ele in xe.elements("responsecontent").elements("document").elements("recordset") select ele; list<readxml> xmlnodelist = new list<readxml>(); foreach (var ele in elements) { readxml xmlnode= new readxml(); model.recordset = ele.attribute("id").value; var subeles = ele.elements("master").elements("record").elements("field"); foreach (var item in subeles) { if (item.attribute("name").value == "mtrl_id") { xmlnode.mtrl_id = item.attribute("name").nextattribute.value; } if (item.attribute("name").value == "is_main") { xmlnode.is_main = item.attribute("name").nextattribute.value; } if (item.attribute("name").value == "main_id") { xmlnode.main_id = item.attribute("name").nextattribute.value; } if (item.attribute("name").value == "dosage") { xmlnode.dosage = item.attribute("name").nextattribute.value; } if (item.attribute("name").value == "nn_qty") { xmlnode.nn_qty = item.attribute("name").nextattribute.value; } if (item.attribute("name").value == "tn_qty") { xmlnode.tn_qty = item.attribute("name").nextattribute.value; } if (item.attribute("name").value == "seq") { xmlnode.seq = item.attribute("name").nextattribute.value; } if (item.attribute("name").value == "point_str") { xmlnode.point_str=item.attribute("name").nextattribute.value; } } xmlnodelist.add(xmlnode); }
以上就是本次介绍的全部知识点内容,感谢大家的阅读和对的支持。