Solved

WSDL to c#

Posted on 2011-03-11
3
3,902 Views
Last Modified: 2013-11-19
Hello Experts,

I build a winforms application and I would like to parse a wsdl from a wcf service and request for an element's attribute's inner text.
I found two possible solutions, but both of them have some difficulties yet.

A sample WSDL (I left out some details for better readability):


  <?xml version="1.0" encoding="utf-8" ?> 
- <wsdl:definitions name="Service1" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" >
+ <wsdl:types>
- <xsd:schema targetNamespace="http://tempuri.org/Imports">
  <xsd:import schemaLocation="http://localhost:6857/Service1.svc?xsd=xsd0" namespace="http://tempuri.org/" /> 
  <xsd:import schemaLocation="http://localhost:6857/Service1.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/" /> 
  <xsd:import schemaLocation="http://localhost:6857/Service1.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/WcfService1" /> 
  </xsd:schema>
  </wsdl:types>
+ <wsdl:message name="IService1_GetData_InputMessage">
  <wsdl:part name="parameters" element="tns:GetData" /> 
  </wsdl:message>
+ <wsdl:message name="IService1_GetData_OutputMessage">
  <wsdl:part name="parameters" element="tns:GetDataResponse" /> 
  </wsdl:message>
+ <wsdl:message name="IService1_GetDataUsingDataContract_InputMessage">
  <wsdl:part name="parameters" element="tns:GetDataUsingDataContract" /> 
  </wsdl:message>
+ <wsdl:message name="IService1_GetDataUsingDataContract_OutputMessage">
  <wsdl:part name="parameters" element="tns:GetDataUsingDataContractResponse" /> 
  </wsdl:message>
- <wsdl:portType name="IService1">
- <wsdl:operation name="GetData">
  <wsdl:input wsaw:Action="http://tempuri.org/IService1/GetData" message="tns:IService1_GetData_InputMessage" /> 
  <wsdl:output wsaw:Action="http://tempuri.org/IService1/GetDataResponse" message="tns:IService1_GetData_OutputMessage" /> 
  </wsdl:operation>
- <wsdl:operation name="GetDataUsingDataContract">
  <wsdl:input wsaw:Action="http://tempuri.org/IService1/GetDataUsingDataContract" message="tns:IService1_GetDataUsingDataContract_InputMessage" /> 
  <wsdl:output wsaw:Action="http://tempuri.org/IService1/GetDataUsingDataContractResponse" message="tns:IService1_GetDataUsingDataContract_OutputMessage" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="CustomBinding_IService1" type="tns:IService1">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="GetData">
  <soap:operation [b]soapAction="http://tempuri.org/IService1/GetData" style="document"[/b] /> 
+ <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
+ <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="GetDataUsingDataContract">
  <soap:operation [b]soapAction="http://tempuri.org/IService1/GetDataUsingDataContract"[/b] style="document" /> 
+ <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
+ <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
+ <wsdl:service name="Service1">
- <wsdl:port name="CustomBinding_IService1" binding="tns:CustomBinding_IService1">
  <soap:address location="http://localhost:6857/Service1.svc" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

Open in new window


What I am looking for is the soapAction attribute's inner text, highlighted bold, all of them.
The services are hosted on different machines, most of them with "https" address. That is the main problem for the first solution I found with the XmlDocument class.

The next code snippet works fine, until I don't try to load a wsdl on an https address:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("http://localhost:6857/Service1.svc?wsdl");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
            nsmgr.AddNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
            nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/");

            XmlNodeList xNodes = xmlDocument.SelectNodes("//wsdl:definitions/wsdl:binding/wsdl:operation/soap:operation", nsmgr);
            if (xNodes != null)
            {
                StringBuilder builder = new StringBuilder();
                foreach (XmlNode node in xNodes)
                {
                    builder.AppendLine(node.Attributes["soapAction"].InnerText);
                }
                MessageBox.Show(builder.ToString());
            }

Open in new window


As soon as I try to load a WSDL that is hosted with https, I get an "System.Net.WebException: The remote server returned an error: (401) Unauthorized." exception.
I know I should authenticate myself, but I don't know how.

The second solution works with the System.Net.HttpWebRequest and System.Web.Services.Description.ServiceDescription classes
UriBuilder uriBuilder = new UriBuilder("[b]https[/b]://example.org/Service.svc");
            uriBuilder.Query = "WSDL";

            System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uriBuilder.Uri);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Method = "GET";
            webRequest.Accept = "text/xml";        
            webRequest.Credentials = new NetworkCredential("userName", "password");

            System.Web.Services.Description.ServiceDescription serviceDescription;

            using (System.Net.WebResponse response = webRequest.GetResponse())
            using (System.IO.Stream stream = response.GetResponseStream())
            {
                serviceDescription = System.Web.Services.Description.ServiceDescription.Read(stream);
            }

            StringBuilder sb = new StringBuilder();          

            foreach (System.Web.Services.Description.Binding binding in serviceDescription.Bindings)
            {
                foreach (System.Web.Services.Description.OperationBinding operation in binding.Operations)
                {
                    sb.AppendLine(operation.Name);
                }
            }

            MessageBox.Show(sb.ToString());

Open in new window


Here I face the problem that I can't go deep enough in the wsdl. I reach the node <wsdl:operation name="GetData"> but don't reach the <soap:operation soapAction="http://tempuri.org/IService1/GetData" style="document" />  part, so I can only gain the name of the Action, but I need the complete address.

I would be satisfied with any of the solutions or any other if exists.
Thank you for reading so far! :)
0
Comment
Question by:develc
  • 2
3 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
XmlDocument.Load() is overloaded to take a stream as an argument, so why not combine the two solutions into one:
UriBuilder uriBuilder = new UriBuilder("[b]https[/b]://example.org/Service.svc");
uriBuilder.Query = "WSDL";

System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uriBuilder.Uri);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Method = "GET";
webRequest.Accept = "text/xml";
webRequest.Credentials = new NetworkCredential("userName", "password");

XmlDocument xmlDocument = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
nsmgr.AddNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/");

using (System.Net.WebResponse response = webRequest.GetResponse())
using (System.IO.Stream stream = response.GetResponseStream())
{
    xmlDocument.Load(stream);

    XmlNodeList xNodes = xmlDocument.SelectNodes("//wsdl:definitions/wsdl:binding/wsdl:operation/soap:operation", nsmgr);
    if (xNodes != null)
    {
        StringBuilder builder = new StringBuilder();
        foreach (XmlNode node in xNodes)
        {
            builder.AppendLine(node.Attributes["soapAction"].InnerText);
        }

        MessageBox.Show(builder.ToString());
    }
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:develc
Comment Utility
You have absolutely right. It works that way.
Next time I should use my brain. :)
Thank you very much kaufmed!
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Don't sweat it. We all go through brain-block now and again  : )


Glad to help!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now