Writing XML to memory instead of disk
Question
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....
Solution
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
OTHER TIPS
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.
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.