首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mono - XDocument.Load因LoadOptions.PreserveWhitespace而失败

Mono - XDocument.Load因LoadOptions.PreserveWhitespace而失败
EN

Stack Overflow用户
提问于 2012-02-06 16:55:13
回答 1查看 1.8K关注 0票数 5

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

代码语言:javascript
复制
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时,才会发生这种情况。对于如何解决这个问题,有什么想法吗?

在Linux Mint 12和Ubuntu 11.10上测试。

下面是一个例外:

代码语言:javascript
复制
 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 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-08 00:39:22

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

然而,我可以在这里带来的可能的解决方案是,

代码语言:javascript
复制
d.Root.Save("Settings1.xml");

与我们在XDocument中遇到的问题一样,XElement中的保存方法似乎没有任何问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9157642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档