C#读取XML的CDATA节点内容实例详解
人气:0首先看实例代码:
昨天读取了以下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); }
以上就是本次介绍的全部知识点内容,感谢大家的阅读和对的支持。
您可能感兴趣的文章:
加载全部内容