Arunachalam
Arunachalam

Reputation: 6097

How do I add extra information to XML generated from a dataset Writexml in C#?

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?

Upvotes: 1

Views: 2012

Answers (2)

Robert Rossney
Robert Rossney

Reputation: 96890

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, XmlWriteMode.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.

Upvotes: 1

Gerrie Schenck
Gerrie Schenck

Reputation: 22378

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");

Upvotes: 5

Related Questions