PC Review


Reply
Thread Tools Rate Thread

Create XML Schema at runtime

 
 
goscottie@gmail.com
Guest
Posts: n/a
 
      12th May 2009
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.
 
Reply With Quote
 
 
 
 
Alberto Poblacion
Guest
Posts: n/a
 
      12th May 2009
<(E-Mail Removed)> wrote in message
news:60859c54-26f3-41eb-b7a9-(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Alberto Poblacion
Guest
Posts: n/a
 
      12th May 2009
<(E-Mail Removed)> wrote in message
news:60859c54-26f3-41eb-b7a9-(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      12th May 2009
(E-Mail Removed) wrote:

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

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      12th May 2009
(E-Mail Removed) wrote:

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

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
goscottie@gmail.com
Guest
Posts: n/a
 
      12th May 2009
On May 12, 10:17*am, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.org> wrote:
> <goscot...@gmail.com> wrote in message
>
> news:60859c54-26f3-41eb-b7a9-(E-Mail Removed)...
>
>
>
>
>
> > 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.- 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.
 
Reply With Quote
 
goscottie@gmail.com
Guest
Posts: n/a
 
      12th May 2009
On May 12, 10:17*am, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.org> wrote:
> <goscot...@gmail.com> wrote in message
>
> news:60859c54-26f3-41eb-b7a9-(E-Mail Removed)...
>
>
>
>
>
> > 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.- 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.
 
Reply With Quote
 
goscottie@gmail.com
Guest
Posts: n/a
 
      12th May 2009
On May 12, 10:40*am, Martin Honnen <mahotr...@yahoo.de> wrote:
> goscot...@gmail.com wrote:
> > <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.
>
> --
>
> * * * * Martin Honnen --- MVP XML
> * * * *http://msmvps.com/blogs/martin_honnen/


I believe this is what I was looking for. Getta try it now. Thanks.
 
Reply With Quote
 
goscottie@gmail.com
Guest
Posts: n/a
 
      12th May 2009
On May 12, 10:40*am, Martin Honnen <mahotr...@yahoo.de> wrote:
> goscot...@gmail.com wrote:
> > <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.
>
> --
>
> * * * * Martin Honnen --- MVP XML
> * * * *http://msmvps.com/blogs/martin_honnen/


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

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create XML Schema at runtime goscottie@gmail.com Microsoft C# .NET 0 12th May 2009 03:33 PM
can't create schema using .adp (or transfertext method renderred useless without schema) kreplech@gmail.com Microsoft Access 1 23rd Jun 2006 09:49 PM
How do I create an XML Schema? =?Utf-8?B?UmVkc3Rlcg==?= Microsoft Word Document Management 1 8th Jun 2005 10:10 AM
How do I use XML to create a Schema? =?Utf-8?B?Y2FtcHM=?= Microsoft Word Document Management 1 8th May 2005 07:51 PM
How to create the schema.ini file? Lior M via AccessMonster.com Microsoft Access External Data 1 1st Jan 2005 11:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:40 AM.