Create XML Schema at runtime

  • Thread starter Thread starter goscottie
  • Start date Start date
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.
 
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.
 
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.
 
<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.
 
<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.
 
   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.
 
   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.
 
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.
 
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.
 
Back
Top