zoukankan      html  css  js  c++  java
  • C#读取带命名空间的xml

    首先带有命名空间的xml读取可以使用Xml.Linq,也可以使用xpath,本文将采用xpath的方式解析。

    原文参考了:https://www.cnblogs.com/duanjt/p/5440540.html

    同时参考了:https://www.cnblogs.com/shixudong/p/4056400.html

    首先带有命名空间的xml如下:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
       <soapenv:Body>
          <ns:queryResponse xmlns:ns="http://release.service.das.jeaw.com">
             <ns:return xsi:type="ax2291:QueryReturnEntity" xmlns:ax2293="http://release.service.das.jeaw.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ax2291="http://pojo.servgen.das.jeaw.com/xsd">
                <ax2291:code>DAS00000</ax2291:code>
                <ax2291:message>服务访问成功</ax2291:message>
                <ax2291:totalRowCount>1</ax2291:totalRowCount>
                <ax2291:currentPageNo>1</ax2291:currentPageNo>
                <ax2291:datas xsi:type="ax2293:EntityGZ_GZBDJXX">
                   <ax2293:GZYXM_1>吕姗姗</ax2293:GZYXM_1>
                   <ax2293:GZYZBH_1 xsi:nil="true"/>
                   <ax2293:ID_1 xsi:nil="true"/>
                </ax2291:datas>
                <ax2291:pageSize>1</ax2291:pageSize>
                <ax2291:totalPageCount>1</ax2291:totalPageCount>
             </ns:return>
          </ns:queryResponse>
       </soapenv:Body>
    </soapenv:Envelope>

    解析如上的xml,就涉及到两个类,XmlNamespaceManager和XmlDocument。XmlDocument用于解析xml,而XmlNamespaceManager则是和命名空间相关的类。

    如上的xml,如果我们想要获取到吕姗姗ID_1的true怎么实现呢,代码如下:

    string xmlStr = "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <ns:queryResponse xmlns:ns="http://release.service.das.jeaw.com"> <ns:return xsi:type="ax2291:QueryReturnEntity" xmlns:ax2293="http://release.service.das.jeaw.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ax2291="http://pojo.servgen.das.jeaw.com/xsd"> <ax2291:code>DAS00000</ax2291:code> <ax2291:message>服务访问成功</ax2291:message> <ax2291:totalRowCount>1</ax2291:totalRowCount> <ax2291:currentPageNo>1</ax2291:currentPageNo> <ax2291:datas xsi:type="ax2293:EntityGZ_GZBDJXX"> <ax2293:GZYXM_1>吕姗姗</ax2293:GZYXM_1> <ax2293:GZYZBH_1 xsi:nil="true"/> <ax2293:ID_1 xsi:nil="true"/> </ax2291:datas> <ax2291:pageSize>1</ax2291:pageSize> <ax2291:totalPageCount>1</ax2291:totalPageCount> </ns:return> </ns:queryResponse> </soapenv:Body> </soapenv:Envelope>";
    
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xmlStr);
    XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);//这一步实例化一个xml命名空间管理器
    nsMgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
    nsMgr.AddNamespace("ns", "http://release.service.das.jeaw.com");
    nsMgr.AddNamespace("ax2293", "http://release.service.das.jeaw.com/xsd");
    nsMgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    nsMgr.AddNamespace("ax2291", "http://pojo.servgen.das.jeaw.com/xsd");
    
    XmlNode nodeGZYXM = doc.SelectSingleNode("soapenv:Envelope/soapenv:Body/ns:queryResponse/ns:return/ax2291:datas/ax2293:GZYXM_1", nsMgr);
    Console.WriteLine(nodeGZYXM.InnerText); //将输出 吕姗姗
    
    XmlNode nodeId = doc.SelectSingleNode("soapenv:Envelope/soapenv:Body/ns:queryResponse/ns:return/ax2291:datas/ax2293:ID_1/@xsi:nil", nsMgr); //@xsi:nil表示获取Attribute而不是node节点
    Console.WriteLine(nodeId.InnerText); //将输出 true
  • 相关阅读:
    20145326蔡馨熠《信息安全系统设计基础》第九周学习总结
    20145326蔡馨熠 《信息安全系统设计基础》实验一 开发环境的熟悉
    20145326蔡馨熠《信息安全系统设计基础》第八周学习总结
    20145326蔡馨熠《信息安全系统设计》第7周学习总结
    20145326蔡馨熠《信息安全系统设计》第6周学习总结
    20145326蔡馨熠《信息安全系统设计》第五周学习总结
    20145326蔡馨熠《信息安全系统设计基础》第三周学习总结
    20145326蔡馨熠《信息安全系统设计》第2周学习总结
    20145326蔡馨熤《信息安全系统设计基础》第1周学习总结
    2015324 《信息安全系统设计基础》第3周学习总结
  • 原文地址:https://www.cnblogs.com/duanjt/p/11654173.html
Copyright © 2011-2022 走看看