+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.
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);
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);
}
}
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);
}
}
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)
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 ();
{
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)
{
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)
WriteAttribute ((XAttribute) o, w);
else
// FIXME: check node validity
- XUtil.ToNode (o).WriteTo (w);
+ foreach (XNode n in XUtil.ToNodes (o))
+ n.WriteTo (w);
}
}
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)