0

C# - When using: DataSet.WriteXml(filePath);

The dataset name will be written as the root element. How do you suppress this?

Note: I have a schema tied to this DataSet, the XML data reads into the schema correctly.

Current Output:

<DataSet>  //dataset name prints -- REMOVE
  <HAPPY>
    <HAPPY2>BLAH</HAPPY2>
  </HAPPY>
</DataSet>  //dataset name prints  -- REMOVE

Desired Output:

  <HAPPY>
    <HAPPY2>BLAH</HAPPY2>
  </HAPPY>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
zoom
  • 3
  • 2
  • Does your DataSet only ever have a single DataTable? If not, are you ok with the output being not properly formatted XML as it will no longer have a root node? – Bradford Dillon Jan 06 '15 at 20:13
  • My DataSet consists of 6 DataTables that are linked based on its schema. – zoom Jan 06 '15 at 20:34
  • Removing the root node entirely is not going to leave you with valid XML if you have multiple data tables. What do you want the output to look like with multiple tables? – Bradford Dillon Jan 06 '15 at 20:39
  • The root node is . All the other table items fall under . This is because of the tables relationships. – zoom Jan 06 '15 at 20:57
  • Could you provide a better example of the xml produced then? I made a very simple DataSet to generate the XML you gave above, but it seems the actual XML being generated is far more complex than the example provided. – Bradford Dillon Jan 06 '15 at 21:11
  • I posted the fix below...I tested it and it does indeed work. Thank you all for your help. – zoom Jan 06 '15 at 21:19

3 Answers3

1

An elegant solution would be to use XSLT, but that may be too much for this simple purpose. You could also implement your own custom XmlWriter which forwards every operation to a real implementation, except for the root element. But this is kind of a hack really, not the most maintainable solution.

In this simple case, I would write the XML into memory (StringWriter + XmlWriter), load that into an XmlDocument, and rearrange things in the DOM.

fejesjoco
  • 11,763
  • 3
  • 35
  • 65
1

You can load the XML into memory then edit there before writing it out. How you want to write it out is up to you as removing the root node of the XML tree is going to leave you with invalid XML.

using(MemoryStream ms = new MemoryStream())
{
    dataSet.WriteXml(ms);
    ms.Position = 0;

    var children = XDocument.Load(ms).Root.Elements();
}

This code leaves you with a collection of XElement objects that represent each DataTable in your DataSet. From there you can do whatever else you need to do with it.

Bradford Dillon
  • 1,750
  • 13
  • 24
0

This works...

        XmlWriter w = new XmlTextWriter("C:Blah.xml", Encoding.UTF8);
        w.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");


        XmlDataDocument xd = new XmlDataDocument(DataSet);


        XmlDataDocument xdNew = new XmlDataDocument();
        DataSet.EnforceConstraints = false;


        XmlNode node = xdNew.ImportNode(xd.DocumentElement.LastChild, true);
        node.WriteTo(w);
        w.Close();
zoom
  • 3
  • 2