首先带有命名空间的xml读取可以使用Xml.Linq,也可以使用xpath,本文将采用xpath的方式解析。
原文参考了:https://www.cnblogs.com/duanjt/p/5440540.html
同时参考了:https://www.cnblogs.com/shixudong/p/4056400.html
首先带有命名空间的xml如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<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怎么实现呢,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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 |
from:https://www.cnblogs.com/duanjt/p/11654173.html