7

I am attempting to parse WSDL, along the lines of the example given here.

The author notes, in the comments, that the example is not capable of drilling down into complex data types.

And in fact, when I run the example, it does not appear to even handle simple data types.

I have poked around in System.Web.Services.Description.ServiceDescription class, which is used in the example, but cannot find any actual parameter or return type information at run-time. I gather that I may need to do some manual parsing of an xsd file?

Both google and stackoverflow appear to lack a complete example of how to drill down into complex types programmatically, so... how should I do this?

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
jaws
  • 1,952
  • 4
  • 20
  • 27

2 Answers2

22

This is not pretty - but it gets the job done (hopefully ;). I based this code partly on the link you provided, and then added some recursion to parse out the different types included in the schema, as well as the inner elements and their data types. This definitely does not take into account all possiblities in an XML schema, but I think it exemplifies enough that you could add complexity to this if neccessary.

I hope this helps!!!!

internal class Program
{
    private static void Main(string[] args)
    {
        //Build the URL request string
        UriBuilder uriBuilder = new UriBuilder(@"http://digicomdev:8888/digitalOrderBroker/digitalOrderBroker.asmx");
        uriBuilder.Query = "WSDL";

        HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(uriBuilder.Uri);
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Method = "GET";
        webRequest.Accept = "text/xml";

        //Submit a web request to get the web service's WSDL
        ServiceDescription serviceDescription;
        using (WebResponse response = webRequest.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                serviceDescription = ServiceDescription.Read(stream);
            }
        }

        //Loop through the port types in the service description and list all of the 
        //web service's operations and each operations input/output
        foreach (PortType portType in serviceDescription.PortTypes)
        {
            foreach (Operation operation in portType.Operations)
            {
                Console.Out.WriteLine(operation.Name);

                foreach (var message in operation.Messages)
                {
                    if (message is OperationInput)
                        Console.Out.WriteLine("Input Message: {0}", ((OperationInput) message).Message.Name);
                    if (message is OperationOutput)
                        Console.Out.WriteLine("Output Message: {0}", ((OperationOutput) message).Message.Name);

                    foreach (Message messagePart in serviceDescription.Messages)
                    {
                        if (messagePart.Name != ((OperationMessage) message).Message.Name) continue;

                        foreach (MessagePart part in messagePart.Parts)
                        {
                            Console.Out.WriteLine(part.Name);
                        }
                    }
                }
                Console.Out.WriteLine();
            }
        } //End listing of types

        //Drill down into the WSDL's complex types to list out the individual schema elements 
        //and their data types
        Types types = serviceDescription.Types;
        XmlSchema xmlSchema = types.Schemas[0];

        foreach (object item in xmlSchema.Items)
        {
            XmlSchemaElement schemaElement = item as XmlSchemaElement;
            XmlSchemaComplexType complexType = item as XmlSchemaComplexType;

            if (schemaElement != null)
            {
                Console.Out.WriteLine("Schema Element: {0}", schemaElement.Name);

                XmlSchemaType schemaType = schemaElement.SchemaType;
                XmlSchemaComplexType schemaComplexType = schemaType as XmlSchemaComplexType;

                if (schemaComplexType != null)
                {
                    XmlSchemaParticle particle = schemaComplexType.Particle;
                    XmlSchemaSequence sequence =
                        particle as XmlSchemaSequence;
                    if (sequence != null)
                    {
                        foreach (XmlSchemaElement childElement in sequence.Items)
                        {
                            Console.Out.WriteLine("    Element/Type: {0}:{1}", childElement.Name,
                                                  childElement.SchemaTypeName.Name);
                        }
                    }
                }
            }
            else if (complexType != null)
            {
                Console.Out.WriteLine("Complex Type: {0}", complexType.Name);
                OutputElements(complexType.Particle);
            }
            Console.Out.WriteLine();
        }

