本文介绍了Mono-XDocument.Load失败,并显示LoadOptions.PreserveWhitespace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Mono版本2.10.5,以下代码在任何XML文档上均失败:

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

这仅在XDocument.Load使用LoadOptions.PreserveWhitespace时发生.关于如何解决此问题或解决问题的任何想法?

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

在Linux Mint 12和Ubuntu 11.10上进行了测试.

Tested on Linux Mint 12 and Ubuntu 11.10.

例外:

 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 

推荐答案

我可以在Ubuntu 11.10的两个代码示例中重现相同的问题.如您所说,在Windows平台上没有问题.似乎Mono运行时在XDocument的Save方法中存在某些错误,从而导致意外错误.我想将此问题报告给Mono运行时团队以获取软件补丁.

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

似乎XElement中的Save方法没有我们在XDocument中遇到的任何问题.

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

这篇关于Mono-XDocument.Load失败,并显示LoadOptions.PreserveWhitespace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:50