Writing XML to memory instead of disk

How do I start writing the results of the FOR XML PATH stored procedure to memory and not to a file on disk?

The current way of doing things is:

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");
}

      

Rather than writing a file to disk where other methods work, I want this method to return xml in memeory.

I guess it will be much faster than writing to disk ....

+2


source to share


3 answers


You have XmlReader

- you can do all kinds of things. for example



XmlDocument doc = new XmlDocument();
using(XmlReader xr = cmd_Org.ExecuteXmlReader()) {
    doc.Load(xr);
}
// TODO: play with "doc" with xpath etc

      

+4


source


DataSet.WriteXml

has a lot of overloads - you can write to stream (including MemoryStream

) and writer (including StringWriter

). Assuming you really want to get through DataSet

- if you're happy to get XML directly without using DataSet

, then use Marc's suggestions.



+1


source


You can call an overload WriteXml

that writes to the stream and use MemoryStream

, though you can mean you want the XML to be a big long string.

+1


source







All Articles