        Console.Out.WriteLine();
        Console.In.ReadLine();
    }

    private static void OutputElements(XmlSchemaParticle particle)
    {
        XmlSchemaSequence sequence = particle as XmlSchemaSequence;
        XmlSchemaChoice choice = particle as XmlSchemaChoice;
        XmlSchemaAll all = particle as XmlSchemaAll;

        if (sequence != null)
        {
            Console.Out.WriteLine("  Sequence");

            for (int i = 0; i < sequence.Items.Count; i++)
            {
                XmlSchemaElement childElement = sequence.Items[i] as XmlSchemaElement;
                XmlSchemaSequence innerSequence = sequence.Items[i] as XmlSchemaSequence;
                XmlSchemaChoice innerChoice = sequence.Items[i] as XmlSchemaChoice;
                XmlSchemaAll innerAll = sequence.Items[i] as XmlSchemaAll;

                if (childElement != null)
                {
                    Console.Out.WriteLine("    Element/Type: {0}:{1}", childElement.Name,
                                          childElement.SchemaTypeName.Name);                        
                }
                else OutputElements(sequence.Items[i] as XmlSchemaParticle);
            }
        }
        else if (choice != null)
        {
            Console.Out.WriteLine("  Choice");
            for (int i = 0; i < choice.Items.Count; i++)
            {
                XmlSchemaElement childElement = choice.Items[i] as XmlSchemaElement;
                XmlSchemaSequence innerSequence = choice.Items[i] as XmlSchemaSequence;
                XmlSchemaChoice innerChoice = choice.Items[i] as XmlSchemaChoice;
                XmlSchemaAll innerAll = choice.Items[i] as XmlSchemaAll;

                if (childElement != null)
                {
                    Console.Out.WriteLine("    Element/Type: {0}:{1}", childElement.Name,
                                          childElement.SchemaTypeName.Name);
                }
                else OutputElements(choice.Items[i] as XmlSchemaParticle);
            }

            Console.Out.WriteLine();
        }
        else if (all != null)
        {
            Console.Out.WriteLine("  All");
            for (int i = 0; i < all.Items.Count; i++)
            {
                XmlSchemaElement childElement = all.Items[i] as XmlSchemaElement;
                XmlSchemaSequence innerSequence = all.Items[i] as XmlSchemaSequence;
                XmlSchemaChoice innerChoice = all.Items[i] as XmlSchemaChoice;
                XmlSchemaAll innerAll = all.Items[i] as XmlSchemaAll;

                if (childElement != null)
                {
                    Console.Out.WriteLine("    Element/Type: {0}:{1}", childElement.Name,
                                          childElement.SchemaTypeName.Name);
                }
                else OutputElements(all.Items[i] as XmlSchemaParticle);
            }
            Console.Out.WriteLine();
        }
    }
}
pmartin
  • 2,731
  • 1
  • 25
  • 30
  • 1
    martin, I tried your code but XmlSchema xmlSchema = types.Schemas[0]; is always null. – Sri Reddy Mar 24 '11 at 15:11
  • @user465876: My best guess is that your URL isn't pointing to the WSDL correctly. Hope that helps! – pmartin Mar 24 '11 at 16:23
  • How can I check that? I open the service url with "?wsdl" and it opens fine. Moreover, with the original example that was posted, my wsdl was parsed and I got all the simple datatypes. Anything else I may be missing? – Sri Reddy Mar 24 '11 at 20:42
  • @pmartin Not sure if you will read this tried your code and all works.How the implementation you extended it would not drill into the complex type,yours does but returns everything .How can I just get the input params for a single method? – user9969 Jun 28 '12 at 16:07
  • @user231465 - just inside the foreach loop (`foreach (Operation operation in portType.Operations)`) just skip to the next record if `operation.Name` does not equal the method you're looking to get information for. – pmartin Jul 10 '12 at 15:17
  • Very nice!! This code much helps me to create custom test page for web services (the one that includes complex types) :) Thanks ! – Alex Dn Jul 22 '13 at 20:36
1

What do you want to do with the parsed result? For instance, you could use ServiceDescription.Read and the importer to compile an assembly if you wanted to get the types into memory for using them.

You can see an example of such an approach in this powershell script:

http://www.leeholmes.com/blog/2007/02/28/calling-a-webservice-from-powershell/

James Manning
  • 13,429
  • 2
  • 40
  • 64
  • I would rather do something a little more lightweight than compiling an assembly. I don't necessarily want to do anything with the result, I just need a listing of the parameter and return types (down to the atomic level). – jaws Dec 21 '10 at 16:51