Take the 2-minute tour ×

I have a dataset named DocumentDataSet along with a class named Document.

When the dataset is serialized, I need it to have it root named "Document" because I'm communicating with a 3rd party webservice.

I though of defining the attribute XmlRoot in the partial class of the dataset, but I cannot add a duplicate of XmlRoot since it is already defined in the designer class.

public partial class DocumentDataSet : global::System.Data.DataSet { ... }

I could change it in the designer class, but it get reset every time I open the dataset in design.

Is there is a way to override XmlRoot or make it serialize with a different name that its class name?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You can use the XmlSerializer constructor that accepts an XmlRootAttribute which represents the XML root element to be used.

new XmlSerializer(typeof(DocumentDataSet), new XmlRootAttribute("Document"));

It's also possible to do something like this:

class DocumentDataSet : DataSet
    public new string GetXml()
        return base.GetXml().Replace("DocumentDataSet ", "Document");

If you end up with this approach a simple Replace is not enough, but this is just for illustration purposes. Also be advised that if you reference your document data set instances by the base class DataSet this last approach won't work.

DataSet ds = new DocumentDataSet();

ds.GetXml(); // Wrong
share|improve this answer
So, I should override at the moment I serialize? sound ok –  Pierre-Alain Vigeant Dec 22 '09 at 15:55
Yes, when serializing the DocumentDataSet you should override the root element as specified in the example. –  João Angelo Dec 22 '09 at 16:00
That would make us change all our GetXml to using a serializer object instead. It is doable, but requires a lot of work.. urk.. –  Pierre-Alain Vigeant Dec 22 '09 at 16:14
add comment

Your Answer


