- Fetch XML (xmldatadocument) from database

This post shows you how to return XmlDataDocument from the database.
This helps you to fetch the xml data created in sql and use in asp.net.
If you are familiar how to get xml data from sql then this post helps you to fetch the xml data in XmlDataDocument.

//Code Starts Here

// http://helpindotnet.blogspot.com/
// http://helpindotnet.wordpress.com

public XmlDataDocument getauditnames(string prefixText, int count)
{

XmlDataDocument xml = new XmlDataDocument();
SqlConnection con = new SqlConnection(” <<Connection String >>”);
SqlCommand com = new SqlCommand(“<<Query>> For XML PATH (‘Path’), root(‘Root’)”, con);
//The key to this step is the FOR XML PATH(###), ROOT(###) part. This tells SQL Server to return XML with each row having the element name //”Path” of and the root of the XML document to be ROOT
con.Open();
XmlReader xdr = com.ExecuteXmlReader();
xml.Load(xdr);
xdr.Close();
con.Close();
return xml;
}
//Code Ends Here

Leave a Reply