Linq 2 xml:如何从wsdl文档中检索Web方法的名称?

Ore*_*n A 3 c# linq linq-to-xml

我有一个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>
Run Code Online (Sandbox Code Playgroud)

...

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

  1. 检索所有元素的名称(GetDummyType,SimplestWebService等),这些都是方法的名称(他们不"响应"结尾).
  2. 通过它的名称检索方法的参数(GetDummyType的param1等)

到目前为止我管理的只是将此文档解析为XmlDocument:

XmlDocument doc = new XmlDocument();
doc.LoadXml(result.ToString());  
Run Code Online (Sandbox Code Playgroud)

(我知道并不多)

我只是无法弄清楚XML是如何映射到你可以使用linq的东西..
你是怎么做到的?

谢谢.

Dan*_*mov 5

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

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

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);
Run Code Online (Sandbox Code Playgroud)

我已经测试了这段代码,它可以处理您的数据.
但是我并不完全是最简单的解决方案,所以我欢迎任何缩短代码的建议.

最好,