Load XML file From Sproc

Last post 05-15-2008 8:40 PM by MJ20. 13 replies.

Sort Posts:

  • Load XML file From Sproc

    05-08-2008, 5:02 PM
    • Loading...
    • MJ20
    • Joined on 01-12-2008, 2:21 PM
    • Posts 18

    Hi. I know how to load a GridView from an xml file that already has data in it. How do you load data from a stored procedure into the xml file?

    Thanks Tongue Tied

  • Re: Load XML file From Sproc

    05-08-2008, 5:54 PM
    Answer
    • Loading...
    • jamesqua
    • Joined on 08-03-2004, 6:55 PM
    • Columbus, OH
    • Posts 959

     Do you mean how do you call a stored procedure and receive xml?

    You can use the "for xml auto" clause

     
    http://msdn.microsoft.com/en-us/library/ms345137.aspx
     

  • Re: Load XML file From Sproc

    05-08-2008, 8:58 PM
    • Loading...
    • MJ20
    • Joined on 01-12-2008, 2:21 PM
    • Posts 18

    Thank you! That's really cool. The only thing is... how do I use it to populate my xml file in my application?

  • Re: Load XML file From Sproc

    05-08-2008, 9:30 PM
    Answer
    • Loading...
    • jamesqua
    • Joined on 08-03-2004, 6:55 PM
    • Columbus, OH
    • Posts 959

    For xml auto returns the entire record as one big string.  You just need to use the LoadXml property of the XmlDocument class and pass the string as an argument like

    objXmlDocument.LoadXml(databaseScalarResult);

  • Re: Load XML file From Sproc

    05-08-2008, 10:23 PM
    • Loading...
    • MJ20
    • Joined on 01-12-2008, 2:21 PM
    • Posts 18

    How do you include the following in the query?

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

  • Re: Load XML file From Sproc

    05-09-2008, 1:19 AM
    • Loading...
    • jamesqua
    • Joined on 08-03-2004, 6:55 PM
    • Columbus, OH
    • Posts 959

    Do you mean how do you put that at the beginning of the xml document?  A very simple way would be just to concatenate it before you pass the string to the LoadXml method.

  • Re: Load XML file From Sproc

    05-09-2008, 1:55 PM
    • Loading...
    • MJ20
    • Joined on 01-12-2008, 2:21 PM
    • Posts 18

    The value of NamesDS is:
    "<?xml version=\"1.0\" standalone=\"yes\"?><db><dbname name=\"ReportServer\"/><dbname name=\"ReportServerTempDB\"/><dbname name=\"Reporting\"/></db>"
    even when I use the .Replace method below to try to remove the \

    I'm getting the error Root element is missing.

    myDAL =
    new DALClass();
    string NamesDs = myDAL.GetXml(); //returns xml resultset
    NamesDs = "<?xml version=\"1.0\" standalone=\"yes\"?>" + NamesDs;
    NamesDs = NamesDs.Replace(
    "\\", string.Empty);
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(NamesDs);
    XmlTextWriter writer = new XmlTextWriter("NamesXml.xml", null);
    writer.Formatting =
    Formatting.Indented;
    doc.Save(writer);

    Tongue Tied
  • Re: Load XML file From Sproc

    05-10-2008, 2:32 PM
    • Loading...
    • jamesqua
    • Joined on 08-03-2004, 6:55 PM
    • Columbus, OH
    • Posts 959

     You don't need to use the replace method.  the backslashes are just escape characters.  The \" signifies that you want a quote within your string, otherwise the compiler would think the quote means the string has ended.  Are you certain that

    "<?xml version=\"1.0\" standalone=\"yes\"?><db><dbname name=\"ReportServer\"/><dbname name=\"ReportServerTempDB\"/><dbname name=\"Reporting\"/></db>"

    is the value of NamesDs?  Because it compiles and runs just fine when I tried it.  On what line is the error occurring?

  • Re: Load XML file From Sproc

    05-10-2008, 9:14 PM
    • Loading...
    • MJ20
    • Joined on 01-12-2008, 2:21 PM
    • Posts 18

    I set a break point on the following line: doc.Save(writer);
    and I'm getting the following value for NamesDs: "<?xml version=\"1.0\" standalone=\"yes\"?><db><dbname name=\"ReportServer\"/><dbname name=\"ReportServerTempDB\"/><dbname name=\"Reporting\"/></db>"

    I'm not getting the escape keys when I run the sproc in SQL Server.
    SELECT custname as "@name"
    FROM CustomerNames
    FOR XML PATH('custname'), ROOT('cust');

    I'm not getting an error anymore. But it's not writing into my xml file. I've tried the following 3 options with my xml file but it doesn't ever get written into when running the app. I'm sorry. I'm new to xml.

    option 1: (blank)
    option 2:
    <?xml version="1.0" standalone="yes"?>
    option 3:
    <?xml version="1.0" standalone="yes"?>
    <
    cust>
    <
    custname name="" />
    </cust>

  • Re: Load XML file From Sproc

    05-11-2008, 5:28 PM
    • Loading...
    • MJ20
    • Joined on 01-12-2008, 2:21 PM
    • Posts 18

    Here's the GetXml method:

            public DataSet GetXml()
            {
                DataSet ds = new DataSet();
                SqlConnection con = new SqlConnection(strConn);
                con.Open();

                SqlCommand sc = new SqlCommand();
                sc.Connection = con;
                sc.CommandType = CommandType.Text;
                sc.CommandText = "dbo.GetCustNames";

                SqlDataAdapter myReader = new SqlDataAdapter(sc);
                myReader.Fill(ds);
                ds.WriteXml(HttpContext.Current.Server.MapPath("NamesXml.xml"));
                con.Close();
                con = null;

                return ds;
            }

     

    myDAL = new DALClass();
    string NamesDs = myDAL.GetXml(); //returns xml resultset
    NamesDs = "<?xml version=\"1.0\" standalone=\"yes\"?>" + NamesDs;
    NamesDs = NamesDs.Replace("\\", string.Empty);
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(NamesDs);
    XmlTextWriter writer = new XmlTextWriter("NamesXml.xml", null);
    writer.Formatting = Formatting.Indented;
    doc.Save(writer);

  • Re: Load XML file From Sproc

    05-13-2008, 3:20 AM
    Answer

    Hi MJ20 ,

    Try my sample,

     

                SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
                SqlCommand com = new SqlCommand("sqlxmltest",con);
                com.CommandType = CommandType.StoredProcedure;
                con.Open();
                object obj = com.ExecuteScalar();
                con.Close();
    
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(obj.ToString ());
                doc.Save(Server.MapPath("sqlxmltest.xml"));
      
    ALTER PROCEDURE dbo.sqlxmltest
    
    AS
    	SELECT id, name
    FROM country FOR XML path('custname'), root('root')

     

     

    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Load XML file From Sproc

    05-14-2008, 4:38 AM

    Hi MJ20 ,

    Just a remind.

    MJ20:

    XmlTextWriter writer = new XmlTextWriter("NamesXml.xml", null);
    writer.Formatting = Formatting.Indented;
    doc.Save(writer);

    Please flush the writer . Try to invoke it's Flush method or Close method.

     

    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Load XML file From Sproc

    20 hours, 21 minutes ago
    • Loading...
    • MJ20
    • Joined on 01-12-2008, 2:21 PM
    • Posts 18

    That's it, thank you!!!! Big Smile

  • Re: Load XML file From Sproc

    1 hour, 38 minutes ago
    • Loading...
    • MJ20
    • Joined on 01-12-2008, 2:21 PM
    • Posts 18

    Is it possible to return multiple result sets using the FOR XML to fill an xml file in my app? It works fine when returning only one result set.
    I've tried:
    object obj = com.ExecuteNonQuery();
    object obj = com.ExecuteReader();
    object obj = com.ExecuteXmlReader();

Page 1 of 1 (14 items)
<