2012-02-06 12 views
5

Utilizzando Mono versione 2.10.5, il seguente codice non è riuscito qualsiasi documento XML:Mono - XDocument.Load fallisce con 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"); 
     } 
    } 
} 

questo accade solo se XDocument.Load usa LoadOptions.PreserveWhitespace. Qualche idea su come aggirare questo problema o risolvere il problema?

Testato su Linux Mint 12 e Ubuntu 11.10.

Ecco l'eccezione:

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

Dovresti imparare come segnalare i problemi Mono tramite il bugzilla ufficiale, http://www.mono-project.com/Bugs –

+0

C'è una domanda? – sehe

+0

La domanda qui è: come posso risolverlo o aggirarlo. –

risposta

3

ho potuto riprodurre lo stesso problema in entrambi gli esempi di codice su Ubuntu 11.10. Nessun problema sulla piattaforma Windows come hai detto. Sembra che Mono runtime abbia alcuni bug nel metodo Save di XDocument, con conseguenti errori imprevisti. Vorrei segnalare questo problema al team di runtime di Mono per una patch software.

Tuttavia, la possibile soluzione che ho potuto portare qui è,

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

Sembra che Salva metodo XElement non ha problemi, come abbiamo incontrato in XDocument.

+0

Soluzione piacevole. Grazie! Per notare, l'opzione 'PreserveWhitespace' sta ancora girando' 'in' ', quindi non è una perfetta rappresentazione degli spazi bianchi del file originale. Non l'ho verificato su Windows, solo su Mac usando 'Mono 2.10.9'. – cod3monk3y

+0

Purtroppo questo non funzionerà per me. Il file 'Info.plist' inizia con un tag DOCTYPE' cod3monk3y

Problemi correlati