开发者

XML Reader and Serialization

开发者 https://www.devze.com 2023-03-14 01:58 出处:网络
XmlReader reader = null; XmlSerializer serailizer = new XmlSerializer(typeof(List<TObject>)); BufferedStream stream = new BufferedStream(new MemoryStream());
        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

精彩评论

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