Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 2.98 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Mono - XDocument.Load fails with LoadOptions.PreserveWhitespace
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.IO;
  6. using System.Xml.Linq;
  7.  
  8. namespace TestXDocument
  9. {
  10.     class MainClass
  11.     {
  12.         public static void Main (string[] args)
  13.         {
  14.             Stream s = File.Open("Settings.xml", FileMode.Open);
  15.             XDocument d = XDocument.Load(s, LoadOptions.PreserveWhitespace);
  16.             s.Close();
  17.             d.Save("Settings.xml");
  18.         }
  19.     }
  20. }
  21.        
  22. Unhandled Exception: System.InvalidOperationException: This XmlWriter does not accept Text at this state Prolog.
  23.   at System.Xml.XmlTextWriter.ShiftStateContent (System.String occured, Boolean allowAttribute) [0x00000] in <filename unknown>:0
  24.   at System.Xml.XmlTextWriter.WriteString (System.String text) [0x00000] in <filename unknown>:0
  25.   at System.Xml.DefaultXmlWriter.WriteString (System.String text) [0x00000] in <filename unknown>:0
  26.   at System.Xml.Linq.XText.WriteTo (System.Xml.XmlWriter w) [0x00000] in <filename unknown>:0
  27.   at System.Xml.Linq.XDocument.WriteTo (System.Xml.XmlWriter w) [0x00000] in <filename unknown>:0
  28.   at System.Xml.Linq.XDocument.Save (System.Xml.XmlWriter w) [0x00000] in <filename unknown>:0
  29.   at System.Xml.Linq.XDocument.Save (System.String filename, SaveOptions options) [0x00000] in <filename unknown>:0
  30.   at System.Xml.Linq.XDocument.Save (System.String filename) [0x00000] in <filename unknown>:0
  31.   at TestXDocument.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
  32. [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: This XmlWriter does not accept Text at this state Prolog.
  33.   at System.Xml.XmlTextWriter.ShiftStateContent (System.String occured, Boolean allowAttribute) [0x00000] in <filename unknown>:0
  34.   at System.Xml.XmlTextWriter.WriteString (System.String text) [0x00000] in <filename unknown>:0
  35.   at System.Xml.DefaultXmlWriter.WriteString (System.String text) [0x00000] in <filename unknown>:0
  36.   at System.Xml.Linq.XText.WriteTo (System.Xml.XmlWriter w) [0x00000] in <filename unknown>:0
  37.   at System.Xml.Linq.XDocument.WriteTo (System.Xml.XmlWriter w) [0x00000] in <filename unknown>:0
  38.   at System.Xml.Linq.XDocument.Save (System.Xml.XmlWriter w) [0x00000] in <filename unknown>:0
  39.   at System.Xml.Linq.XDocument.Save (System.String filename, SaveOptions options) [0x00000] in <filename unknown>:0
  40.   at System.Xml.Linq.XDocument.Save (System.String filename) [0x00000] in <filename unknown>:0
  41.   at TestXDocument.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
  42.        
  43. using System;
  44. using System.Xml.Linq;
  45. using System.IO;
  46.  
  47. namespace Test
  48. {
  49.     class MainClass
  50.     {
  51.         public static void Main (string[] args)
  52.         {
  53.             using (var s = new StreamReader("E:\expected.xml"))
  54.             {
  55.                 XDocument d = XDocument.Load(s, LoadOptions.PreserveWhitespace);
  56.                 s.Close();
  57.                 d.Save("e:\Settings.xml");
  58.             }
  59.         }
  60.     }
  61. }
  62.        
  63. d.Root.Save("Settings1.xml");