解析复杂的WSDL参数信息 [英] Parse Complex WSDL Parameter Information

查看:167
本文介绍了解析复杂的WSDL参数信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析WSDL中,沿示例的线定这里

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.

我在System.Web.Services.Description.ServiceDescription类,这是在本例中使用戳左右,但找不到任何实际的参数或在运行时返回类型的信息。据我了解,我可能需要做一个XSD文件的一些手动解析?

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?

推荐答案

这是不是pretty - 但它能够完成任务(希望)。根据我这code部分您所提供的链接,然后增加了一些递归解析出包含在架构中的不同类型,以及内部元素和它们的数据类型。这绝对不考虑所有possiblities在一个XML架构,但我认为这体现不够,你可以,如果neccessary增加了复杂性这一点。

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();
        }
    }
}

这篇关于解析复杂的WSDL参数信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