472,145 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

How do you use a XSD to create a XML document?

Is it possible to load a XSD and loop throw the nodes and attributes that
you whant filled out, and then generate a XML instance with the trees and
data that you have filled data in?
And how do you do that?

Regards Paw
Nov 12 '05 #1
5 17577
what do you *really* want to do?

do you want a generic xsd-to-xml generator? Something that can contrive XML
documents from any general XSD?
or is there a particular XSD you want to generate for?

one approach I have used is to use "xsd.exe /c" to generate classes, then
just instantiate the classes. Serialization of an instance of said class
produces an XML document that conforms to the XSD. example:

http://www.winisp.net/cheeso/srcview...ile=Xsd2xml.cs
However, xsd.exe supports only a subset of W3C XML Schema. For example,
lists (as elements) are not well supported.

-Dino
"Paw Pedersen" <ne**@paws.dk> wrote in message
news:Op**************@TK2MSFTNGP11.phx.gbl...
Is it possible to load a XSD and loop throw the nodes and attributes that
you whant filled out, and then generate a XML instance with the trees and
data that you have filled data in?
And how do you do that?

Regards Paw

Nov 12 '05 #2
What I really whant, is the following:
I receive a xml instance and load it into XmlDocument. I have a XSD
specifying the full XML for the xml instance I have received. I now want to
iterate throw the xsd tree with a foreach-node/foreach-attribut the same way
as I can do it with my XmlDocument instance. I need this because I need to
do some comparison with the xml instance and the xsd and maybe fill in some
"blank" values in the xml instance (not just a simple validate againt XSD).
The XSD can be severel different XSD's and therefore I can't use the
"xsd.exe /c".
Hope you have a good idea on how to do this. I don't know much about working
with XSD in .Net.

Regards Paw

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:#$**************@TK2MSFTNGP12.phx.gbl...
what do you *really* want to do?

do you want a generic xsd-to-xml generator? Something that can contrive XML documents from any general XSD?
or is there a particular XSD you want to generate for?

one approach I have used is to use "xsd.exe /c" to generate classes, then
just instantiate the classes. Serialization of an instance of said class
produces an XML document that conforms to the XSD. example:

http://www.winisp.net/cheeso/srcview...ile=Xsd2xml.cs

However, xsd.exe supports only a subset of W3C XML Schema. For example,
lists (as elements) are not well supported.

-Dino
"Paw Pedersen" <ne**@paws.dk> wrote in message
news:Op**************@TK2MSFTNGP11.phx.gbl...
Is it possible to load a XSD and loop throw the nodes and attributes that you whant filled out, and then generate a XML instance with the trees and data that you have filled data in?
And how do you do that?

Regards Paw


Nov 12 '05 #3
Read in the Schema and walk through it. One way to do it: build a map in
memory of required and optional fields, and then walk the XML DOM to
determine which elements are present or not present. It sounds messy and
tedious.

see this for a hint:
http://www.fawcette.com/xmlmag/2002_...hlin_09_03_02/
The XSD can be severel different XSD's and therefore I can't use the
"xsd.exe /c".
The possible use of multiple XSDs does not prevent you from using xsd.exe.
For example, you could have 3 or 4 (or more) sets of classes derived from 3
or 4 (or more) XSDs. Then you get can object programming semantics rather
than XML parsing.

Example, you de-serialize the XML file into an instance of Class1, then
examine the fields in that instance of Class1 to see if they have been set.

// Class1 is generated from xsd.exe /c (From a given XSD)
XmlSerializer s= new XmlSerializer(typeof(Class1));
Class1 c= s.Deserialize(thefile);
if (c.Field1==null) c.Field1= Field1DefaultValue;
//etc

Actually, by modifying the generated source for Class1, you could insert
"default" values in the instances by modifying the default ctor. These
"default" values would be over-written when de-serialiing a document that
actually has values for them. Going with this approach, you wouldn't need
to

if (c.Field1==null) c.Field1= Field1DefaultValue;
//etc

....because all that would be done implicitly. On the other hand if you have
more complex rules for which fields need to be set (eg, relationships
between one field and another), then this tweak may not work. but xml
serialization still may be a nice simplification.

Though I agree, XML serialization (and xsd.exe) does not work if there is an
*infinite* number of possible XSDs.

-D
"Paw Pedersen" <ne**@paws.dk> wrote in message
news:uF**************@TK2MSFTNGP15.phx.gbl... What I really whant, is the following:
I receive a xml instance and load it into XmlDocument. I have a XSD
specifying the full XML for the xml instance I have received. I now want
to
iterate throw the xsd tree with a foreach-node/foreach-attribut the same
way
as I can do it with my XmlDocument instance. I need this because I need to
do some comparison with the xml instance and the xsd and maybe fill in
some
"blank" values in the xml instance (not just a simple validate againt
XSD).
Hope you have a good idea on how to do this. I don't know much about
working
with XSD in .Net.

