Search code examples
c#datasetwritexml

How do you suppress the dataset name being written when using WriteXml?


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>

Solution

  • 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.