Add a Root element to a loaded XmlDocument?

G

Guest

How can I add an XML Declaration and root element to an XmlDocument after it
has been loaded?

Here is the code...

SqlCommand cmd = cnn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText= "SELECT ID, Desc FROM products FOR XML AUTO, ELEMENT";
XmlReader xr = cmd.ExecuteXmlReader();
XmlDocument xd = new XmlDocument(xr);
xr.Close;
....at this point I would like to add an XML Declaration and root element
nodes, but I am unsure how...
 
M

Manoj G [MVP]

Hi,

This would be a crude way, but works:

xd.LoadXml("<?xml version="1.0" encoding="iso-8859-1"?><Root>" + xd.OuterXml
+ "</Root>");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top