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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joshblair

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

    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="t est" myAttribute2="t est2"/>
    . . .
    </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(f ileName, Encoding.UTF8);

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

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

    tw.WriteStartEl ement("myRootNo de");

    tw.WriteStartEl ement("myChildN ode1");

    tw.WriteStartEl ement("myChildN ode2");

    tw.WriteAttribu teString("myAtt ribute1", "test");
    tw.WriteAttribu teString("myAtt ribute2", "test2");

    tw.WriteEndElem ent(); // myChildNode2

    tw.WriteEndElem ent(); // myChildNode1

    tw.WriteEndElem ent(); // myRootNode

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

    // close the stream
    tw.Close();

    Thanks for your time and input,

    Josh Blair
    Evergreen, CO

  • Dhanvanth

    #2
    Re: Code Generator that writes code to generate an XML doc structure?

    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.

    Comment

    • joshblair

      #3
      Re: Code Generator that writes code to generate an XML doc structure?

      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

      Comment

      • Dhanvanth

        #4
        Re: Code Generator that writes code to generate an XML doc structure?

        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


        joshblair wrote:[color=blue]
        > 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[/color]

        Comment

        Working...