tags:

views:

125

answers:

3

How do i go about writing the results of a FOR XML PATH stored procedure into memory rather than a file on disk?

Current way of doing things:

private  void GetChartData(string OC_Ttl1, string OC_Ttl2, string OC_OL31)
{
    OC_Ttl_1 = OC_Ttl1;
    OC_Ttl_2 = OC_Ttl2;
    OC_OL3_1 = OC_OL31;
    //Output xml
    DataSet orgDataSet = new DataSet();
    orgDataSet.ReadXml(cmd_Org.ExecuteXmlReader(), XmlReadMode.Auto);
    orgDataSet.WriteXml("InputXMLFiles/" + OC_OL3_1.Replace(" ", 
        "_").Replace("/", "-") + ".xml");
}

Instead of writing the file to disk that other methods then operate on, i want to have this method return the xml to memeory.

I assume that this would be far faster than writing to disk....

+4  A: 

You have an XmlReader - you can do all sorts of things. For example

XmlDocument doc = new XmlDocument();
using(XmlReader xr = cmd_Org.ExecuteXmlReader()) {
    doc.Load(xr);
}
// TODO: play with "doc" with xpath etc
Marc Gravell
Thanks Marc. I'm not sure how that helps in my current situation. Be gentle, i'm a .Net novice.....
Doozer1979
Sorry for delay; I was on the train. An `XmlReader` is essentially a "pipe" to the incoming xml. By loading this into something like `XmlDocument`, you get access to the full DOM object model, and can investigate the xml. If not - please clarify what you want to do with the data?
Marc Gravell
Thanks Marc. AT the moment the XML data gets written to disk, and then another method is called which performs an XSLCompiledTransform on the data to turn it from a flat xml document into a hierarchical document. That data is then written to an xml file on disk.Another method is then called to do another Xsl transform which outputs the xml to html.Apologies for the delay in getting back, girlfriends birthday yesterday.
Doozer1979
+1  A: 

DataSet.WriteXml has lots of overloads - you can write to a stream (including a MemoryStream) and a writer (including a StringWriter). That's assuming you really want to go via a DataSet - if you're happy getting the XML directly without using a DataSet then just use Marc's suggestions.

Jon Skeet
+1  A: 

You can call the overload of WriteXml that writes to a stream, and use a MemoryStream, although you may mean that you want the XML as a big long string.

MusiGenesis