?
Solved

WSDL to c#

Posted on 2011-03-11
3
Medium Priority
?
4,228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

765 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