5 // Copyright 2007 Novell (http://www.novell.com)
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Collections;
29 using System.Collections.Generic;
34 namespace System.Xml.Linq
36 public abstract class XContainer : XNode
38 internal XContainer ()
45 public XNode FirstNode {
47 internal set { first = value; }
50 public XNode LastNode {
52 internal set { last = value; }
55 void CheckChildType (object o, bool addFirst)
57 if (o == null || o is string || o is XNode)
59 if (o is IEnumerable) {
60 foreach (object oc in ((IEnumerable) o))
61 CheckChildType (oc, addFirst);
65 throw new ArgumentException ("Invalid child type: " + o.GetType ());
69 private void AddAttribute (XAttribute attr)
72 ((XElement) this).SetAttributeNode (attr);
74 throw new ArgumentException ("Attribute is not allowed here.");
78 public void Add (object content)
83 foreach (object o in XUtil.ExpandArray (content))
84 if (!OnAddingObject (o))
85 foreach (XNode n in XUtil.ToNodes (o))
89 void AddNode (XNode n)
91 CheckChildType (n, false);
98 n.PreviousNode = last;
103 public void Add (params object [] content)
107 foreach (object o in XUtil.ExpandArray (content))
111 public void AddFirst (object content)
116 AddFirst (XUtil.ExpandArray (content));
119 public void AddFirst (params object [] content)
126 foreach (object o in XUtil.ExpandArray (content))
127 if (!OnAddingObject (o))
128 first.AddBeforeSelf (o);
131 internal virtual bool OnAddingObject (object o)
136 public XmlWriter CreateWriter ()
138 return new XNodeWriter (this);
141 public IEnumerable <XNode> Nodes ()
144 for (XNode n = FirstNode; n != null; n = next) {
150 public IEnumerable<XNode> DescendantNodes ()
152 foreach (XNode n in Nodes ()) {
154 XContainer c = n as XContainer;
156 foreach (XNode d in c.DescendantNodes ())
161 public IEnumerable <XElement> Descendants ()
163 foreach (XNode n in DescendantNodes ()) {
164 XElement el = n as XElement;
170 public IEnumerable <XElement> Descendants (XName name)
172 foreach (XElement el in Descendants ())
177 public IEnumerable <XElement> Elements ()
179 foreach (XNode n in Nodes ()) {
180 XElement el = n as XElement;
186 public IEnumerable <XElement> Elements (XName name)
188 foreach (XElement el in Elements ())
193 public XElement Element (XName name)
195 foreach (XElement el in Elements ())
201 internal void ReadContentFrom (XmlReader reader, LoadOptions options)
203 while (!reader.EOF) {
204 if (reader.NodeType == XmlNodeType.EndElement)
205 // end of the element.
207 Add (XNode.ReadFrom (reader, options));
211 public void RemoveNodes ()
213 foreach (XNode n in Nodes ())
217 public void ReplaceNodes (object content)
223 public void ReplaceNodes (params object [] content)
230 public void WriteContentTo (XmlWriter writer)
232 foreach (object o in Content ()) {
234 writer.WriteString ((string) o);
236 ((XNode) o).WriteTo (writer);
238 throw new SystemException ("INTERNAL ERROR: list content item was " + o.GetType ());