如何在.NET Core 3.1中以编程方式从WSDL定义文件中提取模式?

问题描述

我有一个如下所述的WSDL文件,通过编程需要使用c#.net core 3.1和每个架构部分的 targetNamespace 从下面的WSDL中读取架构部分,以将其传递给以下XmlReaderSettings。

XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("targetNamespace from Schema1,"schema1");
settings.Schemas.Add("targetNamespace from Schema2,"schema2");
settings.Schemas.Add("targetNamespace from Schema3,"schema3");


<wsdl:deFinitions name="Service" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:tns="http://tempuri.org/" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/Metadata" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <wsdl:types>
      <xs:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
         <xs:import namespace="http://schemas.datacontract.org/2004/07/"/>
         <xs:element name="GetData">
            <xs:complexType>
               <xs:sequence>
                  <xs:element minOccurs="0" name="value" type="xs:int"/>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
         <xs:element name="GetDataResponse">
            <xs:complexType>
               <xs:sequence>
                  <xs:element minOccurs="0" name="GetDataResult" nillable="true" type="xs:string"/>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
         <xs:element name="GetDataUsingDataContract">
            <xs:complexType>
               <xs:sequence>
                  <xs:element minOccurs="0" name="composite" nillable="true" type="q1:CompositeType" xmlns:q1="http://schemas.datacontract.org/2004/07/"/>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
         <xs:element name="GetDataUsingDataContractResponse">
            <xs:complexType>
               <xs:sequence>
                  <xs:element minOccurs="0" name="GetDataUsingDataContractResult" nillable="true" type="q2:CompositeType" xmlns:q2="http://schemas.datacontract.org/2004/07/"/>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
      </xs:schema>
      <xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/">
         <xs:complexType name="CompositeType">
            <xs:sequence>
               <xs:element minOccurs="0" name="BoolValue" type="xs:boolean"/>
               <xs:element minOccurs="0" name="StringValue" nillable="true" type="xs:string"/>
            </xs:sequence>
         </xs:complexType>
         <xs:element name="CompositeType" nillable="true" type="tns:CompositeType"/>
      </xs:schema>
   </wsdl:types>
</wsdl:deFinitions>

更新1 这是我的代码,出现以下错误:W3C XML Schema的根元素应该是,其命名空间应该是'w3.org/2001/XMLSchema'

string xml = File.ReadAllText("SingleWSDL.xml");
StringReader sReader = new StringReader(xml);
XmlReader xReader = XmlReader.Create(sReader);
xReader.ReadToFollowing("schema","schemas.xmlsoap.org/wsdl/");
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("w3.org/2001/XMLSchema",xReader); 
settings.ValidationType = ValidationType.Schema;
XmlReader reader = XmlReader.Create("Test6.xml",settings);
XmlDocument document = new XmlDocument();
document.Load(reader);document.Validate(eventHandler);

解决方法

您缺少带有ReadToFollowing的名称空间:

           string xml = File.ReadAllText("SingleWSDL.xml");
            StringReader sReader = new StringReader(xml);
            XmlReader xReader = XmlReader.Create(sReader);
            xReader.ReadToFollowing("schema","http://schemas.xmlsoap.org/wsdl/");

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.Schemas.Add("w3.org/2001/XMLSchema",xReader);