Regards Paw

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:#$**************@TK2MSFTNGP12.phx.gbl...
what do you *really* want to do?

do you want a generic xsd-to-xml generator? Something that can contrive

XML
documents from any general XSD?
or is there a particular XSD you want to generate for?

one approach I have used is to use "xsd.exe /c" to generate classes, then
just instantiate the classes. Serialization of an instance of said class
produces an XML document that conforms to the XSD. example:

http://www.winisp.net/cheeso/srcview...ile=Xsd2xml.cs


However, xsd.exe supports only a subset of W3C XML Schema. For example,
lists (as elements) are not well supported.

-Dino
"Paw Pedersen" <ne**@paws.dk> wrote in message
news:Op**************@TK2MSFTNGP11.phx.gbl...
> Is it possible to load a XSD and loop throw the nodes and attributes that > you whant filled out, and then generate a XML instance with the trees and > data that you have filled data in?
> And how do you do that?
>
> Regards Paw
>
>



Nov 12 '05 #4
Thanks for your input.
I need the class to be dynamical so it can load any XSD without privious
knowledge, and therefore I can't use the "xsd.exe /c".
Also the "default" values are dependent on the xml instance and therefor I
can't use the "default" values from the XSD.
Basically I loop throw the xml instance I have received, and for every node
with one or more attributes filled out, I need to check in the xsd if there
exist more attributes to this specific node, and if there does, I need to
fill them with a blank value in the xml instance.
I have found out how to load the XSD ind a XmlSchema class and I have the
xpath to the specific node, for which I need to check if it has any
attributes. I just don't know how to "navigate" throw a XmlSchem instance,
and get the list of attribute names this specific node has.
Any ideas?

Regards Paw

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:OG**************@TK2MSFTNGP09.phx.gbl...
Read in the Schema and walk through it. One way to do it: build a map in
memory of required and optional fields, and then walk the XML DOM to
determine which elements are present or not present. It sounds messy and
tedious.

see this for a hint:
http://www.fawcette.com/xmlmag/2002_...hlin_09_03_02/
The XSD can be severel different XSD's and therefore I can't use the
"xsd.exe /c".
The possible use of multiple XSDs does not prevent you from using xsd.exe.
For example, you could have 3 or 4 (or more) sets of classes derived from

3 or 4 (or more) XSDs. Then you get can object programming semantics rather than XML parsing.

Example, you de-serialize the XML file into an instance of Class1, then
examine the fields in that instance of Class1 to see if they have been set.
// Class1 is generated from xsd.exe /c (From a given XSD)
XmlSerializer s= new XmlSerializer(typeof(Class1));
Class1 c= s.Deserialize(thefile);
if (c.Field1==null) c.Field1= Field1DefaultValue;
//etc

Actually, by modifying the generated source for Class1, you could insert
"default" values in the instances by modifying the default ctor. These
"default" values would be over-written when de-serialiing a document that
actually has values for them. Going with this approach, you wouldn't need
to

if (c.Field1==null) c.Field1= Field1DefaultValue;
//etc

...because all that would be done implicitly. On the other hand if you have more complex rules for which fields need to be set (eg, relationships
between one field and another), then this tweak may not work. but xml
serialization still may be a nice simplification.

Though I agree, XML serialization (and xsd.exe) does not work if there is an *infinite* number of possible XSDs.

-D
"Paw Pedersen" <ne**@paws.dk> wrote in message
news:uF**************@TK2MSFTNGP15.phx.gbl...
What I really whant, is the following:
I receive a xml instance and load it into XmlDocument. I have a XSD
specifying the full XML for the xml instance I have received. I now want
to
iterate throw the xsd tree with a foreach-node/foreach-attribut the same
way
as I can do it with my XmlDocument instance. I need this because I need to
do some comparison with the xml instance and the xsd and maybe fill in
some
"blank" values in the xml instance (not just a simple validate againt
XSD).
Hope you have a good idea on how to do this. I don't know much about
working
with XSD in .Net.

Regards Paw

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:#$**************@TK2MSFTNGP12.phx.gbl...
what do you *really* want to do?

do you want a generic xsd-to-xml generator? Something that can contrive
XML
documents from any general XSD?
or is there a particular XSD you want to generate for?

one approach I have used is to use "xsd.exe /c" to generate classes,

then just instantiate the classes. Serialization of an instance of said class produces an XML document that conforms to the XSD. example:

http://www.winisp.net/cheeso/srcview...ile=Xsd2xml.cs


However, xsd.exe supports only a subset of W3C XML Schema. For example, lists (as elements) are not well supported.

