Search code examples
c#xmlsql-server-2005.net-3.5for-xml

Writing the results of a FOR XML query to a file with C#.NET


I'm trying to write the result of a FOR XML PATH query to a file. I can generate the file, but it doesn't contain the results of the query. Any one know where i'm going wrong?

private static void GetChartData(string OC_Ttl1, string OC_Ttl2, string OC_OL31)
    {

        //Prepare Connection Variables
        SqlConnection conn_Org = new SqlConnection();
        SqlCommand cmd_Org = new SqlCommand();



        //Open Connection
        conn_Org.ConnectionString = Set_OrgChartConn();
        conn_Org.Open();

        //Execute Procedure
        cmd_Org.Connection = conn_Org;
        cmd_Org.CommandText = "dbo.usp_CreateOrgDataSet";
        cmd_Org.CommandType = CommandType.StoredProcedure;
        cmd_Org.Parameters.AddWithValue("@OC_Ttl_1", OC_Ttl1);
        cmd_Org.Parameters.AddWithValue("@OC_Ttl_2", OC_Ttl2);
        cmd_Org.Parameters.AddWithValue("@OC_OL3_1", OC_OL31);




        DataSet myDataSet = new DataSet();
        myDataSet.ReadXml(cmd_Org.ExecuteXmlReader(), XmlReadMode.Fragment);
        myDataSet.WriteXml("myData.xml");

        conn_Org.Close();






    }

THis is the contents of the xml file that is generated with the above code.

<?xml version="1.0" standalone="yes"?>


Solution

  • Try this:

    myDataSet.ReadXml(cmd_Org.ExecuteXmlReader(), XmlReadMode.Auto);