Dre*_*ult 5 c# mono linq-to-xml
使用Mono版本2.10.5,以下代码在任何XML文档上都失败:
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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
只有在XDocument.Load使用LoadOptions.PreserveWhitespace时才会发生这种情况.有关如何解决这个问题或解决问题的任何想法?
在Linux Mint 12和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
Run Code Online (Sandbox Code Playgroud)
我可以在 Ubuntu 11.10 上的两个代码示例中重现相同的问题。正如你所说,Windows 平台上没有问题。Mono 运行时似乎在 XDocument 的 Save 方法中存在某些错误,从而导致意外错误。我想向 Mono 运行时团队报告此问题以获取软件补丁。
然而,我可以在这里带来的可能的解决方法是,
d.Root.Save("Settings1.xml");
Run Code Online (Sandbox Code Playgroud)
看来 XElement 中的 Save 方法没有我们在 XDocument 中遇到的任何问题。
| 归档时间: |
|
| 查看次数: |
1706 次 |
| 最近记录: |