加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:38:22 所属栏目:百科 来源:网络整理
导读:我有一个 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.
我有一个 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的东西..
你是怎样做的?

谢谢.

解决方法

您需要确保在查询中使用正确的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);

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

最好,担

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读