Eric J. Smith's Weblog

CodeSmith, .NET, etc...

CodeSmith and XML

Stefan Cullmann points out CodeSmith's current lack of good XML support.  I agree completely and dramatically improving XML support in CodeSmith is very high on my priority list so I decided to take this opportunity to let you all know what I am planning to do and see what you think.

Currently, CodeSmith only supports XML serializable objects.  Basically, you create an object that is XML serializable, create a property of this type in your template, CodeSmith then allows you to select an XML file which will be deserialized into an instance of your object, and you can then proceed to use the information provided in this object in a fully object oriented and strongly typed fashion.  Even though you can easily use XSD.exe to generate this object for you based on either an XSD or infered from an XML instance document, it is still a bit of a pain to setup and needs to be updated as you decide to add more information to your XML document.

I am planning to add 2 additional ways of supporting XML in CodeSmith.  First, I plan to put together a simple designer that will allow you to select any XML file and CodeSmith will load this XML for you into an XmlDocument object and you will be able to traverse the information using the XML DOM.

Secondly, I plan to implement my own version of XSD.exe that will be embedded into the CodeSmith engine and will take any XSD and dynamically create a strongly typed object model for it on the fly.  This will basically give you the best of both worlds.  You will be able to use XML as an input source and you will also be able to write your templates against a strongly typed object model.  Here is a little sample of what this might look like:

<%@ XmlProperty Name="MyOrder" Schema="MyOrderSchema.xsd" %>
<% foreach (DetailLine detailLine in MyOrder.DetailLines) { %>
ProductID: <%= detailLine.ProductID %> Quantity: <%= detailLine.Quantity %>
<% } %>

I'm interested to know what you guys think of this so feel free to comment on this post and let me know.

Posted: Mar 15 2004, 06:18 PM by ejsmith | with 3 comment(s)
Filed under:

Comments

Jason Goodwin said:

There is also a nice middle ground between those two taking the same path that typed DataSet's did. Namely, making typed XmlDocuments. I think it would hit the sweet spot between those that need xml-document semantics and those that want typed getters.

I first saw mention of it here: http://hyperthink.net/blog/PermaLink,guid,eeee6c2a-36c6-40a1-8e63-b85b9d1a0776.aspx

I can't find the original article's site (It's down or my ISP sucks), but a search on 'Ted Neward "strongly typed" XmlDocument' will turn up a lot of related stuff.

It looks like they are shying away form XmlDocument in the next framework release though, so you might want to keep that in mind. (I read in a short blurb XmlDocument will likely be outclassed by XmlPathDocument, if you don't mind the pun.)
# March 15, 2004 9:59 PM

Edris Hasani Nasab said:


http://ncodegen.sf.net/templates.html
(XML Manipulation)

Regards
# March 21, 2004 2:14 AM

adam chester said:

You might even use xsd.exe, i thought of doing the same thing you mention, creating my own xsd replacement, however to speed things up, I created a class that wraps the xsd.exe tool, and generates a class from the xsd-generated class that uses properties instead of fields. This way I could still use the property grid. IIRC, part of my problem was that reflection doesn't allow enough access to metadata about custom attributes, but I think I hacked around it.
# April 24, 2004 10:11 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)