all groups > dotnet xml > may 2006 >
You're in the

dotnet xml

group:

Code Generator that writes code to generate an XML doc structure?


Code Generator that writes code to generate an XML doc structure? joshblair
5/25/2006 3:14:54 PM
dotnet xml:
Hello,

Has anyone ever seen or created such a code generator?

I'm looking for a sample of a code generator that will generate code
(preferably one that uses C# and the XMLTextWriter) to create an XML
document structure based on an XML file as input.

I have to build some classes that allow me to generate some very
complex/large/nasty XML documents for use in B2B exchange of data (like
invoices, orders, etc.). A third party has dictated the structure and
provided example XML documents. Instead of hand coding these classes,
I'd like to be able to automatically generate them by using these
example XML documents as input. This seems like a fairly easy task but
I haven't run across a demonstration of this yet.

This code generator would walk the elements/nodes, checking for
attributes and other child nodes and output the code necessary to
generate the same XML document structure. See below for an example:

An Example XML file (contents):
===============================

<myRootNode>
<myChildNode1>
<myChildNode2 myAttribute1="test" myAttribute2="test2"/>
. . .
</myChildNode1>
.. . .
</myRootNode>

Example output from the code generator that takes the path of the xml
file above (this is merely a simple example):
==================================================

// instantiate XmlTextWriter over file stream using UTF-8
XmlTextWriter tw = new XmlTextWriter(fileName, Encoding.UTF8);

// specify serialization details
tw.Formatting = Formatting.Indented;
tw.Indentation = 8;
tw.QuoteChar = '\"';

// No need for a start element
//tw.WriteStartDocument();

tw.WriteStartElement("myRootNode");

tw.WriteStartElement("myChildNode1");

tw.WriteStartElement("myChildNode2");

tw.WriteAttributeString("myAttribute1", "test");
tw.WriteAttributeString("myAttribute2", "test2");

tw.WriteEndElement(); // myChildNode2

tw.WriteEndElement(); // myChildNode1

tw.WriteEndElement(); // myRootNode

// No need for a start element
//tw.WriteEndDocument();

// close the stream
tw.Close();

Thanks for your time and input,

Josh Blair
Evergreen, CO
Re: Code Generator that writes code to generate an XML doc structure? Dhanvanth
5/25/2006 3:40:24 PM
This would be easier to do if you have a corresponding schema document
for each xml file. You can then combine XmlSerializer or XmlTextWriter
to generate the Xml documents based on the schema.
Re: Code Generator that writes code to generate an XML doc structure? joshblair
5/25/2006 3:51:36 PM
Dhanvanth,

Thanks for the reply. I was given the formats by a third party and
these documents are in a proprietary xml structure without a schema or
DTD. These XML document structures represent business entities like
invoices, orders, ASNs, etc. Knowing this, would you recommend that I
create a schema for these documents? They are very large and complex
and I currently don't speak XSL/XSD/XSLT, etc. I'd be willing to work
on implementing these techniques if this is the best way. Please let
me know your thoughts.

Josh Blair
Evergreen, CO
Re: Code Generator that writes code to generate an XML doc structure? Dhanvanth
5/25/2006 7:03:09 PM
Creating Schema files have their advantages in that we can always be
sure that the format is correct(produce valid XML). But they involve a
lot of time in design.

If each document is huge then producing them manually will take a lot
of code and hard to maintain. Any change in the structure will
typically render all your previous code worthless.

Schemas will help in creating valid objects out of them and you can
easily create valid Xml using a schema file. Also there are tools now
available that will generate schema files from XML documents. You could
try one such at http://www.hitsw.com/xml_utilites/. Remember that they
are only as good as how well the input sample represents your document
structure.

On the other hand, hard coding the production of XML will solve the
problem and be a pretty decent solution if the input formats are pretty
constant and do not change( I have never seen that happen though :-) ).
Also they can be coded fairly easily.

I am not sure of your exact requirements. So I cant advice as to what
to choose. Hope I was of some help

-Dhanvanth


[quoted text, click to view]
AddThis Social Bookmark Button