How to compile a schema that uses a DataSet (xs:schema)?

1.3k Views Asked by At

I have created the simplest web service in c#:

public void AddData(DataSet ds)

The generated schema (Wsdl) looks like this:

<s:schema xmlns:s="http://www.w3.org/2001/XMLSchema">
...
<s:element ref="s:schema" />
...
</s:schema>

Note the schema does not contain any import/include elements.

I am trying to load this schema to a c# System.Xml.XmlSchema and add it to System.Xml.XmlSchemaSet:

var set = new XmlSchemaSet();
var fs = new FileStream(@"c:\temp\schema.xsd", FileMode.Open);
var s = XmlSchema.Read(fs, null);
set.Add(s);            
set.Compile();

The last line throws this exception:

The 'http://www.w3.org/2001/XMLSchema:schema' element is not declared.

It kind of makes sense: The schema generated by .Net uses the "s:schema" type which is declared in a schema which is not imported.

  1. Why does .Net create a non valid schema?
  2. How to compile the schema anyway? Whay I did is download the schema in http://www.w3.org/2001/XMLSchema and added it to the XmlSchemaSet also. This did not work since that online schema contains DTD definition. I had to manually remove it and now all works. Does this make sense or am I missing something?
2

There are 2 best solutions below

1
On

This means that the service does not have enough information to build a meaningful format for the elements so it generates a WSDL essentially saying "I'll send some sort XML, but I don't know what it will look like." We need to build out the structure of the service a bit more so the WSDL generator can create a meaningful schema.

0
On
  1. I would call it a bug. It is though very unusual to see an XML Schema referencing elements from the http://www.w3.org/2001/XMLSchema namespace.
  2. What you did is the right way - almost; in general, you should be able to compile an XML Schema file that uses a DOCTYPE reference; just make sure that the DTD is available at the specified location (or available through an XML resolver) and your reader settings are configured not to prohibit DTD processing (either the obsoleted ProhibitDtd or DtdProcessing properties on XmlReaderSettings, by default those are true).