首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Linq2XML:如何从wsdl文档中检索web方法的名称?

Linq2XML:如何从wsdl文档中检索web方法的名称?
EN

Stack Overflow用户
提问于 2010-09-29 11:43:20
回答 3查看 2.2K关注 0票数 3

我有一个XML文档(描述wsdl服务的接口):

代码语言:javascript
运行
复制
<?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>
代码语言:javascript
运行
复制
                                                                           ...

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

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

到目前为止,我只将这个文档解析为一个XmlDocument:

代码语言:javascript
运行
复制
XmlDocument doc = new XmlDocument();
doc.LoadXml(result.ToString());  

(我知道这并不多)

我只是不知道XML是如何映射到可以使用linq的东西上的。

你怎么做到的?

谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-09-29 12:43:54

您需要确保在查询中使用正确的XML名称空间。另外,对于LINQ,请使用XDocument,而不是XmlDocument,它来自旧的System.Xml

到目前为止,这就是我想出来的:

代码语言:javascript
运行
复制
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);

我已经测试过这段代码,它可以在你的数据上工作。

然而,我并不完全是,这是最简单的解决方案,所以我欢迎任何关于缩短代码的建议。

最好的

票数 5
EN

Stack Overflow用户

发布于 2010-09-29 12:06:25

对于#1,使用程序集System.Linq.Xml,您可以这样做:

代码语言:javascript
运行
复制
List<string> names = new List<string>();
XDocument doc = Xdocument.Parse(result.ToString());
foreach (XElement element in doc.Elements("wsdl:types").First().Elements("s:schema").First().Elements("s:element"))
{
    names.Add(element.Attributes("name").First().Value);
}

它没有经过测试,所以您可能需要对代码进行一点调优;)

顺便说一下,您可以找到更多关于与System.Xml.Linq有关的的信息。

票数 2
EN

Stack Overflow用户

发布于 2010-09-29 12:44:40

使用linq到xml,您可以这样做-

代码语言:javascript
运行
复制
XDocument doc = XDocument.Parse( xmlstring );
var methods = from methods in doc.Descendants( "s:element" )
                where !methods.Attribute("name").Value.EndsWith("Resopnse")
                select methods;
        var methodNames = ( from method in methods
                            select method.Attribute( "name" ).Value ).ToList( );
        var paramList = from type in methods.Descendants( "s:complexType" )
                        from param in type.Descendants("s:sequence")
                        where type.HasElements && type.Parent.Attribute("name").Value == somemethodname   
                        select new { Name = param.Attribute( "name" ).Value };

添加对system.core和system.xml.linq的引用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3821262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档