简体   繁体   English

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

[英]Isolated storage reading data : System.Xml.XmlException: Unexpected XML declaration

I am writing the data and reading it, i am getting the exception as "System.Xml.XmlException: Unexpected XML declaration" ,i am unable to figure it out whats the issue. 我正在编写数据并读取它,我得到异常为"System.Xml.XmlException: Unexpected XML declaration" ,我无法弄清楚这是什么问题。

I have also added the exception that its printing.Please help me to solve the issue. 我还添加了它的打印例外。请帮我解决问题。

Here my code: 这是我的代码:

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

Exception : 例外:

{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.

EDIT 编辑

If there is any other way that i can save the data to a txt file also is fine for me, but only thing is i need to append the data to the existing document and read it get back the data. 如果有任何其他方式我可以将数据保存到txt文件对我来说也没关系,但唯一的事情是我需要将数据附加到现有文档并读取它取回数据。

Your problem is because you are appending to your Stats.xml document, as a result it will contain multiple root elements once it has been written to more than once. 您的问题是因为您要附加到Stats.xml文档,因此一旦写入多个根元素,它将包含多个根元素。

If you wish to only store the latest stats, you should use FileMode.Create : 如果您只想存储最新的统计信息,则应使用FileMode.Create

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

Valid XmlDocuments can only contain one root element, if you wish to store multiple 'stats' a different strategy is required: 有效的XmlDocuments只能包含一个根元素,如果您希望存储多个“统计信息”,则需要采用不同的策略:

  • If writes are only creates (eg not updates) write out each topic to a different file and combine them when reading 如果仅创建写入(例如,不更新),则将每个主题写出到不同的文件,并在阅读时将它们组合
  • Create a container element that will store multiple topics and then parse this from disk, add to it, then subsequently overwrite the file on disk (you'll need to be careful with concurrency if you choose this option) 创建一个容器元素,它将存储多个主题,然后从磁盘解析,添加到它,然后覆盖磁盘上的文件(如果选择此选项,则需要注意并发)
  • Use a different storage medium than the file system (eg a document database, a SQL database, etc) 使用与文件系统不同的存储介质(例如文档数据库,SQL数据库等)
  • Many other options 许多其他选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将XML读入数据表-System.Xml.XmlException:根元素丢失 - Reading XML Into A DataTable - System.Xml.XmlException: Root element is missing Windows Identity Foundation System.Xml.XmlException:意外的文件结尾 - Windows Identity Foundation System.Xml.XmlException: Unexpected end of file System.XML.XmlException:“ ”是意外标记。 预期的令牌是 ';' - System.XML.XmlException: ' ' is an unexpected token. The expected token is ';' System.Xml.XmlException:“[值]”是意外标记。 预期的标记是 '"' 或 ''' - System.Xml.XmlException: '[value]' is an unexpected token. The expected token is '"' or ''' 如何捕获system.xml.xmlexception - How to catch system.xml.xmlexception System.Xml.XmlException-根元素丢失 - System.Xml.XmlException - Root element is missing W8电话System.Xml.XmlException& - W8 Phone System.Xml.XmlException & System.Xml.XmlException与dotnet测试一起发生 - System.Xml.XmlException occurs with dotnet test System.Xml.XmlException:解析名称时发生了意外的文件结尾 - System.Xml.XmlException: Unexpected end of file while parsing Name has occurred 流异常&System.Xml.XmlException:'-'是意外令牌。 预期令牌为” - Stream exception & System.Xml.XmlException: '--' is an unexpected token. The expected token is ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM