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

我与影子孤独终老i 提交于 2019-12-13 19:20:03

问题


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.

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.


回答1:


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.

If you wish to only store the latest stats, you should use 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:

  • 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)
  • Many other options


来源:https://stackoverflow.com/questions/22480512/isolated-storage-reading-data-system-xml-xmlexception-unexpected-xml-declarat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!