0

我有一个如下所述的 WSDL 文件,并且需要通过编程方式使用 c#.net core 3.1 从下面的 WSDL 读取架构部分以及每个架构部分的targetNamespace以将其传递给以下 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);
4

1 回答 1

0

您缺少带有 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);
于 2020-10-31T21:23:29.287 回答