开发者

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.Cre开发者_StackOverflowate(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?


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