2012-02-06 26 views
5

Sử dụng Mono phiên bản 2.10.5, đoạn code sau thất bại trên bất kỳ tài liệu XML:Mono - XDocument.Load không thành công với 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"); 
     } 
    } 
} 

này chỉ xảy ra nếu XDocument.Load sử dụng LoadOptions.PreserveWhitespace. Bất kỳ ý tưởng về cách làm việc xung quanh này, hoặc giải quyết vấn đề?

Thử nghiệm trên Linux Mint 12 và Ubuntu 11.10.

Đây là ngoại lệ:

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

Bạn nên tìm hiểu cách báo cáo vấn đề Mono qua bugzilla chính thức, http://www.mono-project.com/Bugs –

+0

Có câu hỏi nào không? – sehe

+0

Câu hỏi đặt ra ở đây là: cách tôi có thể giải quyết hoặc làm việc xung quanh nó. –

Trả lời

3

tôi có thể tái tạo các vấn đề tương tự trong cả hai ví dụ mã trên Ubuntu 11.10. Không có vấn đề gì trên nền tảng Windows như bạn đã nói. Dường như thời gian chạy Mono có một số lỗi trong phương thức Save của XDocument do đó dẫn đến lỗi không mong muốn. Tôi muốn báo cáo vấn đề này cho nhóm thời gian chạy Mono để có bản vá phần mềm.

Tuy nhiên, cách giải quyết có thể là tôi có thể mang lại ở đây là,

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

Dường như phương thức Save trong XElement không có bất kỳ vấn đề khi chúng ta gặp phải trong XDocument.

+0

Giải pháp tốt. Cảm ơn! Để lưu ý, tùy chọn 'PreserveWhitespace' vẫn đang chuyển' 'thành' ', vì vậy nó không phải là một đại diện khoảng trống hoàn hảo của tệp gốc. Tôi đã không kiểm tra điều này trên các cửa sổ, chỉ trên Mac sử dụng 'Mono 2.10.9'. – cod3monk3y

+0

Thật không may điều này sẽ không làm việc cho tôi. Tệp 'Info.plist' bắt đầu bằng thẻ DOCTYPE' cod3monk3y

Các vấn đề liên quan