Solved

WSDL to c#

Posted on 2011-03-11
3
3,940 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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35108725
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
ID: 35108885
You have absolutely right. It works that way.
Next time I should use my brain. :)
Thank you very much kaufmed!
0
 
LVL 75

Expert Comment

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


Glad to help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Chat Room 1 43
Is setting this variable making a duplicate in memory 4 30
Need help on C# Linq query on object 6 53
Error in JQuery 5 38
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

911 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

24 Experts available now in Live!

Get 1:1 Help Now