gpt4 book ai didi

c# - Linq 2 XML : How to retrieve web methods' names from a wsdl document?

转载 作者:行者123 更新时间:2023-11-30 14:18:49 25 4
gpt4 key购买 nike

我有一个 XML 文档(它描述了一个 wsdl 服务的接口(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>

...

我需要对上面的xml执行两个操作:

  1. 检索所有元素名称(GetDummyType、SimplestWebService 等)。这些是方法名称(它们不以“Response”结尾)。
  2. 通过名称检索方法的参数(GetDummyType 的 param1 等)

到目前为止,我只设法将此文档解析为 XmlDocument:

XmlDocument doc = new XmlDocument();
doc.LoadXml(result.ToString());

(我知道那不多)

我只是想不通该 XML 是如何映射到您可以在其上使用 linq 的东西..
你是怎样做的?

谢谢。

最佳答案

您需要确保在查询中使用正确的 XML 命名空间。此外,对于 LINQ to XML,请使用 XDocument,而不是来自旧 System.XmlXmlDocument

这是我到目前为止设法想出的:

XDocument doc = XDocument.Parse(xml);
XNamespace wsdl = "http://schemas.xmlsoap.org/wsdl/";
XNamespace s = "http://www.w3.org/2001/XMLSchema";

var schema = doc.Root
.Element(wsdl + "types")
.Element(s + "schema");

var elements = schema.Elements(s + "element");
Func<XElement, string> getName = (el) => el.Attribute("name").Value;

// these are all method names
var names = from el in elements
let name = getName(el)
where !name.EndsWith("Response")
select name;

string methodName = "GetDummyType";
var method = elements
.Single(el => getName(el) == methodName);

// these are all parameters for a given method
var parameters = from par in method.Descendants(s + "element")
select getName(par);

我已经测试了这段代码,它适用于您的数据。
然而,我并不完全认为这是最简单的解决方案,所以我欢迎任何缩短代码的建议。

最好的,

关于c# - Linq 2 XML : How to retrieve web methods' names from a wsdl document?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3821262/

25 4 0