Groups | Blog | Home
all groups > dotnet xml > june 2005 >

dotnet xml : xsd.exe cannot generate schema


PeterW
6/14/2005 7:36:02 AM
I have an xml file from which I want to generate an xsd schema and at a later
stage a cs class.

The xml file has a mix of defined namespaces and also an empty namespace.
These are defined as follows:
<silcn:silcn xmlns:silcn='http://silcn.org/200309'
xmlns='http://xmlprobe.com/200312'>

it contains an element <report> off the root and also a separate
<Silcn:report> again off the root.

When running xsd from the command line on the xml file it fails and returns
an Error Message as follows:
A DataTable named 'report' already belongs to this DataSet.

Why can xsd not tell the difference between <report> and <Silcn:report> when
they are clearly different?

cheers
--
DC
6/14/2005 1:11:50 PM
Correct, xsd.exe cannot generate (infer) a schema from such an xml file.
But it is pretty simple to do it yourself. For example, write the XSD like
so:

---- Begin Silcn.xsd ----

<xs:schema
targetNamespace="http://silcn.org/200309"
xmlns="http://silcn.org/200309"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ns1="http://xmlprobe.com/200312"
attributeFormDefault="qualified"
elementFormDefault="qualified"
[quoted text, click to view]

<xs:import namespace="http://xmlprobe.com/200312"
schemaLocation="XmlProbeReport.xsd" />

<xs:element name="silcn">
<xs:complexType>
<xs:sequence>
<xs:element ref="ns1:report" minOccurs="0" />
<xs:element ref="report" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="report" type="xs:string" />

</xs:schema>

---- End Silcn.xsd ----


---- Begin XmlProbeReport.xsd ----
<xs:schema
targetNamespace="http://xmlprobe.com/200312"
xmlns="http://xmlprobe.com/200312"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="qualified"
elementFormDefault="qualified"
[quoted text, click to view]

<xs:element name="report" type="xs:string" />

</xs:schema>
---- End XmlProbeReport.xsd ----


Then, if you run xsd.exe to generate classes like so:

xsd.exe /c Silcn.xsd XmlProbeReport.xsd

you will get something like this:

---- Begin generated class ----

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://silcn.org/200309")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://silcn.org/200309",
IsNullable=false)]
public class silcn {

[System.Xml.Serialization.XmlElementAttribute(Namespace="http://xmlprobe.com/200312")]
public string report;

[System.Xml.Serialization.XmlElementAttribute("report")]
public string report1;
}

---- End generated class ----


Some people find it simpler to go in the other direction - start with C# and
then generate XSD from it. To do this, write the C# (or VB) code similar
to the above, attributed with the proper namespaces and so on. Then compile
into an assembly (exe or DLL). Then do:

xsd /type:silcn <assembly>

...This will give you 2 xsd files, corresponding to what I provided above.

You can then round-trip, working in C# or VB and generating XSD, then
tweaking XSD and re-generating C# or VB. If you have a test driver, you
can verify that the C# code can actually de-serialize from a sample XML
stream. Eventually you will get to the right schema and class.

It is only in the inferring of the initial schema from an XML document, that
the obstacle arises. If you skip that step, you should be on your way. I
know this example is probably much simpler than what you need, but you
should be able to follow the solution pattern for larger, more complex
schema.

-Dino
..NET Developer Group
d i n o c h / online . m i c r o s o f t . c o m





[quoted text, click to view]

PeterW
6/15/2005 8:16:10 AM
I had gone down the route of constructing xsds myself before I posted the
question. You did show me how to fix some things I got wrong.

I resorted to updating the incoming xml files with a transformation as a
kludge and then extracting a schema which is not a preferred option when
updating Vocabularies and Policies.

The original question is still valid however, being if a namespace is
defined and is not qualified with a prefix, it should apply to all
unqualified elements in the file by default as I understand it. I would have
thought it should be dealt with by default by any schema inferencing tool
such as xsd.exe. I would still like to understand why it is not as a matter
of interest rather than to fix a problem.

The reason I ask is that BizTalk makes a lot of use of xsd schemas.
Maintaining such systems should be as easy as possible and if possible be
able to be done by non programmers. The harder it is to create schemas the
more of a maintainability nightmare there is with evolving business rules
using tools such as the Business Rule Composer as business changes over time.
If that is not the case then instead of making reliance on skills less over
time the converse is true. You can see where I am going.

cheers
--
PeterW


[quoted text, click to view]
AddThis Social Bookmark Button