0

I have a WSDL file as described below, and through programmatically need to read the the schema part from the below WSDL using c#.net core 3.1 along with targetNamespace of each schema part to pass it to the following 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>

Update 1 Here is my code that Getting the following error : The root element of a W3C XML Schema should be and its namespace should be '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);
jdweng
  • 33,250
  • 2
  • 15
  • 20
u100619
  • 1
  • 2
  • Why? I gave following link in your previous posting : https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldocument.validate?view=netcore-3.1. You only need main schema for validation. The schema validate method will automatically extra the child schemas. To get schema all you need to do in the link code is to move the xmlreader to the schema tag. – jdweng Oct 31 '20 at 14:13
  • @jdweng, Do you mean main schema means, main wsdl file? If so, in WSDL file there are two schemas. So when use WSDL getting an error that code is expecting – u100619 Oct 31 '20 at 14:52

1 Answers1

0

You are missing the namespace with the 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);
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • string xml = File.ReadAllText("SingleWSDL.xml"); StringReader sReader = new StringReader(xml); XmlReader xReader = XmlReader.Create(sReader); xReader.ReadToFollowing("schema", "http://www.w3.org/2001/XMLSchema"); XmlDocument document = new XmlDocument(); document.Load(xReader); ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler1); document.Validate(eventHandler); – u100619 Nov 01 '20 at 10:00
  • Above code throwing an error that "This document already has a 'DocumentElement' node." – u100619 Nov 01 '20 at 10:03
  • You have two XmlReader. You do not need the following line : XmlDocument document = new XmlDocument(); Then change my xReader to the sample code reader. – jdweng Nov 01 '20 at 11:08
  • Sorry, I am not getting your point clearly. In the XML document statement I am loading the xReader data and then validating. So if XmlDocument document = new XmlDocument(); line removes then how to validate. Could you please explain me? – u100619 Nov 01 '20 at 11:31
  • I updated my code. The Schemas.Add() comes from the XmlReader. – jdweng Nov 01 '20 at 12:04
  • Getting the following error : The root element of a W3C XML Schema should be and its namespace should be 'http://www.w3.org/2001/XMLSchema'. – u100619 Nov 01 '20 at 13:12
  • 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("http://www.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); – u100619 Nov 01 '20 at 13:14
  • Put a break point on Validate. Does xReader say none or has a value? I getting a value. I you are seeing results I think it means the xml file is not using the same namespace as the schema. – jdweng Nov 01 '20 at 14:57
  • Are you getting an exception or just the results of the Validation? Who created the WSDL? The namespace prefix in the WSDL (schema) isn't agreeing with the actual xml. Not sure if the WSDL is wrong or the xml. That is the purpose of doing the validation is to find discrepancies. – jdweng Nov 01 '20 at 17:45
  • I have used the Xelement object to find the schema local name in that same xml. Then loop through the schema and and pass it to Schemaset for validation. – u100619 Nov 06 '20 at 09:05
  • Are you getting any errors? That sound like a Kludge to work around a WSDL that has a namespace that doesn't match the schema namespace. If your task is to validate then instead of fixing the root issue you are allowing the error to still exist. It is like taking at test and instead of marking your answer wrong, you are changing the answer sheet to a wrong answer. – jdweng Nov 06 '20 at 10:34