Custom XML Writer

Posted on 2005-04-26
Last Modified: 2012-05-05

I need to generate an XML file based on data in an ADO.NET DataSet.  I realise the DataSet object has a simple WriteXml() method but I need the XML in a very specific structure.

My question is this:  Is it completely wrong or out of the ordinary to write a custom XML Writing method..?  I mean, for example, a method that iterates through all the data in a DataSet, one row at a time, and writes it to a TextStream with the tags and elements exactly as they are required..?

Is there another way..?

Question by:TDSnet
    LVL 15

    Expert Comment

    i think the easiest way serizlize DataSet using WriteXml() and then apply XSLT transformation to create the structure you want.

    Certanly you can write your custom method to serialize DataSet in yours format, if you have really problems with XSLT.

    But i think XSLT will be easier to develop
    LVL 23

    Accepted Solution

    it depends, if the data is large then you are better off using the XmlTextWriter and write the data thru this object. It is lighter and performs faster than getting the Xml through the WriteXml() method  and then performing element transformations through Xslt.

    If you do not wish to recompile your application every time there is a structural change in your Xml Requirements, then you should go ahead with Xslt on the proviso that the data is not large (say not more than 500 rows of say 5 columns: it is admittedly a subjective field. But with MSXML.NET, anything about 200KB can be dreadfully slow in transforming as contrasted to using the XmlTextWriter.

    If you want it to be performant-oriented due to the largeness or potential largeness of the data, then the only way to go is the use of the XmlTextWriter.

    I'd say in most cases where the data is not large, XSLT is the way to go because it affords flexibility in making changes. However, if performance is dreadful or unacceptable then, you must use the XmlTextWriter.
    LVL 5

    Expert Comment

    XSL is easier but way too slow.  Just overload the WriteXML and make your own.  Use XMLTextWriter for fastest access or XMLDocument if you want to save some coding and sacrifice some speed.

    You may end up writing a serializer and a deserializer if you need to bring your data back in.  This is not uncommon at all, I usually end up writing several custom serializers in an application.
    LVL 6

    Author Comment

    Okay thanks guys, I'm going with the XmlTextWriter.

    b1xml2 gets the poinx.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now