Mark:
If your proc has that select statement in it, I think you are going to get
back three datatables which may not be what you want. You don't you try
adding ", Elements" after your xml auto predicate.
Also, you are going to have the execute the SP from a command object, a
stream alone isn't going to get you anything without a command/connection to
the db.
I'm not sure how the rest of your code looks, but if you wanted to get back
your code in well formed valid XML, this code snippet will do it for you.
I'm outputting the text to a text box, but you would just as easily add this
to a stream, write it to a file etc. Just substitue the second select
statement you have with my SELECT statment, change the connection string,
and check the output, I think this is what you want...
private void btnXMLReader_Click(object sender, System.EventArgs e)
{
SqlConnection cn = new SqlConnection("CONNECTION STRING");
SqlCommand cmd = new SqlCommand("Select somedata for xml AUTO, Elements",
cn);
if (cn.State != ConnectionState.Open){cn.Open();}
XmlReader reader = cmd.ExecuteXmlReader();
while(reader.Read())
{
tbOut.AppendText(reader.Value +"\r\n");
}
reader.Close();
cn.Close();
}
You could also wrap it in a code snippet like this within the reader to
write out datareader results if they Weren't in XML already:
private void btnGenXML_Click(object sender, System.EventArgs e)
{
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("Products");
doc.AppendChild(root);
XmlAttribute productInfo = doc.CreateAttribute("Date");
productInfo.InnerText = DateTime.Now.ToShortDateString();
root.SetAttributeNode(productInfo);
XmlElement product = doc.CreateElement("Product");
root.AppendChild(product);
XmlAttribute productName = doc.CreateAttribute("Name");
productName.InnerText = "DietCoke";
product.SetAttributeNode(productName);
XmlProcessingInstruction xmlInstruction =
doc.CreateProcessingInstruction("xml", "version='1.0'
encoding='ISO-8859-1'");
doc.InsertBefore(xmlInstruction, doc.ChildNodes[0]);
try
{
doc.Save(@"\Sample.xml");
}
HTH,
Bill