使用 XmlSerializer.Deserialize 从 MemoryStream 反序列化 XML 文件不起作用

发布于 2024-11-19 06:12:14 字数 1035 浏览 1 评论 0原文

我在从 MemoryStream 反序列化 XML 文件时遇到问题。我有一个生成的 MyXmlFile 类和一个包含 XML 文件的 MemoryStream,我想将其反序列化为 MyXmlFile 类型的对象。

public static class XmlSerializeObject
{
    public static T FromStream<T>(Stream s)
    {
        var serializer = new XmlSerializer(typeof(T));
        return (T) serializer.Deserialize(s);
    }
}

我有一个 MemoryStream ms,其中包含一个 xml 文件。如果我尝试将该流反序列化为 MyXmlFile 类型的对象,则会收到异常“XML 文档 (0,0) 中存在错误”,

MyXmlFile test = XmlSerializeObject.FromStream<MyXmlFile>(ms);

但是我验证了我的 MemoryStream 是正确的。如果我首先将流写入光盘上的文件,然后再次读取该文件,则它可以正常工作。

        FileStream outStream = File.OpenWrite("D:\\p.xml");
        outStream.Write(((MemoryStream)ms).ToArray(), 0, ((MemoryStream)ms).ToArray().Length);
        outStream.Flush();
        outStream.Close();
        MyXmlFile test= XmlSerializeObject.FromStream<MyXmlFile>(File.OpenRead("D:\\p.xml"));

我自己无法找到解决方案,这就是为什么我决定发布我的问题。也许有人以前遇到过同样的问题并且能够帮助我。

提前致谢。如果有任何不清楚的地方请询问。

I'm having trouble deserializing my XML file from a MemoryStream. I have a generated MyXmlFile class and a MemoryStream containing an XML file which I want to deserialize into an object of type MyXmlFile.

public static class XmlSerializeObject
{
    public static T FromStream<T>(Stream s)
    {
        var serializer = new XmlSerializer(typeof(T));
        return (T) serializer.Deserialize(s);
    }
}

I have a MemoryStream ms which contains an xml file. If I try to deserialize that stream into an object of type MyXmlFile I get an exception "There is an error in XML document (0,0)"

MyXmlFile test = XmlSerializeObject.FromStream<MyXmlFile>(ms);

However I verified that my MemoryStream is correct. If I first write my stream into a file on my disc and than read that file again it works fine.

        FileStream outStream = File.OpenWrite("D:\\p.xml");
        outStream.Write(((MemoryStream)ms).ToArray(), 0, ((MemoryStream)ms).ToArray().Length);
        outStream.Flush();
        outStream.Close();
        MyXmlFile test= XmlSerializeObject.FromStream<MyXmlFile>(File.OpenRead("D:\\p.xml"));

I was not able to find a solution myself that is why I decided to post my question. Maybe someone had the same problem before and is able to help me out.

Thanks in advance. If anything is unclear please ask.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘世孤行 2024-11-26 06:12:14

如果您在调用反序列化之前实例化了内存流(例如,首先将 XML 加载到内存流中),则它可能位于错误的索引处。尝试

ms.Seek(0, SeekOrigin.Begin)

返回到流的开头。

If you instantiated your memory stream prior to your call to deserialize (say, to load the XML into the memory stream in the first place) it may be that it's at the wrong index. Try

ms.Seek(0, SeekOrigin.Begin)

To go back to the beginning of the stream.

a√萤火虫的光℡ 2024-11-26 06:12:14

您必须将 MemoryStream 的位置设置为 0。

((MemoryStream)ms).Position = 0;
MyXmlFile test = XmlSerializeObject.FromStream<MyXmlFile>(ms);

You must set position of MemoryStream to the 0.

((MemoryStream)ms).Position = 0;
MyXmlFile test = XmlSerializeObject.FromStream<MyXmlFile>(ms);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文