Appending DataSet to an XML file

M

markliam

Is there a a quick way of appending a dataset to an XML file, or does
the XML file need to be read into a new dataset and then re-saved with
a new dataset containing both old/new data?
 
N

Nicholas Paldino [.NET/C# MVP]

Mark,

You should be able to save the dataset to an XML file and then load that
into a new XmlDocument (you can use a MemoryStream for the intermediate
storage). Then, you can take the old XML file and you should be able to
assign the nodes from the new file to wherever you want in the old document.
 
M

Martin Honnen

To clarify, I'm saving the dataset by using dataset.WriteXml(stream).

You can use XmlDocument to load your existing XML document, then you can
create an XPathNavigator on the node you want to append the dataset's
XML to and use AppendChild to feed the returned XmlWriter to the
WriteXml method e.g. assuming you have an XmlDocument xmlDoc and a
DataSet dataSet and you want to append to the root element you can use

using (XmlWriter writer =
xmlDoc.DocumentElement.CreateNavigator().AppendChild())
{
dataSet.WriteXml(writer);
}
xmlDoc.Save("file.xml");
 

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