Do not call WriteString() for whitespace XText.
authorAtsushi Eno <atsushieno@veritas-vos-liberabit.com>
Tue, 7 Feb 2012 06:24:03 +0000 (15:24 +0900)
committerAtsushi Eno <atsushieno@veritas-vos-liberabit.com>
Tue, 7 Feb 2012 06:24:03 +0000 (15:24 +0900)
mcs/class/System.Xml.Linq/System.Xml.Linq/XText.cs
mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XTextTest.cs

index 8eac39d162e9d2a60493092a10def46c040c0c89..4ad1b25bf6aedd049aa1f25f7c62c6f25cfb42c8 100644 (file)
@@ -26,6 +26,7 @@
 
 using System;
 using System.IO;
+using System.Linq;
 using System.Text;
 using System.Xml;
 
@@ -60,7 +61,10 @@ namespace System.Xml.Linq
 
                public override void WriteTo (XmlWriter w)
                {
-                       w.WriteString (value);
+                       if (Value.Length > 0 && Value.All (c => c == ' ' || c == '\t' || c == '\r' || c == '\n'))
+                               w.WriteWhitespace (value);
+                       else
+                               w.WriteString (value);
                }
        }
 }
index 865e5d5ecca81acc658dd60c6b7df64a020b8f59..5d72f657f49bbdedd5d43af14ff4a5aaf1250d65 100644 (file)
@@ -61,5 +61,12 @@ namespace MonoTests.System.Xml.Linq
                        Assert.IsNotNull (newDocument);
                        Assert.IsNotNull (newDocument.Elements ().First ());
                }
+               
+               [Test]
+               public void WriteWhitespaceToXml ()
+               {
+                       var doc = new XDocument (new XText ("\n"), new XElement ("root"));
+                       doc.Save (TextWriter.Null);
+               }
        }
 }