Linq 2 xml:如何检索 Web 方法wsdl 文档中的名称?
我有一个 XML 文档(它描述了 wsdl 服务的接口):
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="GetDummyType">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="param1" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetDummyTypeResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetDummyTypeResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="SimplestWebService">
<s:complexType />
</s:element>
<s:element name="SimplestWebServiceResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="SimplestWebServiceResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="SignInComp">
<s:complexType />
</s:element>
<s:element name="SignInCompResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="SignInCompResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
...
我需要对上述 xml 执行两个操作:
- 检索所有元素名称(GetDummyType、SimplestWebService 等)。这些是方法名称(它们不以“Response”结尾)。
- 按名称检索方法的参数(GetDummyType 的 param1 等)
到目前为止,我只能将此文档解析为 XmlDocument:(
XmlDocument doc = new XmlDocument();
doc.LoadXml(result.ToString());
我知道这并不多)
我只是无法弄清楚该 XML 是如何映射到某些内容的你可以使用 linq..
你是怎么做到的?
谢谢。
I have a XML documents (which describes a wsdl service's interface):
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="GetDummyType">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="param1" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetDummyTypeResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetDummyTypeResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="SimplestWebService">
<s:complexType />
</s:element>
<s:element name="SimplestWebServiceResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="SimplestWebServiceResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="SignInComp">
<s:complexType />
</s:element>
<s:element name="SignInCompResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="SignInCompResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
...
Two operations I need to perform on the above xml:
- retrieve all elements names (GetDummyType, SimplestWebService etc.) Those are the methods names (they don't end with "Response").
- retrieve a method's params by it's name (param1 for GetDummyType etc.)
I've managed so far only to parse this document as an XmlDocument:
XmlDocument doc = new XmlDocument();
doc.LoadXml(result.ToString());
(I know that's not much)
I just can't figure out how that XML is mapped to something you can use linq on..
How do you do that?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要确保在查询中使用正确的 XML 命名空间。
另外,对于 LINQ to XML,请使用
XDocument
,而不是XmlDocument
,后者来自旧的System.Xml
。这是我到目前为止所想出的:
我已经测试了这段代码并且它适用于您的数据。
然而我并不完全认为它是最简单的解决方案,所以我欢迎任何缩短代码的建议。
最好的,
担
You need to make sure to use correct XML namespaces in queries.
Also, for LINQ to XML, use
XDocument
, notXmlDocument
, which is from oldSystem.Xml
.This is what I managed to come up with so far:
I have tested this code and it works on your data.
However I am not entirely it is the simplest solution there is so I welcome any suggestions to shortening the code.
Best,
Dan
对于#1,使用程序集 System.Linq.Xml 您可以执行以下操作:
它未经测试,因此您可能需要稍微调整代码;)
顺便说一句,您可能会找到更多信息有关 System.Xml.Linq 的 msdn
for #1, using the assembly
System.Linq.Xml
you could do sth like:it isn't tested so you may have to tune a bit the code ;)
BTW, you may find more information on msdn concerning System.Xml.Linq
使用 linq to xml 你可以做这样的事情 -
添加对 system.core 和 system.xml.linq 的引用
using linq to xml you can do something like this -
add reference to system.core and system.xml.linq