2010-12-15 35 views
5

hereの例の行に沿ってWSDLを解析しようとしています。複雑なWSDLパラメータ情報を解析する

コメントには、この例では複雑なデータ型を掘り下げることができないことに注意してください。

例を実行すると、単純なデータ型を処理することさえできません。

この例で使用されているSystem.Web.Services.Description.ServiceDescriptionクラスでは突き刺されましたが、実行時に実際のパラメータや型情報を見つけることはできません。 xsdファイルを手動で解析する必要があるかもしれないことがわかりますか?

Googleとstackoverflowの両方で、複雑な型にプログラムでドリルダウンする方法の完全な例が不足しているようです。どうすればよいですか?

答えて

16

これはかなりありません:

あなたはこのPowerShellスクリプトでは、このようなアプローチの例を見ることができます。私はあなたが提供したリンクにこのコードを部分的に基づいており、内部要素とそのデータ型だけでなく、スキーマに含まれるさまざまな型を解析するための再帰を追加しました。これは間違いなくXMLスキーマのすべての可能性を考慮に入れていますが、必要であればこれを複雑にすることができるということを十分に例示していると思います。

私はこれが役立つことを願っています!

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

martin、あなたのコードを試しましたが、XmlSchema xmlSchema = types.Schemas [0];常にnullです。 –

+0

@ user465876:私の推測では、あなたのURLがWSDLを正しく指していないと思います。希望が助けてくれる! – pmartin

+0

どうすれば確認できますか?私は "?wsdl"でサービスのURLを開き、それは正常に開きます。さらに、投稿された元の例では、私のwsdlが解析され、すべての単純なデータ型が得られました。他に何か迷っているかもしれませんか? –

1

解析結果はどうしますか?たとえば、型をメモリに入れて使用する場合は、ServiceDescription.Readとインポータを使用してアセンブリをコンパイルできます。 (うまくいけば;)しかし、それは仕事を取得します -

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

+0

私はむしろ、アセンブリをコンパイルするよりも少し軽いものをやります。私は必ずしも結果で何かをしたいとは思っていません、私はパラメータと戻り値の型(原子レベルまで)のリストが必要です。 – jaws

関連する問題