-Dino
"Paw Pedersen" <ne**@paws.dk> wrote in message
news:Op**************@TK2MSFTNGP11.phx.gbl...
> Is it possible to load a XSD and loop throw the nodes and attributes

that
> you whant filled out, and then generate a XML instance with the trees

and
> data that you have filled data in?
> And how do you do that?
>
> Regards Paw
>
>



Nov 12 '05 #5
I have reposted this question since it kind of a new subject :o)
Reposted with subject: "Get attributes from XmlSchema for specific Node
referenced by Xpath"

Regards Paw

"Paw Pedersen" <ne**@paws.dk> wrote in message
news:Ow**************@TK2MSFTNGP12.phx.gbl...
Thanks for your input.
I need the class to be dynamical so it can load any XSD without privious
knowledge, and therefore I can't use the "xsd.exe /c".
Also the "default" values are dependent on the xml instance and therefor I
can't use the "default" values from the XSD.
Basically I loop throw the xml instance I have received, and for every node with one or more attributes filled out, I need to check in the xsd if there exist more attributes to this specific node, and if there does, I need to
fill them with a blank value in the xml instance.
I have found out how to load the XSD ind a XmlSchema class and I have the
xpath to the specific node, for which I need to check if it has any
attributes. I just don't know how to "navigate" throw a XmlSchem instance,
and get the list of attribute names this specific node has.
Any ideas?

Regards Paw

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:OG**************@TK2MSFTNGP09.phx.gbl...
Read in the Schema and walk through it. One way to do it: build a map in
memory of required and optional fields, and then walk the XML DOM to
determine which elements are present or not present. It sounds messy and tedious.

see this for a hint:
http://www.fawcette.com/xmlmag/2002_...hlin_09_03_02/
The XSD can be severel different XSD's and therefore I can't use the
"xsd.exe /c".
The possible use of multiple XSDs does not prevent you from using xsd.exe. For example, you could have 3 or 4 (or more) sets of classes derived from 3
or 4 (or more) XSDs. Then you get can object programming semantics rather
than XML parsing.

Example, you de-serialize the XML file into an instance of Class1, then
examine the fields in that instance of Class1 to see if they have been

set.

// Class1 is generated from xsd.exe /c (From a given XSD)
XmlSerializer s= new XmlSerializer(typeof(Class1));
Class1 c= s.Deserialize(thefile);
if (c.Field1==null) c.Field1= Field1DefaultValue;
//etc

Actually, by modifying the generated source for Class1, you could insert
"default" values in the instances by modifying the default ctor. These
"default" values would be over-written when de-serialiing a document

that actually has values for them. Going with this approach, you wouldn't need to

if (c.Field1==null) c.Field1= Field1DefaultValue;
//etc

...because all that would be done implicitly. On the other hand if you

have
more complex rules for which fields need to be set (eg, relationships
between one field and another), then this tweak may not work. but xml
serialization still may be a nice simplification.

Though I agree, XML serialization (and xsd.exe) does not work if there

is an
*infinite* number of possible XSDs.

-D
"Paw Pedersen" <ne**@paws.dk> wrote in message
news:uF**************@TK2MSFTNGP15.phx.gbl...
What I really whant, is the following:
I receive a xml instance and load it into XmlDocument. I have a XSD
specifying the full XML for the xml instance I have received. I now want to
iterate throw the xsd tree with a foreach-node/foreach-attribut the same way
as I can do it with my XmlDocument instance. I need this because I need
to do some comparison with the xml instance and the xsd and maybe fill in
some
"blank" values in the xml instance (not just a simple validate againt
XSD).
Hope you have a good idea on how to do this. I don't know much about
working
with XSD in .Net.

Regards Paw

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in
message news:#$**************@TK2MSFTNGP12.phx.gbl...
> what do you *really* want to do?
>
> do you want a generic xsd-to-xml generator? Something that can

contrive XML
> documents from any general XSD?
> or is there a particular XSD you want to generate for?
>
> one approach I have used is to use "xsd.exe /c" to generate classes, then> just instantiate the classes. Serialization of an instance of said class> produces an XML document that conforms to the XSD. example:
>
>

http://www.winisp.net/cheeso/srcview...ile=Xsd2xml.cs
>
>
> However, xsd.exe supports only a subset of W3C XML Schema. For example,> lists (as elements) are not well supported.
>
> -Dino
>
>
> "Paw Pedersen" <ne**@paws.dk> wrote in message
> news:Op**************@TK2MSFTNGP11.phx.gbl...
> > Is it possible to load a XSD and loop throw the nodes and attributes that
> > you whant filled out, and then generate a XML instance with the trees and
> > data that you have filled data in?
> > And how do you do that?
> >
> > Regards Paw
> >
> >
>
>



Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by obsidian8 | last post: by
3 posts views Thread by takarimasu | last post: by
3 posts views Thread by sloesch | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.