仅序列化数据的反序列化失败,为什么?

发布于 2024-09-14 02:22:15 字数 1553 浏览 3 评论 0 原文

我正在尝试在 silverlight 中序列化和反序列化对象,但似乎无法让它工作。序列化工作得很好,但它抱怨我的 rootObject 不是预期的。我不明白,因为是 Silverlight 生成了 XML。

顺便提一句;我还是 C# 新手,

我有一个类,如下所示:

[XmlRoot("DataStorage")] // has no effect
public class DataStorage
{
    public string type { get; set; }
    public string imgUrl { get; set; }
    public List<AbstractionObject> children { get; set; }

    public DataStorage()
    {
        type = "default";
        children = new List<AbstractionObject>();
    }
}

当由 Silverlight 序列化时,它会产生:

<?xml version="1.0" encoding="utf-8"?>
<DataStorage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <type>default</type>
  <children />
</DataStorage>

但是,当我尝试反序列化时,它会产生:

System.InvalidOperationException 未由用户代码处理 消息=XML 文档中存在错误 (2, 2)。

内部异常:System.InvalidOperationException Message=>没想到。

我的反序列化代码如下:

// create the xmlSerializer for DataObject
XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataObject));

// Open the file again for reading.
StreamReader fileStream = new StreamReader(isoStore.OpenFile("IsoStoreFile.xml", FileMode.Open));
XmlReader xmlReader = XmlReader.Create(fileStream);

DataObject deserializedObject = (DataObject)xmlSerializer.Deserialize(xmlReader);
fileStream.Close();

Debug.WriteLine(deserializedObject);

有什么想法吗?

I am trying to serialize and deserialize objects in silverlight, but cannot seem to get it to work. Serializing works just fine but it complains about my rootObject not being expected. I don't get it because it it is Silverlight who generated the XML.

Btw; i'm still new to C#

I have a class which looks like:

[XmlRoot("DataStorage")] // has no effect
public class DataStorage
{
    public string type { get; set; }
    public string imgUrl { get; set; }
    public List<AbstractionObject> children { get; set; }

    public DataStorage()
    {
        type = "default";
        children = new List<AbstractionObject>();
    }
}

When serialized by Silverlight it produces:

<?xml version="1.0" encoding="utf-8"?>
<DataStorage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <type>default</type>
  <children />
</DataStorage>

However, when I attempt to deserialize it produces:

System.InvalidOperationException was unhandled by user code
Message=There is an error in XML document (2, 2).

InnerException: System.InvalidOperationException
Message=<DataStorage xmlns=''> was not expected.

My deserialization code is as follows:

// create the xmlSerializer for DataObject
XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataObject));

// Open the file again for reading.
StreamReader fileStream = new StreamReader(isoStore.OpenFile("IsoStoreFile.xml", FileMode.Open));
XmlReader xmlReader = XmlReader.Create(fileStream);

DataObject deserializedObject = (DataObject)xmlSerializer.Deserialize(xmlReader);
fileStream.Close();

Debug.WriteLine(deserializedObject);

Any idea?

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

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

发布评论

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

评论(1

匿名的好友 2024-09-21 02:22:15

我不知道这是否只是您示例中的拼写错误,但是您在第一个块中定义的类是“DataStorage”,并且您正在序列化和反序列化“DataObject”,这可能解释了为什么它在遇到“数据存储”。

I don't know if this may just be a typo in your example, but the class you've defined in the first block is "DataStorage" and you're serializing and deserializing a "DataObject" which may explains why it's breaking when it encounters "DataStorage".

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