tags:

views:

259

answers:

2
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?

+5  A: 

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");
Gerrie Schenck
i want it to write it to same xml
Arunachalam
It is a reference to the same object you use to write your DataSet's xml and extra xml to.
Gerrie Schenck
actually my question is i have saved my dataset in a xml now after writing it to the xml i have to add a extra line inside the xml which i saved now
Arunachalam
Yes, and he answered your question. What part of his answer do you think doesn't answer your question?
Mystere Man
when run this code its returns error as use of unassigned local variables
Arunachalam
My code snippet was just to illustrate the use of an xmlwriter together with a DataSet, of course is doesn't work.
Gerrie Schenck
+1  A: 

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.

Robert Rossney