ds.WriteXml(strXmlTestCasePath, XmlWriteMode.IgnoreSchema);
ds is a dataset
. I want to add an extra line or extra information into this XML. How do I do it?
ds.WriteXml(strXmlTestCasePath, XmlWriteMode.IgnoreSchema);
ds is a dataset
. I want to add an extra line or extra information into this XML. How do I do it?
Use an XmlWriter
to write your DataSet
. You can then use the same object to write additional XML.
illustrative code:
System.Data.DataSet ds;
System.Xml.XmlWriter x;
ds.WriteXml(x);
x.WriteElementString("test", "value");
You can't simply write more XML to the end of a serialized DataSet
, since if you do you'll be producing an XML document with more than one top-level element. Using an XmlWriter
, you'd need to do something like this:
using (XmlWriter xw = XmlWriter.Create(strXmlTestCasePath));
{
xw.WriteStartElement("container");
ds.WriteXml(xw, XmlWriterMode.IgnoreSchema);
// from here on, you can use the XmlWriter to add XML to the end; you then
// have to wrap things up by closing the enclosing "container" element:
...
xw.WriteEndElement();
}
But this won't help you if what you're trying to do is add XML elements inside the serialized DataSet
. To do that, you'll need to serialize the DataSet
, read it into an XmlDocument
, and then use DOM methods to manipulate the XML.
Or, alternatively, create and populate a new DataTable
right before you serialize the DataSet
and then delete it when you're done. It really depends on what your actual requirements are.