Create XML Schema at runtime

G

goscottie

Hi,
I see plenty examples of create XML doc using schema. How about going
from the XML doc to schema using code at runtime? All elements are
string. So,

<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

With above, I would like to create

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Without using design time tool but rather create at runtime in C#
code. Can you help? TIA.
 
A

Alberto Poblacion

Hi,
I see plenty examples of create XML doc using schema. How about going
from the XML doc to schema using code at runtime? All elements are
string. So,

<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

With above, I would like to create

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Without using design time tool but rather create at runtime in C#
code. Can you help? TIA.


One trick: Load de XML into a DataSet (by menans of the ReadXml method of
the DataSet) and then use the WriteXmlSchema method of the DataSet to write
out the automatically inferred schema. However, this may not write the xsd
exactly as you expected, depending on how the DataSet infers its shema from
the supplied XML.
 
A

Alberto Poblacion

Hi,
I see plenty examples of create XML doc using schema. How about going
from the XML doc to schema using code at runtime? All elements are
string. So,

<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

With above, I would like to create

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Without using design time tool but rather create at runtime in C#
code. Can you help? TIA.


One trick: Load de XML into a DataSet (by menans of the ReadXml method of
the DataSet) and then use the WriteXmlSchema method of the DataSet to write
out the automatically inferred schema. However, this may not write the xsd
exactly as you expected, depending on how the DataSet infers its shema from
the supplied XML.
 
M

Martin Honnen

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

With above, I would like to create

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"

The XML instance documents has elements in no namespace so I don't see
why you would have a targetNamespace for your schema.

Without using design time tool but rather create at runtime in C#
code. Can you help? TIA.

XmlSchemaInference inf = new XmlSchemaInference();
XmlSchemaSet schemaSet = inf.InferSchema(XmlReader.Create("file.xml"));


Then you can process schemaSet.Schemas() or generally use the SchemaSet
as it suits you.
 
M

Martin Honnen

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

With above, I would like to create

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"

The XML instance documents has elements in no namespace so I don't see
why you would have a targetNamespace for your schema.

Without using design time tool but rather create at runtime in C#
code. Can you help? TIA.

XmlSchemaInference inf = new XmlSchemaInference();
XmlSchemaSet schemaSet = inf.InferSchema(XmlReader.Create("file.xml"));


Then you can process schemaSet.Schemas() or generally use the SchemaSet
as it suits you.
 
G

goscottie

   One trick: Load de XML into a DataSet (by menans of the ReadXml method of
the DataSet) and then use the WriteXmlSchema method of the DataSet to write
out the automatically inferred schema. However, this may not write the xsd
exactly as you expected, depending on how the DataSet infers its shema from
the supplied XML.- Hide quoted text -

- Show quoted text -

Yes, I tried this method and like you said, it is slightly different
than expected. Not too bad to start with, however. I'm still looking
for more elegant method.
 
G

goscottie

   One trick: Load de XML into a DataSet (by menans of the ReadXml method of
the DataSet) and then use the WriteXmlSchema method of the DataSet to write
out the automatically inferred schema. However, this may not write the xsd
exactly as you expected, depending on how the DataSet infers its shema from
the supplied XML.- Hide quoted text -

- Show quoted text -

Yes, I tried this method and like you said, it is slightly different
than expected. Not too bad to start with, however. I'm still looking
for more elegant method.
 
G

goscottie

The XML instance documents has elements in no namespace so I don't see
why you would have a targetNamespace for your schema.


   XmlSchemaInference inf = new XmlSchemaInference();
   XmlSchemaSet schemaSet = inf.InferSchema(XmlReader.Create("file.xml"));

Then you can process schemaSet.Schemas() or generally use the SchemaSet
as it suits you.

I believe this is what I was looking for. Getta try it now. Thanks.
 
G

goscottie

The XML instance documents has elements in no namespace so I don't see
why you would have a targetNamespace for your schema.


   XmlSchemaInference inf = new XmlSchemaInference();
   XmlSchemaSet schemaSet = inf.InferSchema(XmlReader.Create("file.xml"));

Then you can process schemaSet.Schemas() or generally use the SchemaSet
as it suits you.

I believe this is what I was looking for. Getta try it now. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top