Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

WSDL to c#

Posted on 2011-03-11
3
Medium Priority
?
4,315 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

610 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