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