Fun with xsd.exe
So
we’re starting to do a rewrite of our music management software, using
whidbey. We wanted to make use of the XmlHttpRequest object to do a lot of asynchronous javascript calls, like you’ve
seen on Google maps, or the
search at www.forgetfoo.com
I have our database tables mapped into c# classes,
so that I can easily get the data from the database, but I needed to convert
those objects into an xml form to send to the browser. I had some sample
xml files that I wanted the resulting xml to look like, but I wans’t sure of the
best way to transform my objects into the appropriate xml
representation.
I’m not
just converting simple database rows into an xml file, which would be really
easy using dataset. I want to convert related data, one to many and one to
one relationships. I knew that there is built in support of using an .xsd
file to convert classes and xml, but I had never used xsd.exe, and furthermore,
I didn’t have an .xsd file, just .xml files.
Just to see what xsd.exe offered, I checked out the
command line options, and low and behold, you can give it an .xml document, and
it will create the corresponding .xsd file! (This is probably obvious to most,
but like I said, I had never used xsd.exe). I already knew that I could use
xsd.exe to create a c# class file from an .xsd file, so I converted one of my
sample xml files into and .xsd file, and then created a corresponding c# class
file.
Now, all I have to do is map my database table
classes into the generated xsd class file, and the I think I can use the
XmlSerializer to transform that class into the xml document that I’m trying to
create! We’ll see how it goes, I’ll keep you
posted.
Anonymous said,
Wrote on February 11, 2005 @ 6:46 am
If you ever have xsd.exe choke while generating your schema because it can’t handle something in your xml I’ve found that XmlSpy can usually do the trick (if you have access to it). I’ve had xsd.exe complain about some of our companies xml before and had to go this route (the xml was valid), but overall xsd.exe is very powerful.
Anonymous said,
Wrote on February 11, 2005 @ 7:46 am
Thanks for the tip, I know that the error messages you get when dealing with serialization can be aa bear sometimes, its nice to know stuff like this in advance!
Anonymous said,
Wrote on February 12, 2005 @ 2:57 am
The Xml Tools Team page on GotDotNet http://www.gotdotnet.com/team/xmltools/ has some other interesting XML tools you might like to check out. As far as I know the XSD - inferencing code in XSD.exe is based on work the dataset team had to do to infer the schema of datasets. If XSD.exe has trouble infering the schema (as Larry above noted it sometimes does) you might want to try the XSD inference tool.
Anonymous said,
Wrote on February 12, 2005 @ 4:00 pm
Joseph, that tool worked like a charm! Larry was right, the code produced by xsd.exe needed some manual modifications by me in order to make it work, but with infer.exe, the schema is more accurate and then I use xsd.exe to make a class file from that schema, and it worked out of the box! awesome!
breichelt said,
Wrote on March 1, 2005 @ 7:17 pm