More on xsd.exe
I recently posted about serializing my objects against a schema
(xsd). I wanted to get an object from the database, and then convert it and its
related data into xml format so that I could send it down to javascript
requests.
(xsd). I wanted to get an object from the database, and then convert it and its
related data into xml format so that I could send it down to javascript
requests.
I wanted to give an update of how this works. I initially was using xsd.exe
to create and .xsd file from my sample xml documents, then from that .xsd file
you can create a class file that will serialize to xml conforming to schema
definition. Joseph Cooney commented in the other post that xsd.exe doesn’t
always infer the schema correctly, and I was noticing this as well; I couldn’t
use the generated class files to serialize my data, I had to manually modify
them. Joseph pointed me to the infer.exe
tool that resides at GotDotNet that does a better job of inferring xml
schemas from an xml document, you can even give it multiple sample xml files so
that it can get a good sampling.
to create and .xsd file from my sample xml documents, then from that .xsd file
you can create a class file that will serialize to xml conforming to schema
definition. Joseph Cooney commented in the other post that xsd.exe doesn’t
always infer the schema correctly, and I was noticing this as well; I couldn’t
use the generated class files to serialize my data, I had to manually modify
them. Joseph pointed me to the infer.exe
tool that resides at GotDotNet that does a better job of inferring xml
schemas from an xml document, you can even give it multiple sample xml files so
that it can get a good sampling.
So now using infer.exe I create my .xsd file, and I still use xsd.exe to
create my class files. These class files are now higher quality and can be used
“out of the box.” Now all I do is write a little method that converts my data
object into the generated class, and then I use the XmlSerializer to serialize
that object into the appropriate xml.
create my class files. These class files are now higher quality and can be used
“out of the box.” Now all I do is write a little method that converts my data
object into the generated class, and then I use the XmlSerializer to serialize
that object into the appropriate xml.
I really like this style because making changes is relatively straight
forward. I simply edit my sample xml file(s) , re-create the .xsd file,
re-create the c# class file, modify the conversion method, and we’re all
set! (It sounds like a lot, but its really simple, and you can script some
of it.) What I like about it is that since the class file has been
generated using a built in .net tool, I haven’t gotten any serialization errors;
I also like the fact that the sample xml files, and the .xsd files are versioned
right along with the source code, which is nice.
forward. I simply edit my sample xml file(s) , re-create the .xsd file,
re-create the c# class file, modify the conversion method, and we’re all
set! (It sounds like a lot, but its really simple, and you can script some
of it.) What I like about it is that since the class file has been
generated using a built in .net tool, I haven’t gotten any serialization errors;
I also like the fact that the sample xml files, and the .xsd files are versioned
right along with the source code, which is nice.
Anonymous said,
Wrote on February 19, 2005 @ 5:14 pm
This is pretty interesting. Have you ever been able to templatize the code that XSD generates? I’ve always wanted to do that, but have been too lazy to look into it.
Anonymous said,
Wrote on February 19, 2005 @ 5:30 pm
Hi Brendan - I’m curious as to what you mean by "templatize". Care to elaborate?
Anonymous said,
Wrote on February 19, 2005 @ 9:27 pm
I think he means that you supply a "template" .cs file for the code generator to use as a template file. Its similar to how Brendan uses template files for his collections classes, I can’t find the link to his post about it, but I think thats what hes talking about. And, no I haven’t tried using a template file at all, this is my first foray into generating code from an .xsd
Anonymous said,
Wrote on February 20, 2005 @ 4:53 am
XSD.exe uses the codedom internally to do it’s code generation (generating typed datasets and classes from .xsd files) so there is not much scope for using templates if you want to leverage that. You could always write your own, or use XSLT.
Anonymous said,
Wrote on February 20, 2005 @ 12:41 pm
There’s an old thread somewhere about doing this, the question popped up in a discussion on typed datasets vs business entities. I’ll see if I can dig it up. It’d be nice to be able to teak the code that it generates, to add custom business rules, etc.
Anonymous said,
Wrote on February 20, 2005 @ 3:37 pm
Is this the thread you were talking about Brendan: http://www.codebetter.com/blogs/brendan.tompkins/archive/2004/05/04/12748.aspx
Anonymous said,
Wrote on February 20, 2005 @ 3:43 pm
You might also want to look at this article from Daniel Cazzulino http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/xsdcodegen.asp
Anonymous said,
Wrote on August 1, 2005 @ 1:33 am
Thanks guys!
can’t believe it was this easy finding information on exactly the topic i was needing.
all respect to bloggers!
Mirity said,
Wrote on March 29, 2007 @ 7:34 am
Where can I find your code?
I need it importunately!