我们如何在C#中更新现有的XML文件

时间:2013-01-23 06:41:19

标签: c# xml

我有一个我想要更新的现有xml文件。

using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
        {
            // Load the xml document for reading
            using (IsolatedStorageFileStream ISFS = ISF.OpenFile(FILE_NAME, FileMode.Open, FileAccess.ReadWrite))
            {
                XDocument file= XDocument.Load(ISFS);
                //Edit the file
                file.Save(ISFS);
            }
        }

问题是每当我使用file.Save(ISFS),然后尝试再次加载它时,使用相同的方法,报告错误

  System.Xml.XmlException was unhandled by user code
  HResult=-2146232000
  Message=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 25, position 14.

更新:

这是保存后的文件

<?xml version="1.0" encoding="utf-8"?>
<A>
   <AB ID="0">
       <NickName>Dummy</NickName>
   </AB>
</A><?xml version="1.0" encoding="utf-8"?>
<A>
   <AB ID="0">
       <NickName>Dummy</NickName>
   </AB>
   <AB ID="1">
       <NickName>My1</NickName>
   </AB>
</A>

0 个答案:

没有答案