Search code examples
c#.netxmlfor-xml

Writing XML to memory instead of disk


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