隔离存储读取数据:System.Xml.XmlException:意外的XML声明

本文关键字:意外 XML 声明 XmlException System 存储 读取 数据 隔离 Xml | 更新日期: 2023-09-27 17:50:56

我正在写数据并读取它,我得到"System.Xml.XmlException: Unexpected XML declaration"的例外,我无法找出问题所在。

我还添加了打印异常。请帮我解决这个问题。

下面是我的代码:

public static void WriteTopicState(Topic topic)
        {
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (StreamWriter sw = new StreamWriter(store.OpenFile("Stats.xml", FileMode.Append, FileAccess.Write)))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(Topic));
                        serializer.Serialize(sw, topic);
                        serializer = null;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static Topic ReadMockTestTopicState()
        {
            Topic topic = null;
            try
            {
                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // Read application settings. 
                    if (isoStore.FileExists("Stats.xml"))
                    {
                        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            using (StreamReader SR = new StreamReader(store.OpenFile("Stats.xml", FileMode.Open, FileAccess.Read)))
                            {
                                XmlSerializer serializer = new XmlSerializer(typeof(Topic));
                                topic = (Topic)serializer.Deserialize(SR);
                                serializer = null;
                            }
                        }
                    }
                    else
                    {
                        // If setting does not exists return default setting.
                        topic = new Topic();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return topic;
        }
异常:

{System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> 
System.InvalidOperationException: There is an error in XML document (9, 19). ---> 
System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 9, position 19.

编辑

如果有任何其他的方式,我可以将数据保存到一个文本文件也很好,但唯一的事情是我需要将数据追加到现有的文档,并读取它得到的数据。

隔离存储读取数据:System.Xml.XmlException:意外的XML声明

您的问题是因为您正在向Stats.xml文档添加内容,因此一旦它被多次写入,它将包含多个根元素。

如果您希望只存储最新的统计数据,您应该使用FileMode.Create:

using (StreamWriter sw = new StreamWriter(store.OpenFile("Stats.xml", FileMode.Create, FileAccess.Write)))

有效的XmlDocuments只能包含一个根元素,如果您希望存储多个"统计",则需要使用不同的策略:

  • 如果写只是创建(例如不是更新),则将每个主题写到不同的文件中,并在读取
  • 时将它们组合在一起
  • 创建一个容器元素,它将存储多个主题,然后从磁盘解析它,添加到它,然后覆盖磁盘上的文件(如果选择此选项,您需要小心并发性)
  • 使用不同于文件系统的存储介质(如文档数据库、SQL数据库等)
  • 许多其他选项