苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3995|回复: 9

[咨询站长] XML文件解析

[复制链接]
发表于 2016-8-9 16:04:01 | 显示全部楼层 |阅读模式
<Response>
<QryReportListResult>
  <s:schema id="DefaultDataSet" xmlns="" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
   <s:element name="DefaultDataSet" msdata:IsDataSet="true">
    <s:complexType>
     <s:choice maxOccurs="unbounded">
      <s:element name="QryReportList">
       <s:complexType>
        <s:sequence>
         <s:element name="VisitNumberDR" type="s:string" minOccurs="0">1</s:element>
         <s:element name="Status" type="s:string" minOccurs="0">1</s:element>
         <s:element name="StatusDesc" type="s:string" minOccurs="0">1</s:element>
         <s:element name="EpisodeNo" type="s:string" minOccurs="0">1</s:element>
         <s:element name="Labno" type="s:string" minOccurs="0">1</s:element>
         <s:element name="RegNo" type="s:string" minOccurs="0">1</s:element>
         <s:element name="RecordNo" type="s:string" minOccurs="0">1</s:element>
         <s:element name="PatName" type="s:string" minOccurs="0">1</s:element>
         <s:element name="SurName" type="s:string" minOccurs="0">1</s:element>
         <s:element name="PatSex" type="s:string" minOccurs="0">1</s:element>
         <s:element name="PatAge" type="s:string" minOccurs="0">1</s:element>
         <s:element name="Location" type="s:string" minOccurs="0">1</s:element>
         <s:element name="Urgent" type="s:string" minOccurs="0">1</s:element>
         <s:element name="TestSetDesc" type="s:string" minOccurs="0">1</s:element>
         <s:element name="SpecimenDesc" type="s:string" minOccurs="0">1</s:element>
         <s:element name="AdmType" type="s:string" minOccurs="0">1</s:element>
         <s:element name="ReportDR" type="s:string" minOccurs="0">1</s:element>
         <s:element name="ResultFlag" type="s:string" minOccurs="0">1</s:element>
         <s:element name="CollectDate" type="s:string" minOccurs="0">1</s:element>
         <s:element name="CollectTime" type="s:string" minOccurs="0">1</s:element>
         <s:element name="ReceiveDate" type="s:string" minOccurs="0">1</s:element>
         <s:element name="ReceiveTime" type="s:string" minOccurs="0">1</s:element>
         <s:element name="AcceptDate" type="s:string" minOccurs="0">1</s:element>
         <s:element name="AcceptTime" type="s:string" minOccurs="0">1</s:element>
         <s:element name="AuthDate" type="s:string" minOccurs="0">1</s:element>
         <s:element name="AuthTime" type="s:string" minOccurs="0">1</s:element>
         <s:element name="ColStyler" type="s:string" minOccurs="0">1</s:element>
         <s:element name="InfectFlag" type="s:string" minOccurs="0">1</s:element>
         <s:element name="InfectAlert" type="s:string" minOccurs="0">1</s:element>
         <s:element name="WorkGroupMachineDR" type="s:string" minOccurs="0">1</s:element>
         <s:element name="WorkGroupMachine" type="s:string" minOccurs="0">1</s:element>
         <s:element name="Printed" type="s:string" minOccurs="0">1</s:element>
         <s:element name="PrinteFlag" type="s:string" minOccurs="0">1</s:element>
         <s:element name="CollectUser" type="s:string" minOccurs="0">1</s:element>
         <s:element name="ReceiveUser" type="s:string" minOccurs="0">1</s:element>
         <s:element name="AcceptUser" type="s:string" minOccurs="0">1</s:element>
         <s:element name="AuthUser" type="s:string" minOccurs="0">1</s:element>
         <s:element name="SelfPrintFlag" type="s:string" minOccurs="0">1</s:element>
        </s:sequence>
       </s:complexType>
      </s:element>
     </s:choice>
    </s:complexType>
   </s:element>
  </s:schema>
</QryReportListResult>
<RetVal>0</RetVal>
<Error/>
<Node>QryReportList</Node>
<RowCount>0</RowCount>
<ResType>0</ResType>
</Response>


这种XML文件格式咋解析啊?各位大神


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2016-8-9 16:21:43 | 显示全部楼层
这就是正常的Xml格式正常解析就行了吧
 楼主| 发表于 2016-8-9 16:57:54 | 显示全部楼层
解析不到- - 最上面是个不带命名空间的,命名空间加在里面的。。。
 楼主| 发表于 2016-8-9 16:58:40 | 显示全部楼层


代码这样写的


