BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / XElement.cs
1 //
2 // Authors:
3 //   Atsushi Enomoto
4 //
5 // Copyright 2007 Novell (http://www.novell.com)
6 //
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:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
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.
25 //
26
27 using System;
28 using System.Collections;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Linq;
32 using System.Text;
33 using System.Xml;
34 using System.Xml.Schema;
35 using System.Xml.Serialization;
36
37 namespace System.Xml.Linq
38 {
39         [XmlSchemaProvider (null, IsAny = true)]
40         public class XElement : XContainer, IXmlSerializable
41         {
42                 static IEnumerable <XElement> emptySequence =
43                         new List <XElement> ();
44
45                 public static IEnumerable <XElement> EmptySequence {
46                         get { return emptySequence; }
47                 }
48
49                 XName name;
50                 XAttribute attr_first, attr_last;
51                 bool explicit_is_empty = true;
52
53                 public XElement (XName name, object value)
54                 {
55                         this.name = name;
56                         Add (value);
57                 }
58
59                 public XElement (XElement source)
60                 {
61                         name = source.name;
62                         Add (source.Attributes ());
63                         Add (source.Nodes ());
64                 }
65
66                 public XElement (XName name)
67                 {
68                         this.name = name;
69                 }
70
71                 public XElement (XName name, params object [] contents)
72                 {
73                         this.name = name;
74                         Add (contents);
75                 }
76
77                 public XElement (XStreamingElement source)
78                 {
79                         this.name = source.Name;
80                         Add (source.Contents);
81                 }
82
83                 [CLSCompliant (false)]
84                 public static explicit operator bool (XElement element)
85                 {
86                         if (element == null)
87                                 throw new ArgumentNullException ("element");
88                         return XUtil.ConvertToBoolean (element.Value);
89                 }
90
91                 [CLSCompliant (false)]
92                 public static explicit operator bool? (XElement element)
93                 {
94                         if (element == null)
95                                 return null;
96                         
97                         return element.Value == null ? (bool?) null : XUtil.ConvertToBoolean (element.Value);
98                 }
99
100                 [CLSCompliant (false)]
101                 public static explicit operator DateTime (XElement element)
102                 {
103                         if (element == null)
104                                 throw new ArgumentNullException ("element");
105                         return XUtil.ToDateTime (element.Value);
106                 }
107
108                 [CLSCompliant (false)]
109                 public static explicit operator DateTime? (XElement element)
110                 {
111                         if (element == null)
112                                 return null;
113                         
114                         return element.Value == null ? (DateTime?) null : XUtil.ToDateTime (element.Value);
115                 }
116
117 #if !TARGET_JVM // Same as for System.Xml.XmlConvert.ToDateTimeOffset
118
119                 [CLSCompliant (false)]
120                 public static explicit operator DateTimeOffset (XElement element)
121                 {
122                         if (element == null)
123                                 throw new ArgumentNullException ("element");
124                         return XmlConvert.ToDateTimeOffset (element.Value);
125                 }
126
127                 [CLSCompliant (false)]
128                 public static explicit operator DateTimeOffset? (XElement element)
129                 {
130                         if (element == null)
131                                 return null;
132                         
133                         return element.Value == null ? (DateTimeOffset?) null : XmlConvert.ToDateTimeOffset (element.Value);
134                 }
135
136 #endif
137
138                 [CLSCompliant (false)]
139                 public static explicit operator decimal (XElement element)
140                 {
141                         if (element == null)
142                                 throw new ArgumentNullException ("element");
143                         return XmlConvert.ToDecimal (element.Value);
144                 }
145
146                 [CLSCompliant (false)]
147                 public static explicit operator decimal? (XElement element)
148                 {
149                         if (element == null)
150                                 return null;
151                         
152                         return element.Value == null ? (decimal?) null : XmlConvert.ToDecimal (element.Value);
153                 }
154
155                 [CLSCompliant (false)]
156                 public static explicit operator double (XElement element)
157                 {
158                         if (element == null)
159                                 throw new ArgumentNullException ("element");
160                         return XmlConvert.ToDouble (element.Value);
161                 }
162
163                 [CLSCompliant (false)]
164                 public static explicit operator double? (XElement element)
165                 {
166                         if (element == null)
167                                 return null;
168                         
169                         return element.Value == null ? (double?) null : XmlConvert.ToDouble (element.Value);
170                 }
171
172                 [CLSCompliant (false)]
173                 public static explicit operator float (XElement element)
174                 {
175                         if (element == null)
176                                 throw new ArgumentNullException ("element");
177                         return XmlConvert.ToSingle (element.Value);
178                 }
179
180                 [CLSCompliant (false)]
181                 public static explicit operator float? (XElement element)
182                 {
183                         if (element == null)
184                                 return null;
185                         
186                         return element.Value == null ? (float?) null : XmlConvert.ToSingle (element.Value);
187                 }
188
189                 [CLSCompliant (false)]
190                 public static explicit operator Guid (XElement element)
191                 {
192                         if (element == null)
193                                 throw new ArgumentNullException ("element");
194                         return XmlConvert.ToGuid (element.Value);
195                 }
196
197                 [CLSCompliant (false)]
198                 public static explicit operator Guid? (XElement element)
199                 {
200                         if (element == null)
201                                 return null;
202                         
203                         return element.Value == null ? (Guid?) null : XmlConvert.ToGuid (element.Value);
204                 }
205
206                 [CLSCompliant (false)]
207                 public static explicit operator int (XElement element)
208                 {
209                         if (element == null)
210                                 throw new ArgumentNullException ("element");
211                         return XmlConvert.ToInt32 (element.Value);
212                 }
213
214                 [CLSCompliant (false)]
215                 public static explicit operator int? (XElement element)
216                 {
217                         if (element == null)
218                                 return null;
219                         
220                         return element.Value == null ? (int?) null : XmlConvert.ToInt32 (element.Value);
221                 }
222
223                 [CLSCompliant (false)]
224                 public static explicit operator long (XElement element)
225                 {
226                         if (element == null)
227                                 throw new ArgumentNullException ("element");
228                         return XmlConvert.ToInt64 (element.Value);
229                 }
230
231                 [CLSCompliant (false)]
232                 public static explicit operator long? (XElement element)
233                 {
234                         if (element == null)
235                                 return null;
236                         
237                         return element.Value == null ? (long?) null : XmlConvert.ToInt64 (element.Value);
238                 }
239
240                 [CLSCompliant (false)]
241                 public static explicit operator uint (XElement element)
242                 {
243                         if (element == null)
244                                 throw new ArgumentNullException ("element");
245                         return XmlConvert.ToUInt32 (element.Value);
246                 }
247
248                 [CLSCompliant (false)]
249                 public static explicit operator uint? (XElement element)
250                 {
251                         if (element == null)
252                                 return null;
253                         
254                         return element.Value == null ? (uint?) null : XmlConvert.ToUInt32 (element.Value);
255                 }
256
257                 [CLSCompliant (false)]
258                 public static explicit operator ulong (XElement element)
259                 {
260                         if (element == null)
261                                 throw new ArgumentNullException ("element");
262                         return XmlConvert.ToUInt64 (element.Value);
263                 }
264
265                 [CLSCompliant (false)]
266                 public static explicit operator ulong? (XElement element)
267                 {
268                         if (element == null)
269                                 return null;
270                         
271                         return element.Value == null ? (ulong?) null : XmlConvert.ToUInt64 (element.Value);
272                 }
273
274                 [CLSCompliant (false)]
275                 public static explicit operator TimeSpan (XElement element)
276                 {
277                         if (element == null)
278                                 throw new ArgumentNullException ("element");
279                         return XmlConvert.ToTimeSpan (element.Value);
280                 }
281
282                 [CLSCompliant (false)]
283                 public static explicit operator TimeSpan? (XElement element)
284                 {
285                         if (element == null)
286                                 return null;
287                         
288                         return element.Value == null ? (TimeSpan?) null : XmlConvert.ToTimeSpan (element.Value);
289                 }
290
291                 [CLSCompliant (false)]
292                 public static explicit operator string (XElement element)
293                 {
294                         if (element == null)
295                                 return null;
296                         
297                         return element.Value;
298                 }
299
300                 public XAttribute FirstAttribute {
301                         get { return attr_first; }
302                         internal set { attr_first = value; }
303                 }
304
305                 public XAttribute LastAttribute {
306                         get { return attr_last; }
307                         internal set { attr_last = value; }
308                 }
309
310                 public bool HasAttributes {
311                         get { return attr_first != null; }
312                 }
313
314                 public bool HasElements {
315                         get {
316                                 foreach (object o in Nodes ())
317                                         if (o is XElement)
318                                                 return true;
319                                 return false;
320                         }
321                 }
322
323                 public bool IsEmpty {
324                         get { return !Nodes ().GetEnumerator ().MoveNext () && explicit_is_empty; }
325                         internal set { explicit_is_empty = value; }
326                 }
327
328                 public XName Name {
329                         get { return name; }
330                         set {
331                                 if (value == null)
332                                         throw new ArgumentNullException ("Name");
333                                 name = value;
334                         }
335                 }
336
337                 public override XmlNodeType NodeType {
338                         get { return XmlNodeType.Element; }
339                 }
340
341                 public string Value {
342                         get {
343                                 StringBuilder sb = null;
344                                 foreach (XNode n in Nodes ()) {
345                                         if (sb == null)
346                                                 sb = new StringBuilder ();
347                                         if (n is XText)
348                                                 sb.Append (((XText) n).Value);
349                                         else if (n is XElement)
350                                                 sb.Append (((XElement) n).Value);
351                                 }
352                                 return sb == null ? String.Empty : sb.ToString ();
353                         }
354                         set {
355                                 RemoveNodes ();
356                                 Add (value);
357                         }
358                 }
359
360                 IEnumerable <XElement> GetAncestorList (XName name, bool getMeIn)
361                 {
362                         List <XElement> list = new List <XElement> ();
363                         if (getMeIn)
364                                 list.Add (this);
365                         for (XElement el = Parent as XElement; el != null; el = el.Parent as XElement)
366                                 if (name == null || el.Name == name)
367                                         list.Add (el);
368                         return list;
369                 }
370
371                 public XAttribute Attribute (XName name)
372                 {
373                         foreach (XAttribute a in Attributes ())
374                                 if (a.Name == name)
375                                         return a;
376                         return null;
377                 }
378
379                 public IEnumerable <XAttribute> Attributes ()
380                 {
381                         XAttribute next;
382                         for (XAttribute a = attr_first; a != null; a = next) {
383                                 next = a.NextAttribute;
384                                 yield return a;
385                         }
386                 }
387
388                 // huh?
389                 public IEnumerable <XAttribute> Attributes (XName name)
390                 {
391                         foreach (XAttribute a in Attributes ())
392                                 if (a.Name == name)
393                                         yield return a;
394                 }
395
396                 static void DefineDefaultSettings (XmlReaderSettings settings, LoadOptions options)
397                 {
398 #if MOONLIGHT
399                         // 2.1 has a DtdProcessing property which defaults to DtdProcessing.Prohibit
400                         settings.DtdProcessing = DtdProcessing.Parse;
401 #else
402                         settings.ProhibitDtd = false;
403 #endif
404
405                         settings.IgnoreWhitespace = (options & LoadOptions.PreserveWhitespace) == 0;
406                 }
407
408                 static XmlReaderSettings CreateDefaultSettings (LoadOptions options)
409                 {
410                         var settings = new XmlReaderSettings ();
411                         DefineDefaultSettings (settings, options);
412                         return settings;
413                 }
414
415                 public static XElement Load (string uri)
416                 {
417                         return Load (uri, LoadOptions.None);
418                 }
419
420                 public static XElement Load (string uri, LoadOptions options)
421                 {
422                         XmlReaderSettings s = CreateDefaultSettings (options);
423
424                         using (XmlReader r = XmlReader.Create (uri, s)) {
425                                 return LoadCore (r, options);
426                         }
427                 }
428
429                 public static XElement Load (TextReader tr)
430                 {
431                         return Load (tr, LoadOptions.None);
432                 }
433
434                 public static XElement Load (TextReader tr, LoadOptions options)
435                 {
436                         XmlReaderSettings s = CreateDefaultSettings (options);
437
438                         using (XmlReader r = XmlReader.Create (tr, s)) {
439                                 return LoadCore (r, options);
440                         }
441                 }
442
443                 public static XElement Load (XmlReader reader)
444                 {
445                         return Load (reader, LoadOptions.None);
446                 }
447
448                 public static XElement Load (XmlReader reader, LoadOptions options)
449                 {
450                         XmlReaderSettings s = reader.Settings != null ? reader.Settings.Clone () : new XmlReaderSettings ();
451                         DefineDefaultSettings (s, options);
452
453                         using (XmlReader r = XmlReader.Create (reader, s)) {
454                                 return LoadCore (r, options);
455                         }
456                 }
457
458 #if MOONLIGHT || MOBILE || NET_4_0
459                 public static XElement Load (Stream stream)
460                 {
461                         return Load (stream, LoadOptions.None);
462                 }
463
464                 public static XElement Load (Stream stream, LoadOptions options)
465                 {
466                         XmlReaderSettings s = new XmlReaderSettings ();
467                         DefineDefaultSettings (s, options);
468
469                         using (XmlReader r = XmlReader.Create (stream, s)) {
470                                 return LoadCore (r, options);
471                         }
472                 }
473 #endif
474
475                 internal static XElement LoadCore (XmlReader r, LoadOptions options)
476                 {
477                         r.MoveToContent ();
478                         if (r.NodeType != XmlNodeType.Element)
479                                 throw new InvalidOperationException ("The XmlReader must be positioned at an element");
480                         XName name = XName.Get (r.LocalName, r.NamespaceURI);
481                         XElement e = new XElement (name);
482                         e.FillLineInfoAndBaseUri (r, options);
483
484                         if (r.MoveToFirstAttribute ()) {
485                                 do {
486                                         // not sure how current Orcas behavior makes sense here though ...
487                                         if (r.LocalName == "xmlns" && r.NamespaceURI == XNamespace.Xmlns.NamespaceName)
488                                                 e.SetAttributeValue (XNamespace.None.GetName ("xmlns"), r.Value);
489                                         else
490                                                 e.SetAttributeValue (XName.Get (r.LocalName, r.NamespaceURI), r.Value);
491                                         e.LastAttribute.FillLineInfoAndBaseUri (r, options);
492                                 } while (r.MoveToNextAttribute ());
493                                 r.MoveToElement ();
494                         }
495                         if (!r.IsEmptyElement) {
496                                 r.Read ();
497                                 e.ReadContentFrom (r, options);
498                                 r.ReadEndElement ();
499                                 e.explicit_is_empty = false;
500                         } else {
501                                 e.explicit_is_empty = true;
502                                 r.Read ();
503                         }
504                         return e;
505                 }
506
507                 public static XElement Parse (string s)
508                 {
509                         return Parse (s, LoadOptions.None);
510                 }
511
512                 public static XElement Parse (string s, LoadOptions options)
513                 {
514                         return Load (new StringReader (s), options);
515                 }
516
517                 public void RemoveAll ()
518                 {
519                         RemoveAttributes ();
520                         RemoveNodes ();
521                 }
522
523                 public void RemoveAttributes ()
524                 {
525                         while (attr_first != null)
526                                 attr_last.Remove ();
527                 }
528
529                 public void Save (string filename)
530                 {
531                         Save (filename, SaveOptions.None);
532                 }
533
534                 public void Save (string filename, SaveOptions options)
535                 {
536                         XmlWriterSettings s = new XmlWriterSettings ();
537
538                         if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
539                                 s.Indent = true;
540 #if NET_4_0 || MOONLIGHT || MOBILE
541                         if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
542                                 s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
543 #endif
544                         using (XmlWriter w = XmlWriter.Create (filename, s)) {
545                                 Save (w);
546                         }
547                 }
548
549                 public void Save (TextWriter tw)
550                 {
551                         Save (tw, SaveOptions.None);
552                 }
553
554                 public void Save (TextWriter tw, SaveOptions options)
555                 {
556                         XmlWriterSettings s = new XmlWriterSettings ();
557                         
558                         if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
559                                 s.Indent = true;
560 #if NET_4_0 || MOONLIGHT || MOBILE
561                         if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
562                                 s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
563 #endif
564                         using (XmlWriter w = XmlWriter.Create (tw, s)) {
565                                 Save (w);
566                         }
567                 }
568
569                 public void Save (XmlWriter w)
570                 {
571                         WriteTo (w);
572                 }
573
574 #if NET_4_0 || MOONLIGHT || MOBILE
575                 public void Save (Stream stream)
576                 {
577                         Save (stream, SaveOptions.None);
578                 }
579
580                 public void Save (Stream stream, SaveOptions options)
581                 {
582                         XmlWriterSettings s = new XmlWriterSettings ();
583                         if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
584                                 s.Indent = true;
585                         if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
586                                 s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
587
588                         using (var writer = XmlWriter.Create (stream, s)){
589                                 Save (writer);
590                         }
591                 }
592 #endif
593                 public IEnumerable <XElement> AncestorsAndSelf ()
594                 {
595                         return GetAncestorList (null, true);
596                 }
597
598                 public IEnumerable <XElement> AncestorsAndSelf (XName name)
599                 {
600                         return GetAncestorList (name, true);
601                 }
602
603                 public IEnumerable <XElement> DescendantsAndSelf ()
604                 {
605                         List <XElement> list = new List <XElement> ();
606                         list.Add (this);
607                         list.AddRange (Descendants ());
608                         return list;
609                 }
610
611                 public IEnumerable <XElement> DescendantsAndSelf (XName name)
612                 {
613                         List <XElement> list = new List <XElement> ();
614                         if (name == this.name)
615                                 list.Add (this);
616                         list.AddRange (Descendants (name));
617                         return list;
618                 }
619
620                 public IEnumerable <XNode> DescendantNodesAndSelf ()
621                 {
622                         yield return this;
623                         foreach (XNode node in DescendantNodes ())
624                                 yield return node;
625                 }
626
627                 public void SetAttributeValue (XName name, object value)
628                 {
629                         XAttribute a = Attribute (name);
630                         if (value == null) {
631                                 if (a != null)
632                                         a.Remove ();
633                         } else {
634                                 if (a == null) {
635                                         SetAttributeObject (new XAttribute (name, value));
636                                 }
637                                 else
638                                         a.Value = XUtil.ToString (value);
639                         }
640                 }
641
642                 void SetAttributeObject (XAttribute a)
643                 {
644                         a = (XAttribute) XUtil.GetDetachedObject (a);
645                         a.SetOwner (this);
646                         if (attr_first == null) {
647                                 attr_first = a;
648                                 attr_last = a;
649                         } else {
650                                 attr_last.NextAttribute = a;
651                                 a.PreviousAttribute = attr_last;
652                                 attr_last = a;
653                         }
654                 }
655
656                 string LookupPrefix (string ns, XmlWriter w)
657                 {
658                         string prefix = ns.Length > 0 ? w.LookupPrefix (ns) : String.Empty;
659                         foreach (XAttribute a in Attributes ()) {
660                                 if (a.IsNamespaceDeclaration && a.Value == ns) {
661                                         if (a.Name.Namespace == XNamespace.Xmlns)
662                                                 prefix = a.Name.LocalName;
663                                         // otherwise xmlns="..."
664                                         break;
665                                 }
666                         }
667                         return prefix;
668                 }
669
670                 public override void WriteTo (XmlWriter w)
671                 {
672                         // some people expect the same prefix output as in input,
673                         // in the loss of performance... see bug #466423.
674                         int createdNS = 0;
675                         string prefix = LookupPrefix (name.NamespaceName, w);
676                         Func<string> nsCreator = () => {
677                                 string p = null;
678                                 do {
679                                         p = "p" + (++createdNS);
680                                         // check conflict
681                                         if (Attributes ().All (a => a.Name.LocalName != p))
682                                                 break;
683                                 } while (true);
684                                 return p;
685                                 };
686                         if (prefix == null)
687                                 prefix = nsCreator ();
688
689                         w.WriteStartElement (prefix, name.LocalName, name.Namespace.NamespaceName);
690
691                         foreach (XAttribute a in Attributes ()) {
692                                 if (a.IsNamespaceDeclaration) {
693                                         if (a.Name.Namespace == XNamespace.Xmlns)
694                                                 w.WriteAttributeString ("xmlns", a.Name.LocalName, XNamespace.Xmlns.NamespaceName, a.Value);
695                                         else
696                                                 w.WriteAttributeString ("xmlns", a.Value);
697                                 } else {
698                                         string apfix = LookupPrefix (a.Name.NamespaceName, w);
699                                         if (apfix == null)
700                                                 apfix = nsCreator ();
701                                         w.WriteAttributeString (apfix, a.Name.LocalName, a.Name.Namespace.NamespaceName, a.Value);
702                                 }
703                         }
704
705                         foreach (XNode node in Nodes ())
706                                 node.WriteTo (w);
707
708                         if (explicit_is_empty)
709                                 w.WriteEndElement ();
710                         else
711                                 w.WriteFullEndElement ();
712                 }
713
714                 public XNamespace GetDefaultNamespace ()
715                 {
716                         for (XElement el = this; el != null; el = el.Parent)
717                                 foreach (XAttribute a in el.Attributes ())
718                                         if (a.IsNamespaceDeclaration && a.Name.Namespace == XNamespace.None)
719                                                 return XNamespace.Get (a.Value);
720                         return XNamespace.None; // nothing is declared.
721                 }
722
723                 public XNamespace GetNamespaceOfPrefix (string prefix)
724                 {
725                         for (XElement el = this; el != null; el = el.Parent)
726                                 foreach (XAttribute a in el.Attributes ())
727                                         if (a.IsNamespaceDeclaration && (prefix.Length == 0 && a.Name.LocalName == "xmlns" || a.Name.LocalName == prefix))
728                                                 return XNamespace.Get (a.Value);
729                         return XNamespace.None; // nothing is declared.
730                 }
731
732                 public string GetPrefixOfNamespace (XNamespace ns)
733                 {
734                         foreach (string prefix in GetPrefixOfNamespaceCore (ns))
735                                 if (GetNamespaceOfPrefix (prefix) == ns)
736                                         return prefix;
737                         return null; // nothing is declared
738                 }
739                 
740                 IEnumerable<string> GetPrefixOfNamespaceCore (XNamespace ns)
741                 {
742                         for (XElement el = this; el != null; el = el.Parent)
743                                 foreach (XAttribute a in el.Attributes ())
744                                         if (a.IsNamespaceDeclaration && a.Value == ns.NamespaceName)
745                                                 yield return a.Name.Namespace == XNamespace.None ? String.Empty : a.Name.LocalName;
746                 }
747
748                 public void ReplaceAll (object item)
749                 {
750                         RemoveNodes ();
751                         Add (item);
752                 }
753
754                 public void ReplaceAll (params object [] items)
755                 {
756                         RemoveNodes ();
757                         Add (items);
758                 }
759
760                 public void ReplaceAttributes (object item)
761                 {
762                         RemoveAttributes ();
763                         Add (item);
764                 }
765
766                 public void ReplaceAttributes (params object [] items)
767                 {
768                         RemoveAttributes ();
769                         Add (items);
770                 }
771
772                 public void SetElementValue (XName name, object value)
773                 {
774                         var element = Element (name);
775                         if (element == null && value != null) {
776                                 Add (new XElement (name, value));
777                         } else if (element != null && value == null) {
778                                 element.Remove ();
779                         } else
780                                 element.SetValue (value);
781                 }
782
783                 public void SetValue (object value)
784                 {
785                         if (value == null)
786                                 throw new ArgumentNullException ("value");
787                         if (value is XAttribute || value is XDocument || value is XDeclaration || value is XDocumentType)
788                                 throw new ArgumentException (String.Format ("Node type {0} is not allowed as element value", value.GetType ()));
789                         RemoveNodes ();
790                         foreach (object o in XUtil.ExpandArray (value))
791                                 Add (o);
792                 }
793
794                 internal override bool OnAddingObject (object o, bool rejectAttribute, XNode refNode, bool addFirst)
795                 {
796                         if (o is XDocument || o is XDocumentType || o is XDeclaration || (rejectAttribute && o is XAttribute))
797                                 throw new ArgumentException (String.Format ("A node of type {0} cannot be added as a content", o.GetType ()));
798
799                         XAttribute a = o as XAttribute;
800                         if (a != null) {
801                                 foreach (XAttribute ia in Attributes ())
802                                         if (a.Name == ia.Name)
803                                                 throw new InvalidOperationException (String.Format ("Duplicate attribute: {0}", a.Name));
804                                 SetAttributeObject (a);
805                                 return true;
806                         }
807                         else if (o is string && refNode is XText) {
808                                 ((XText) refNode).Value += o as string;
809                                 return true;
810                         }
811                         else
812                                 return false;
813                 }
814
815                 void IXmlSerializable.WriteXml (XmlWriter writer)
816                 {
817                         Save (writer);
818                 }
819
820                 void IXmlSerializable.ReadXml (XmlReader reader)
821                 {
822                         ReadContentFrom (reader, LoadOptions.None);
823                 }
824
825                 XmlSchema IXmlSerializable.GetSchema ()
826                 {
827                         return null;
828                 }
829         }
830 }