Mono - XDocument.Load fails with LoadOptions.PreserveWhitespace

爱⌒轻易说出口 提交于 2019-12-10 14:19:19

问题


Using Mono version 2.10.5, the following code fails on 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 work around this, or solve the problem?

Tested on Linux Mint 12 and Ubuntu 11.10.

Here's the 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 

回答1:


I could reproduce the same issue in both code examples on Ubuntu 11.10. No issues on Windows platform as you said. It seems that Mono runtime has certain bugs in Save method of XDocument thereby resulting unexpected errors. I would like to report this issue to Mono runtime team for a software patch.

However, the possible workaround that I could bring here is,

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

It seems that Save method in XElement does not have any issues as we encountered in XDocument.



来源:https://stackoverflow.com/questions/9157642/mono-xdocument-load-fails-with-loadoptions-preservewhitespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!