Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ2XML:如何从wsdl文档中检索web方法的名称?_C#_Linq_Linq To Xml - Fatal编程技术网

C# LINQ2XML:如何从wsdl文档中检索web方法的名称?

C# LINQ2XML:如何从wsdl文档中检索web方法的名称?,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,我有一个描述wsdl服务接口的XML文档: <?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

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

<?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是如何映射到可以在上面使用linq的东西上的。。 你是怎么做到的

谢谢。

对于1,使用assembly System.Linq.Xml可以执行以下操作:

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);
}
它没有经过测试,因此您可能需要对代码进行一些调优


顺便说一句,您可能会找到有关

的更多信息,您需要确保在查询中使用正确的XML名称空间。 另外,对于LINQ到XML,请使用XDocument,而不是XmlDocument,它来自于old 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);
我已经测试了这段代码,它对您的数据有效。 然而,我并不完全相信这是最简单的解决方案,所以我欢迎任何缩短代码的建议

最好的,
Dan

使用linq到xml,您可以执行以下操作-

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的引用

Hi!我想对你的代码做一些评论。首先,您不能通过ns:element约定查询具有名称空间的元素。您需要显式指定名称空间,请参阅MSDN:。其次,为什么要在一个方法上多次使用后代?如果要精确地再现层次结构,应该使用元素扩展方法,因为它们保证结构与您想象的相同。如果不是,method.degenantss:element将以任何方式返回所有方法参数,即使它们更深。仅供参考:Minor glitch返回了一些不相关的方法名称,我通过:var methodsNamements=elements解决了这些问题。其中x=>x.subgenders.Count!=0; 以及查询方法元素而不是元素。但除此之外-就像一个魅力!谢谢。
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 };