2012-02-06 29 views
5

Usando Mono versión 2.10.5, el siguiente código falla en cualquier documento XML:Mono - XDocument.Load falla 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"); 
     } 
    } 
} 

Esto sólo ocurre si utiliza XDocument.Load LoadOptions.PreserveWhitespace. ¿Alguna idea sobre cómo solucionar esto o resolver el problema?

Probado en Linux Mint 12 y Ubuntu 11.10.

Aquí es la excepción:

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

Debería aprender a reportar problemas Mono a través del bugzilla oficial, http://www.mono-project.com/Bugs –

+0

¿Hay alguna pregunta? – sehe

+0

La pregunta aquí es: cómo puedo resolverlo o solucionarlo. –

Respuesta

3

que podría reproducir el mismo problema en ambos ejemplos de código en Ubuntu 11.10. No hay problemas en la plataforma de Windows como dijiste. Parece que el tiempo de ejecución Mono tiene ciertos errores en el método Save de XDocument, lo que resulta en errores inesperados. Me gustaría informar este problema al equipo de tiempo de ejecución de Mono para obtener un parche de software.

Sin embargo, la posible solución que podría traer aquí es,

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

Parece que Guardar método en el XElement no tiene ningún problema como el que nos encontramos en XDocument.

+0

Una buena solución. ¡Gracias! Para tener en cuenta, la opción 'PreserveWhitespace' todavía está convirtiendo' 'en' ', por lo que no es una representación perfecta del espacio en blanco del archivo original. No he comprobado esto en Windows, solo en Mac usando 'Mono 2.10.9'. – cod3monk3y

+0

Lamentablemente, esto no funcionará para mí. El archivo 'Info.plist' comienza con una etiqueta DOCTYPE' cod3monk3y