By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,978 Members | 1,362 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,978 IT Pros & Developers. It's quick & easy.

xsd.exe cannot generate schema

P: n/a
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
--
PeterW
Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
DC
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"

<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"
<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(Namespac e="http://silcn.org/200309")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="http://silcn.org/200309",
IsNullable=false)]
public class silcn {

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

[System.Xml.Serialization.XmlElementAttribute("repo rt")]
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

"PeterW" <ns**@noemail.nospam> wrote in message
news:D1**********************************@microsof t.com...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
--
PeterW

Nov 12 '05 #2

P: n/a
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
"DC" wrote:
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"


<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"


<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(Namespac e="http://silcn.org/200309")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="http://silcn.org/200309",
IsNullable=false)]
public class silcn {

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

[System.Xml.Serialization.XmlElementAttribute("repo rt")]
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

"PeterW" <ns**@noemail.nospam> wrote in message
news:D1**********************************@microsof t.com...
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
--
PeterW


Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.