2008-02-11 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Mon, 11 Feb 2008 14:45:04 +0000 (14:45 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Mon, 11 Feb 2008 14:45:04 +0000 (14:45 -0000)
* XNode.cs, XElement.cs, XStreamingElement.cs, XContainer.cs,
  XUtil.cs : one-object to one-XNode conversion is wrong. It could
  be one-or-more nodes (i.e. when object is IEnumerable).

svn path=/trunk/mcs/; revision=95438

mcs/class/System.Xml.Linq/System.Xml.Linq/ChangeLog
mcs/class/System.Xml.Linq/System.Xml.Linq/XContainer.cs
mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs
mcs/class/System.Xml.Linq/System.Xml.Linq/XNode.cs
mcs/class/System.Xml.Linq/System.Xml.Linq/XStreamingElement.cs
mcs/class/System.Xml.Linq/System.Xml.Linq/XUtil.cs

index 22c9e344a3d62a8de80420d38940d26e2be05498..0e9928073da73421fe8d893193e0d0d8b7a7e57e 100644 (file)
@@ -1,3 +1,9 @@
+2008-02-11  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * XNode.cs, XElement.cs, XStreamingElement.cs, XContainer.cs,
+         XUtil.cs : one-object to one-XNode conversion is wrong. It could
+         be one-or-more nodes (i.e. when object is IEnumerable).
+
 2008-02-11  Atsushi Enomoto  <atsushi@ximian.com>
 
        * XNamespace.cs : make Get() and GetName() table-based.
index a2317fd2b88fd951bb0d95ad751644226a54c613..6e6586f4761ce48e42953a6f7c8874b3019d9d54 100644 (file)
@@ -80,7 +80,12 @@ namespace System.Xml.Linq
                        if (content == null)
                                return;
 
-                       XNode n = XUtil.ToNode (content);
+                       foreach (XNode n in XUtil.ToNodes (content))
+                               AddNode (n);
+               }
+
+               void AddNode (XNode n)
+               {
                        CheckChildType (n, false);
                        OnAdded (n, false);
                        n.SetOwner (this);
index 1b5def7a4fac6bbbe19dd8e6fac014ccc3a521bf..829a053f37d8e592753e56cafd033d6f6c1dae22 100644 (file)
@@ -455,13 +455,8 @@ namespace System.Xml.Linq
                public void Save (string filename, SaveOptions options)
                {
                        XmlWriterSettings s = new XmlWriterSettings ();
-                       if ((options & SaveOptions.DisableFormatting) != 0) {
-                               // hacky!
-                               s.Indent = true;
-                               s.IndentChars = String.Empty;
-                               s.NewLineChars = String.Empty;
-                       }
-                       using (XmlWriter w = XmlWriter.Create (filename)) {
+                       s.Indent = options != SaveOptions.DisableFormatting;
+                       using (XmlWriter w = XmlWriter.Create (filename, s)) {
                                Save (w);
                        }
                }
@@ -474,13 +469,8 @@ namespace System.Xml.Linq
                public void Save (TextWriter tw, SaveOptions options)
                {
                        XmlWriterSettings s = new XmlWriterSettings ();
-                       if ((options & SaveOptions.DisableFormatting) != 0) {
-                               // hacky!
-                               s.Indent = true;
-                               s.IndentChars = String.Empty;
-                               s.NewLineChars = String.Empty;
-                       }
-                       using (XmlWriter w = XmlWriter.Create (tw)) {
+                       s.Indent = options != SaveOptions.DisableFormatting;
+                       using (XmlWriter w = XmlWriter.Create (tw, s)) {
                                Save (w);
                        }
                }
@@ -639,9 +629,9 @@ namespace System.Xml.Linq
 
                public void SetValue (object value)
                {
-                       XNode n = XUtil.ToNode (value);
                        RemoveNodes ();
-                       Add (n);
+                       foreach (XNode n in XUtil.ToNodes (value))
+                               Add (n);
                }
 
                internal override void OnAdded (XNode node, bool addFirst)
index 64ba94cc484768137921a3d2254e229c148b5e50..d1aeecfa710636f292cfe63e13fcdce73815effc 100644 (file)
@@ -82,12 +82,7 @@ namespace System.Xml.Linq
                        StringWriter sw = new StringWriter ();
                        XmlWriterSettings s = new XmlWriterSettings ();
                        s.ConformanceLevel = ConformanceLevel.Auto;
-                       if ((options & SaveOptions.DisableFormatting) == 0) {
-                               // hacky!
-                               s.Indent = true;
-                               s.IndentChars = String.Empty;
-                               s.NewLineChars = String.Empty;
-                       }
+                       s.Indent = options != SaveOptions.DisableFormatting;
                        XmlWriter xw = XmlWriter.Create (sw, s);
                        WriteTo (xw);
                        xw.Close ();
@@ -98,15 +93,16 @@ namespace System.Xml.Linq
                {
                        if (Parent == null)
                                throw new InvalidOperationException ();
-                       XNode n = XUtil.ToNode (content);
-                       n.SetOwner (Parent);
-                       n.previous = this;
-                       n.next = next;
-                       if (next != null)
-                               next.previous = n;
-                       next = n;
-                       if (Parent.LastNode == this)
-                               Parent.LastNode = n;
+                       foreach (XNode n in XUtil.ToNodes (content)) {
+                               n.SetOwner (Parent);
+                               n.previous = this;
+                               n.next = next;
+                               if (next != null)
+                                       next.previous = n;
+                               next = n;
+                               if (Parent.LastNode == this)
+                                       Parent.LastNode = n;
+                       }
                }
 
                public void AddAfterSelf (params object [] content)
@@ -124,15 +120,16 @@ namespace System.Xml.Linq
                {
                        if (Parent == null)
                                throw new InvalidOperationException ();
-                       XNode n = XUtil.ToNode (content);
-                       n.SetOwner (Parent);
-                       n.previous = previous;
-                       n.next = this;
-                       if (previous != null)
-                               previous.next = n;
-                       previous = n;
-                       if (Parent.FirstNode == this)
-                               Parent.FirstNode = n;
+                       foreach (XNode n in XUtil.ToNodes (content)) {
+                               n.SetOwner (Parent);
+                               n.previous = previous;
+                               n.next = this;
+                               if (previous != null)
+                                       previous.next = n;
+                               previous = n;
+                               if (Parent.FirstNode == this)
+                                       Parent.FirstNode = n;
+                       }
                }
 
                public void AddBeforeSelf (params object [] content)
index bfd53c57d25d297d802dbfef0f390acbde944b61..8cd93c10531b66682f669e47d3d4714807f07cff 100644 (file)
@@ -138,7 +138,8 @@ namespace System.Xml.Linq
                                        WriteAttribute ((XAttribute) o, w);
                                else
                                        // FIXME: check node validity
-                                       XUtil.ToNode (o).WriteTo (w);
+                                       foreach (XNode n in XUtil.ToNodes (o))
+                                               n.WriteTo (w);
                        }
                }
 
index 4e3eb4fec8760a009d6ae20fccc6cc74534cb5c4..178dbf2c40ac7dabc18eb71cbf29d7111c852ed1 100644 (file)
@@ -60,17 +60,19 @@ namespace System.Xml.Linq
                        throw new NotImplementedException ();
                }
 
-               // FIXME: this method is not enough by design.
-               public static XNode ToNode (object o)
+               public static IEnumerable<XNode> ToNodes (object o)
                {
                        XNode n = o as XNode;
                        if (n != null)
-                               return n;
-                       if (o is string)
-                               return new XText ((string) o);
-                       if (o is IEnumerable)
-                               throw new NotImplementedException ();
-                       return new XText (o.ToString ());
+                               yield return n;
+                       else if (o is string)
+                               yield return new XText ((string) o);
+                       else if (o is IEnumerable)
+                               foreach (object obj in (IEnumerable) o)
+                                       foreach (XNode nn in ToNodes (obj))
+                                               yield return nn;
+                       else
+                               yield return new XText (o.ToString ());
                }
 
                public static object Clone (object o)