[C#] 纯文本查看 复制代码
string xmlStr = "<Response> <QryReportListResult> <s:schema id=\"DefaultDataSet\" xmlns=\"\" attributeFormDefault=\"qualified\" elementFormDefault=\"qualified\" xmlns:s=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"> <s:element name=\"DefaultDataSet\" msdata:IsDataSet=\"true\"> <s:complexType> <s:choice maxOccurs=\"unbounded\"> <s:element name=\"QryReportList\"> <s:complexType> <s:sequence> <s:element name=\"VisitNumberDR\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"Status\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"StatusDesc\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"EpisodeNo\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"Labno\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"RegNo\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"RecordNo\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"PatName\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"SurName\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"PatSex\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"PatAge\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"Location\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"Urgent\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"TestSetDesc\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"SpecimenDesc\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AdmType\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"ReportDR\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"ResultFlag\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"CollectDate\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"CollectTime\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"ReceiveDate\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"ReceiveTime\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AcceptDate\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AcceptTime\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AuthDate\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AuthTime\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"ColStyler\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"InfectFlag\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"InfectAlert\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"WorkGroupMachineDR\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"WorkGroupMachine\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"Printed\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"PrinteFlag\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"CollectUser\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"ReceiveUser\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AcceptUser\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AuthUser\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"SelfPrintFlag\" type=\"s:string\" minOccurs=\"0\">1</s:element> </s:sequence> </s:complexType> </s:element> </s:choice> </s:complexType> </s:element> </s:schema> </QryReportListResult> <RetVal>0</RetVal> <Error/> <Node>QryReportList</Node> <RowCount>0</RowCount> <ResType>0</ResType> </Response>".Trim();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xmlStr);

            //XmlNodeList value = xmlDoc.SelectNodes("/Response/QryReportListResult/s:schema/s:element/s:complexType/s:choice/s:element/s:complexType/s:sequence");

            XmlElement root = xmlDoc.DocumentElement;
            string nameSpace = root.NamespaceURI;

            XmlNamespaceManager xmlNameSpace = new XmlNamespaceManager(xmlDoc.NameTable);
            xmlNameSpace.AddNamespace("s", nameSpace);

            XmlNodeList x = xmlDoc.GetElementsByTagName("QryReportList", "");

            XmlNodeList value2 = xmlDoc.SelectNodes("/s:schema/s:element/s:complexType/s:choice/s:element/s:complexType/s:sequence/s:element", xmlNameSpace);

            int i = xmlDoc.SelectNodes("/Response/QryReportListResult/s:schema/s:element/s:complexType/s:choice/s:element/s:complexType/s:sequence/s:element", xmlNameSpace).Count;
发表于 2016-8-9 16:58:41 | 显示全部楼层
报什么错吗?还是说取不到数据?
 楼主| 发表于 2016-8-9 16:59:08 | 显示全部楼层
站长苏飞 发表于 2016-8-9 16:21
这就是正常的Xml格式正常解析就行了吧

[C#] 纯文本查看 复制代码
string xmlStr = "<Response> <QryReportListResult> <s:schema id=\"DefaultDataSet\" xmlns=\"\" attributeFormDefault=\"qualified\" elementFormDefault=\"qualified\" xmlns:s=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"> <s:element name=\"DefaultDataSet\" msdata:IsDataSet=\"true\"> <s:complexType> <s:choice maxOccurs=\"unbounded\"> <s:element name=\"QryReportList\"> <s:complexType> <s:sequence> <s:element name=\"VisitNumberDR\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"Status\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"StatusDesc\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"EpisodeNo\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"Labno\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"RegNo\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"RecordNo\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"PatName\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"SurName\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"PatSex\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"PatAge\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"Location\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"Urgent\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"TestSetDesc\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"SpecimenDesc\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AdmType\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"ReportDR\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"ResultFlag\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"CollectDate\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"CollectTime\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"ReceiveDate\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"ReceiveTime\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AcceptDate\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AcceptTime\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AuthDate\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AuthTime\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"ColStyler\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"InfectFlag\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"InfectAlert\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"WorkGroupMachineDR\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"WorkGroupMachine\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"Printed\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"PrinteFlag\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"CollectUser\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"ReceiveUser\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AcceptUser\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"AuthUser\" type=\"s:string\" minOccurs=\"0\">1</s:element> <s:element name=\"SelfPrintFlag\" type=\"s:string\" minOccurs=\"0\">1</s:element> </s:sequence> </s:complexType> </s:element> </s:choice> </s:complexType> </s:element> </s:schema> </QryReportListResult> <RetVal>0</RetVal> <Error/> <Node>QryReportList</Node> <RowCount>0</RowCount> <ResType>0</ResType> </Response>".Trim();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xmlStr);

            //XmlNodeList value = xmlDoc.SelectNodes("/Response/QryReportListResult/s:schema/s:element/s:complexType/s:choice/s:element/s:complexType/s:sequence");

            XmlElement root = xmlDoc.DocumentElement;
            string nameSpace = root.NamespaceURI;

            XmlNamespaceManager xmlNameSpace = new XmlNamespaceManager(xmlDoc.NameTable);
            xmlNameSpace.AddNamespace("s", nameSpace);

            XmlNodeList x = xmlDoc.GetElementsByTagName("QryReportList", "");

            XmlNodeList value2 = xmlDoc.SelectNodes("/s:schema/s:element/s:complexType/s:choice/s:element/s:complexType/s:sequence/s:element", xmlNameSpace);

            int i = xmlDoc.SelectNodes("/Response/QryReportListResult/s:schema/s:element/s:complexType/s:choice/s:element/s:complexType/s:sequence/s:element", xmlNameSpace).Count;



我这样解析的,解析不到
 楼主| 发表于 2016-8-9 17:07:24 | 显示全部楼层
站长苏飞 发表于 2016-8-9 16:58
报什么错吗?还是说取不到数据?

读不到数据
发表于 2016-8-9 17:46:25 | 显示全部楼层
这个很明显是有命名空间的,你不填那肯定是取不出来的
[C#] 纯文本查看 复制代码
   XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xmlStr);

            XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDoc.NameTable);
            xmlnsManager.AddNamespace("msdata", "urn:schemas-microsoft-com:xml-msdata");
            xmlnsManager.AddNamespace("s", "http://www.w3.org/2001/XMLSchema");

            string str = xmlDoc.SelectSingleNode("//s:element", xmlnsManager).InnerText;

发表于 2016-8-9 18:48:16 | 显示全部楼层
应该解决了吧
 楼主| 发表于 2016-8-17 13:25:05 | 显示全部楼层

是的
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-12-29 05:08

© 2014-2021

快速回复 返回顶部 返回列表