首先带有命名空间的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. Poj-3286- How many 0's? - 【基础数位DP】

    How many 0's? Description A Benedict monk No.16 writes down the decimal representations of all natur ...

  2. m_strcpy

    自己实现strcpy函数 #include <stdio.h> #include <assert.h> //如果它的条件返回错误,则终止程序执行 char *m_strcpy( ...

  3. singleton单例模式小结

    1.饿汉模式 public class SingletonEntity2 { // 在类加载的时候创建对象:饿汉模式 public static SingletonEntity2 obj = new ...

  4. python - django (查询、聚合、分组)

    # """ ---- 正向查询按字段,反向查询按表名 一: 一对多 正向查询:(字段对象.关联表.查询字段) x_obj = models.Book.objects.fi ...

  5. jQuery模拟键盘打字逐字逐句显示文本

    jQuery模拟键盘打字逐字逐句显示文本 html代码 <!doctype html> <html lang="zh"> <head> < ...

  6. 重写Router.prototype.push后还报NavigationDuplicated错误的解决方法

      vue项目路由跳转时控制台出现NavigationDuplicated错误, message: "Navigating to current location (XXX) is not  ...

  7. 44、[源码]-Spring容器创建-BeanFactory预准备

    44.[源码]-Spring容器创建-BeanFactory预准备 @Override public void refresh() throws BeansException, IllegalStat ...

  8. hive 的 beeline用法

    先开启服务端: nohup hive --service metastore & nohup  hive --service hiveserver2 & 进入beeline: beel ...

  9. 学到了林海峰,武沛齐讲的Day26 反射 组合的方式完成授

    class BlackMedium: feature='Ugly' def __init__(self,name,addr): self.name=name self.addr=addr def se ...

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

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