2010-12-15 4 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

관련 문제