Jyothish
Jyothish

Reputation: 561

XML Reader and Serialization

        XmlReader reader = null;

        XmlSerializer serailizer = new XmlSerializer(typeof(List<TObject>));

        BufferedStream stream = new BufferedStream(new MemoryStream());
        serailizer.Serialize(stream, items);

        reader = XmlReader.Create(stream);
        reader.ReadStartElement(_words);

I am trying to make an XmlReader from a serialized stream of an object. But it throws an exception “Root element is missing.” Any idea how I would fix it?

Upvotes: 0

Views: 1541

Answers (1)

PeskyGnat
PeskyGnat

Reputation: 2464

After you've serialized your object to the stream, you'll need to rewind the stream back to the beginning so that the XmlReader reads from the start and not the end. You can set the position back to 0 with:

    serailizer.Serialize(stream, items);
    stream.Position = 0;

Upvotes: 1

Related Questions