Linq 2 xml: How to get web method names from wsdl document?

I have XML documents (which describes the wsdl service 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>

                                                              ...

Two operations that I need to perform on the above xml:

  • get all element names (GetDummyType, SimplestWebService, etc.) These are method names (they do not end with "Response").
  • get method parameters by name (param1 for GetDummyType, etc.)

So far, I have only been able to parse this document as an XmlDocument:

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

(I know that not much)

I just can't figure out how this XML maps to the fact that you can use linq on.
How do you do that?

Thank.

+3
3

, XML . , LINQ to XML XDocument, not 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);

, .
, .

Best,
Dan

+5

# 1, 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);
}

, , , ;)

, msdn System.Xml.Linq

+2

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

0
source

Source: https://habr.com/ru/post/1767155/


All Articles