2012-02-06 63 views
5

使用單聲道版本2.10.5,下面的代碼無法在任何XML文檔:單 - XDocument.Load失敗LoadOptions.PreserveWhitespace

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.IO; 
using System.Xml.Linq; 

namespace TestXDocument 
{ 
    class MainClass 
    { 
     public static void Main (string[] args) 
     { 
      Stream s = File.Open("Settings.xml", FileMode.Open); 
      XDocument d = XDocument.Load(s, LoadOptions.PreserveWhitespace); 
      s.Close(); 
      d.Save("Settings.xml"); 
     } 
    } 
} 

如果XDocument.Load使用LoadOptions.PreserveWhitespace這只是發生。有關如何解決此問題或解決問題的任何想法?

在Linux Mint 12和Ubuntu 11.10上測試。

這裏的例外:

Unhandled Exception: System.InvalidOperationException: This XmlWriter does not accept Text at this state Prolog. 
    at System.Xml.XmlTextWriter.ShiftStateContent (System.String occured, Boolean allowAttribute) [0x00000] in <filename unknown>:0 
    at System.Xml.XmlTextWriter.WriteString (System.String text) [0x00000] in <filename unknown>:0 
    at System.Xml.DefaultXmlWriter.WriteString (System.String text) [0x00000] in <filename unknown>:0 
    at System.Xml.Linq.XText.WriteTo (System.Xml.XmlWriter w) [0x00000] in <filename unknown>:0 
    at System.Xml.Linq.XDocument.WriteTo (System.Xml.XmlWriter w) [0x00000] in <filename unknown>:0 
    at System.Xml.Linq.XDocument.Save (System.Xml.XmlWriter w) [0x00000] in <filename unknown>:0 
    at System.Xml.Linq.XDocument.Save (System.String filename, SaveOptions options) [0x00000] in <filename unknown>:0 
    at System.Xml.Linq.XDocument.Save (System.String filename) [0x00000] in <filename unknown>:0 
    at TestXDocument.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: This XmlWriter does not accept Text at this state Prolog. 
    at System.Xml.XmlTextWriter.ShiftStateContent (System.String occured, Boolean allowAttribute) [0x00000] in <filename unknown>:0 
    at System.Xml.XmlTextWriter.WriteString (System.String text) [0x00000] in <filename unknown>:0 
    at System.Xml.DefaultXmlWriter.WriteString (System.String text) [0x00000] in <filename unknown>:0 
    at System.Xml.Linq.XText.WriteTo (System.Xml.XmlWriter w) [0x00000] in <filename unknown>:0 
    at System.Xml.Linq.XDocument.WriteTo (System.Xml.XmlWriter w) [0x00000] in <filename unknown>:0 
    at System.Xml.Linq.XDocument.Save (System.Xml.XmlWriter w) [0x00000] in <filename unknown>:0 
    at System.Xml.Linq.XDocument.Save (System.String filename, SaveOptions options) [0x00000] in <filename unknown>:0 
    at System.Xml.Linq.XDocument.Save (System.String filename) [0x00000] in <filename unknown>:0 
    at TestXDocument.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
+2

您應該學習如何通過官方bugzilla報告單聲道問題,http://www.mono-project.com/Bugs – 2012-02-06 09:08:32

+0

有問題嗎? – sehe 2012-02-06 09:14:04

+0

這裏的問題是:我如何解決或解決它。 – 2012-02-06 09:29:31

回答

3

我能複製同樣的問題,在這兩個代碼示例在Ubuntu 11.10。正如你所說,在Windows平臺上沒有問題。看來,Mono運行時在XDocument的Save方法中存在某些錯誤,從而導致意外錯誤。我想將此問題報告給Mono運行團隊以獲取軟件補丁。

然而,可能的解決方法我把我在這裏,

d.Root.Save("Settings1.xml"); 

看來,在的XElement保存方法沒有任何問題,因爲我們的XDocument遇到。

+0

很好的解決方法。謝謝!要注意的是,'PreserveWhitespace'選項仍然將''轉換爲'',所以它不是原始文件的完美空白表示。我沒有在Windows上檢查過,只在Mac上使用'Mono 2.10.9'。 – cod3monk3y 2013-02-07 23:27:11

+0

不幸的是,這不適合我。 'Info.plist'文件以DOCTYPE標記'<!DOCTYPE plist PUBLIC ...'開始,它不會被'd.Root.Save'保存。回到'LoadOptions.None'我想! – cod3monk3y 2013-02-07 23:51:09