Mono - XDocument.Load not working with LoadOptions.PreserveWhitespace

Using Mono version 2.10.5, the following code does not execute in any XML document:

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");
        }
    }
}

This only happens if XDocument.Load uses LoadOptions.PreserveWhitespace. Any ideas on how to get around this or solve the problem?

Tested on Linux Mint 12 and Ubuntu 11.10.

Here's an exception:

 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 
+5
source share
1 answer

I could reproduce the same problem in both code samples on Ubuntu 11.10. As you said, there are no problems on the Windows platform. It seems that there are certain errors in the Mono runtime during the Save XDocument method, which leads to unexpected errors. I would like to report this issue to the Mono runtime team for a software fix.

, ,

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

, Save XElement , XDocument.

+3

All Articles