tags:

views:

92

answers:

4

What's the ideal way of generating XML without creating and saving a file?

I'm thinking of using an ASP.NET page with code behind to generate the markup as XML.

Is this possible? Or would you have an alternative way?

I have a flash component that reads an XML file and I need to dynamically generate this file. I don't have write permission so I won't have the ability to create and save a file.

I was thinking of having an application page that grabs the data and provide property methods to generate the xml on the Settings.xml.aspx page with Settings.xml.aspx.cs codebehind.

Thanks.

+2  A: 

System.Xml.XmlDocument ?

ybo
+1  A: 

Yes, it is perfectly feasible to generate XML "on the fly". Take a look at the XmlDocument class. And more info here.

Steve Haigh
+2  A: 

In fact there are many ways to do it. It all depends on your needs. Maybe you could have a look at some examples of XDocument (or XmlDocument in .NET 2.0) and XmlWriter, none of these require you to save the XML to a file. You can either keep the object model in memory when using XDocument or write to a MemoryStream when using XmlWriter:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
    using (XmlWriter writer = XmlWriter.Create (stream, settings))
    {
      writer.WriteStartElement ("customer");
      writer.WriteElementString ("firstname", "Jim");
      writer.WriteElementString ("lastname"," Bo");
      writer.WriteEndElement();
    }
    // do further processing with the stream
}

The difference between the two is basically that the first one gives you access to the DOM whereas the second one simply writes out XML to an underlying stream.

Unfortunately, without knowing more details this question can only be answered vaguely.

0xA3
basically my Flash component is referencing this xml, and I need the xml to be dynamic. I was thinking of having an application page, and then generating the xml that way as Settings.xml.aspx
LB
XmlWriter is iirc the fastest way to do this performance wise
annakata
Yes, this is true, XmlWriter is fastest and needs the least memory overhead. However, it's a write once approach which does not allow further processing. XmlWriter basically wraps write operations on a stream.
0xA3
+4  A: 

The easiest way is to use System.Xml.XmlDocument or System.Xml.Linq.XDocument to build up the document. Both can be streamed out to the Response.OutputStream.

The smoothest approach (especially if you turn off buffering) is simply to create an XmlTextWriter round the Response.OutputStream. This is a forward only approach to generate XML but if the output is large it means you need less memory and content starts to arrive at the client earlier.

AnthonyWJones