首先带有命名空间的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

C#读取带命名空间的xml的更多相关文章

  1. C#读取带命名空间的xml,xaml文件的解决方案

    使用C#读取xml文件有三种常用的方式: 1.xmlDocument 2.XmlTextReader 3.Linq To Xml 但是这些方式在读写有些带命名空间的xml时就不知道怎么办了(例如把xa ...

  2. 带命名空间的XML的dom4j应用<转>

    Element root = document.getRootElement();     List   recordenvlist = document.selectNodes("//gm ...

  3. 由“Jasperrpeorts 4.1.2升级到5.1.2对flex项目的解析”到AS3 带命名空间的XML的操作

    原文同步至:http://www.waylau.com/from-jasperrpeorts-4-1-2-upgraded-to-5-1-2-parsing-of-flex-projects-to-t ...

  4. dom4j解析带命名空间的xml文件

    文件内容如下 <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=& ...

  5. XML记一次带命名空间的xml读取

    public static void ReadXML(string xmlUrl) { //判断文件是否存在 if (!File.Exists(xmlUrl)) { Console.WriteLine ...

  6. Linq to xml 操作带命名空间的xml

    昨天需要操作用代码操作csproj文件,实现不同vs版本的切换. 在用XElement读取了csproj文件以后怎么也获取不到想要的对象. 反反复复试验了好多次都不得要领:先看下csproj文件的内容 ...

  7. C# 读取带有命名空间的xml

    XML文本: <?xml version="1.0" encoding="utf-8"?> <ECG xmlns:xsi="http ...

  8. linq检索带命名空间的xml

    XElement el = XElement.Load(fil); XNamespace ns = "http://schemas.microsoft.com/ado/2009/11/edm ...

  9. Linq to Xml读取复杂xml(带命名空间)

    前言:xml的操作方式有多种,但要论使用频繁程度,博主用得最多的还是Linq to xml的方式,觉得它使用起来很方便,就用那么几个方法就能完成简单xml的读写.之前做的一个项目有一个很变态的需求:C ...

随机推荐

  1. python3 读取avro文件

    官网示例文档:http://avro.apache.org/docs/current/gettingstartedpython.html#download_install 需要注意的是,官网给出的是p ...

  2. VUE this.$http.post 与后端flask 数据交互

    背景: 小鱼第一次前端用的VUE,然后前后端的交互调了几次,记录下来留给自己下次使用 前端 通过  form.XXX 获取数据,代码: <template> <el-form ref ...

  3. python_反射:应用

    class User(object): def denglu(self): print('欢迎来到登录页面!') def zhuce(self): print('欢迎来到注册页面!') def you ...

  4. idea远程debug调试阿里云ECS

    1.首先远程服务器的代码跟本地项目代码应该完全一致,否则会出现debug混乱现象,亲测. 2.config如图: ①命名可以省略②复制这个地址③输入远程ip和自定义且未被占用的端口号xxxx 3.开放 ...

  5. 字符编码,python解释器------总结

    目录 1. 编码: 1.字符编码 2. 编码的历史 3. 编码和解码 2. python解释器 解释代码的流程 1. 读取文本到解释器 2. 识别代码(检查语法问题) 3. 往终端打印 1. 编码: ...

  6. HTML 006 文本格式化(了解)

    HTML 文本格式化 HTML 文本格式化 加粗文本 斜体文本 电脑自动输出 这是 下标 和 上标 尝试一下 » HTML 格式化标签 HTML 使用标签 <b>("bold&q ...

  7. YARN构建--解决cypress下载慢问题

      背景 注意:      此方案仅适合已经自行搭建私有仓库的用户使用      如非必要,尽可能使用软件开发云或其他服务提供的镜像站,避免此类特殊处理(会导致仓库维护成本增加) 场景描述 YARN构 ...

  8. 洛谷 P3627 [APIO2009]抢掠计划 题解

    Analysis 建图+强连通分量+SPFA求最长路 但要保证最后到达的点中包含酒馆 虽然思路并不难想,但要求的代码能力很高. #include<iostream> #include< ...

  9. PHP全栈学习笔记28

    数据库Mysql概述,数据库操作,数据表操作,数据类型,管理数据库 order by asc; 升序 desc 降序 sql标准语言: 数据查询语言 select 数据定义语言 create/alte ...

  10. docker hub 国内镜像加速地址

    当前可用 配置文件:vim /etc/docker/daemon.json { "registry-mirrors" : [ "http://docker.mirrors ...