[runtime] Updates comments.
[mono.git] / mcs / class / System.Xml.Linq / Test / System.Xml.Linq / XElementTest.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.Generic;
29 using System.Globalization;
30 using System.IO;
31 using System.Linq;
32 using System.Threading;
33 using System.Xml;
34 using System.Xml.Linq;
35 using System.Xml.Serialization;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Xml.Linq
40 {
41         [TestFixture]
42         public class XElementTest
43         {
44
45                 [Test]
46                 public void Constructor_NullParameters()
47                 {
48                         AssertThrows<ArgumentNullException>(() => new XElement((XName)null), "#1");
49                         AssertThrows<ArgumentNullException>(() => new XElement((XElement)null), "#2");
50                         AssertThrows<ArgumentNullException>(() => new XElement((XStreamingElement)null), "#3");
51                         AssertThrows<ArgumentNullException>(() => new XElement((XName)null, null), "#4");
52                         AssertThrows<ArgumentNullException>(() => new XElement((XName)null, null, null, null), "#5");
53
54                         // This is acceptable though
55                         new XElement(XName.Get("foo"), null);
56                 }
57
58                 [Test] // xml declaration is skipped.
59                 public void LoadWithXmldecl ()
60                 {
61                         string xml = "<?xml version='1.0'?><root />";
62                         XElement.Load (new StringReader (xml));
63                 }
64
65                 [Test]
66                 public void Load1 ()
67                 {
68                         string xml = "<root><foo/></root>";
69
70                         XElement el = XElement.Load (new StringReader (xml));
71                         XElement first = el.FirstNode as XElement;
72                         Assert.IsNotNull (first, "#1");
73                         Assert.IsTrue (el.LastNode is XElement, "#2");
74                         Assert.IsNull (el.NextNode, "#3");
75                         Assert.IsNull (el.PreviousNode, "#4");
76                         Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#5");
77                         Assert.AreEqual (el, first.Parent, "#6");
78                         Assert.AreEqual (first, el.LastNode, "#7");
79
80                         Assert.AreEqual ("root", el.Name.ToString (), "#8");
81                         Assert.AreEqual ("foo", first.Name.ToString (), "#9");
82                         Assert.IsFalse (el.Attributes ().GetEnumerator ().MoveNext (), "#10");
83                 }
84
85                 [Test]
86                 [ExpectedException (typeof (InvalidOperationException))]
87                 public void LoadInvalid ()
88                 {
89                         string xml = "text";
90                         XmlReaderSettings s = new XmlReaderSettings ();
91                         s.ConformanceLevel = ConformanceLevel.Fragment;
92
93                         XElement.Load (XmlReader.Create (new StringReader (xml), s));
94                 }
95
96                 [Test]
97                 public void PrecedingWhitespaces ()
98                 {
99                         string xml = "  <root/>";
100                         XmlReaderSettings s = new XmlReaderSettings ();
101                         s.ConformanceLevel = ConformanceLevel.Fragment;
102
103                         XElement.Load (XmlReader.Create (new StringReader (xml), s));
104                 }
105
106                 [Test]
107                 public void PrecedingWhitespaces2 ()
108                 {
109                         string xml = "  <root/>";
110                         XmlReaderSettings s = new XmlReaderSettings ();
111                         s.ConformanceLevel = ConformanceLevel.Fragment;
112
113                         XmlReader r = XmlReader.Create (new StringReader (xml), s);
114                         r.Read (); // at whitespace
115                         XElement.Load (r);
116                 }
117
118                 [Test]
119                 public void Rename()
120                 {
121                         bool changed = false;
122                         bool changing = false;
123                         var element = new XElement("foo");
124                         element.Changing += (o, e) => {
125                                 Assert.IsFalse (changing, "#1");
126                                 Assert.IsFalse (changed, "#2");
127                                 Assert.AreSame (element, o, "#3");
128                                 Assert.AreEqual (XObjectChange.Name, e.ObjectChange, "#4");
129                                 changing = true;
130                         };
131
132                         element.Changed += (o, e) => {
133                                 Assert.IsTrue (changing, "#5");
134                                 Assert.IsFalse (changed, "#6");
135                                 Assert.AreSame (element, o, "#7");
136                                 Assert.AreEqual (XObjectChange.Name, e.ObjectChange, "#8");
137                                 changed = true;
138                         };
139
140                         element.Name = "bar";
141                         Assert.AreEqual("bar", element.Name.LocalName, "#name");
142                         Assert.IsTrue(changed, "changed");
143                 }
144
145                 [Test]
146                 public void Load2 ()
147                 {
148                         string xml = "<root>foo</root>";
149
150                         XElement el = XElement.Load (new StringReader (xml));
151                         XText first = el.FirstNode as XText;
152                         Assert.IsNotNull (first, "#1");
153                         Assert.IsTrue (el.LastNode is XText, "#2");
154                         Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#3");
155                         Assert.AreEqual (el, first.Parent, "#4");
156                         Assert.AreEqual (first, el.LastNode, "#5");
157
158                         Assert.AreEqual ("foo", first.Value, "#6");
159                 }
160
161                 [Test]
162                 [ExpectedException (typeof (ArgumentException))]
163                 public void AddDocumentTypeToElement ()
164                 {
165                         XElement el = new XElement (XName.Get ("foo"));
166                         el.Add (new XDocumentType ("foo", null, null, null));
167                 }
168
169                 [Test]
170                 [ExpectedException (typeof (ArgumentException))]
171                 [Category ("NotDotNet")]
172                 [Ignore ("see inline comment")]
173                 public void AddXDeclarationToElement ()
174                 {
175                         XElement el = new XElement (XName.Get ("foo"));
176                         // LAMESPEC: in .NET, XDeclaration is not treated as
177                         // invalid, and converted to a string without error.
178                         el.Add (new XDeclaration ("1.0", null, null));
179                 }
180
181                 [Test]
182                 public void SetAttribute ()
183                 {
184                         XElement el = new XElement (XName.Get ("foo"));
185                         el.SetAttributeValue (XName.Get ("a1"), "v1");
186                         XAttribute a = el.FirstAttribute;
187                         Assert.IsNotNull (a, "#1-1");
188                         Assert.AreEqual (el, a.Parent, "#1-2");
189                         Assert.IsNotNull (el.LastAttribute, "#1-3");
190                         Assert.AreEqual (a, el.LastAttribute, "#1-4");
191                         Assert.AreEqual ("a1", a.Name.LocalName, "#1-5");
192                         Assert.AreEqual ("v1", a.Value, "#1-6");
193                         Assert.IsNull (a.PreviousAttribute, "#1-7");
194                         Assert.IsNull (a.NextAttribute, "#1-8");
195
196                         el.SetAttributeValue (XName.Get ("a2"), "v2");
197                         Assert.IsFalse (el.FirstAttribute == el.LastAttribute, "#2-1");
198                         Assert.AreEqual ("a2", el.LastAttribute.Name.LocalName, "#2-2");
199
200                         el.SetAttributeValue (XName.Get ("a1"), "v3");
201                         XAttribute b = el.FirstAttribute;
202                         Assert.IsNotNull (b, "#2-3");
203                         Assert.IsNotNull (el.LastAttribute, "#2-4");
204                         Assert.AreEqual ("a1", b.Name.LocalName, "#2-5");
205                         Assert.AreEqual ("v3", b.Value, "#2-6");
206                         Assert.AreEqual (a, b, "#2-7");
207                         XAttribute c = el.LastAttribute;
208                         Assert.AreEqual (a, c.PreviousAttribute, "#2-8");
209
210                         a.Remove ();
211                         Assert.IsNull (a.Parent, "#3-1");
212                         Assert.IsNull (a.PreviousAttribute, "#3-2");
213                         Assert.IsNull (a.NextAttribute, "#3-3");
214                         Assert.IsNull (c.PreviousAttribute, "#3-4");
215                         Assert.IsNull (c.NextAttribute, "#3-5");
216
217                         el.RemoveAttributes ();
218                         Assert.IsFalse (el.HasAttributes, "#4-1");
219                         Assert.IsNull (b.Parent, "#4-2");
220                         Assert.IsNull (c.Parent, "#4-3");
221                         Assert.IsNull (el.FirstAttribute, "#4-4");
222                         Assert.IsNull (el.LastAttribute, "#4-5");
223                 }
224
225                 [Test]
226                 public void AddAfterSelf ()
227                 {
228                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
229                         el.FirstNode.AddAfterSelf ("text");
230                         XText t = el.FirstNode.NextNode as XText;
231                         Assert.IsNotNull (t, "#1");
232                         Assert.AreEqual ("text", t.Value, "#2");
233                         XElement bar = t.NextNode as XElement;
234                         Assert.IsNotNull (bar, "#3");
235                         Assert.AreEqual ("bar", bar.Name.LocalName, "#4");
236                 }
237
238                 [Test]
239                 public void AddAfterSelfList ()
240                 {
241                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
242                         el.FirstNode.AddAfterSelf (new XText [] {
243                                 new XText ("t1"),
244                                 new XText ("t2"),
245                                 new XText ("t3")});
246                         XText t = el.FirstNode.NextNode as XText;
247                         Assert.IsNotNull (t, "#1");
248                         Assert.AreEqual ("t1", t.Value, "#2");
249                         Assert.AreEqual ("t2", ((XText) t.NextNode).Value, "#3");
250                         Assert.AreEqual ("t3", ((XText) t.NextNode.NextNode).Value, "#4");
251                         XElement bar = t.NextNode.NextNode.NextNode as XElement;
252                         Assert.IsNotNull (bar, "#5");
253                         Assert.AreEqual ("bar", bar.Name.LocalName, "#6");
254                 }
255
256                 [Test]
257                 [ExpectedException (typeof (ArgumentException))]
258                 public void AddAfterSelfAttribute ()
259                 {
260                         var el = new XElement ("root", new XElement ("child"));
261                         var el2 = el.FirstNode as XElement;
262                         el2.AddAfterSelf (new XAttribute ("foo", "bar"));
263                 }
264
265                 [Test]
266                 [ExpectedException (typeof (ArgumentException))]
267                 public void AddAfterSelfXDocument ()
268                 {
269                         var el = new XElement ("root", new XElement ("child"));
270                         var el2 = el.FirstNode as XElement;
271                         el2.AddAfterSelf (new XDocument ());
272                 }
273
274                 [Test]
275                 [ExpectedException (typeof (ArgumentException))]
276                 [Category ("NotDotNet")]
277                 [Ignore ("see code comment")]
278                 // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
279                 public void AddAfterSelfXDeclaration ()
280                 {
281                         var el = new XElement ("root", new XElement ("child"));
282                         var el2 = el.FirstNode as XElement;
283                         el2.AddAfterSelf (new XDeclaration ("1.0", null, null));
284                 }
285
286                 [Test]
287                 public void AddAfterSelfCollection ()
288                 {
289                         var el = new XElement ("root", new XElement ("child"));
290                         el.FirstNode.AddAfterSelf (new List<XElement> (new XElement [] {new XElement ("foo"), new XElement ("bar")}));
291                         Assert.AreEqual ("<root><child /><foo /><bar /></root>", el.ToString (SaveOptions.DisableFormatting), "#1");
292                         Assert.AreEqual ("bar", (el.LastNode as XElement).Name.LocalName, "#2");
293                 }
294
295                 [Test]
296                 public void AddAfterSelfJoinsStringAfterText ()
297                 {
298                         var el = XElement.Parse ("<foo>text1</foo>");
299                         el.LastNode.AddAfterSelf ("text2");
300                         el.LastNode.AddAfterSelf (new XText ("text3"));
301                         IEnumerator<XNode> e = el.Nodes ().GetEnumerator ();
302                         Assert.IsTrue (e.MoveNext (), "#1");
303                         Assert.AreEqual ("text1text2", e.Current.ToString (), "#2");
304                         Assert.IsTrue (e.MoveNext (), "#3");
305                         Assert.AreEqual ("text3", e.Current.ToString (), "#4");
306                         Assert.IsFalse (e.MoveNext (), "#5");
307                 }
308
309                 [Test]
310                 public void AddBeforeSelf ()
311                 {
312                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
313                         el.FirstNode.AddBeforeSelf ("text");
314                         XText t = el.FirstNode as XText;
315                         Assert.IsNotNull (t, "#1");
316                         Assert.AreEqual ("text", t.Value, "#2");
317                         XElement foo = t.NextNode as XElement;
318                         Assert.IsNotNull (foo, "#3");
319                         Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
320                 }
321
322                 [Test]
323                 public void AddBeforeSelfList ()
324                 {
325                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
326                         el.FirstNode.AddBeforeSelf (new XText [] {
327                                 new XText ("t1"),
328                                 new XText ("t2"),
329                                 new XText ("t3")});
330                         XText t = el.FirstNode as XText;
331                         Assert.IsNotNull (t, "#1");
332                         Assert.AreEqual ("t1", t.Value, "#2");
333                         Assert.AreEqual ("t2", ((XText) t.NextNode).Value, "#3");
334                         Assert.AreEqual ("t3", ((XText) t.NextNode.NextNode).Value, "#4");
335                         XElement foo = t.NextNode.NextNode.NextNode as XElement;
336                         Assert.IsNotNull (foo, "#5");
337                         Assert.AreEqual ("foo", foo.Name.LocalName, "#6");
338                 }
339
340                 [Test]
341                 public void AddBeforeSelfList2 ()
342                 {
343                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
344                         el.FirstNode.AddBeforeSelf ("t1", "t2", "t3");
345                         XText t = el.FirstNode as XText;
346                         Assert.IsNotNull (t, "#1");
347                         Assert.AreEqual ("t1t2t3", t.Value, "#2");
348                         XElement foo = t.NextNode as XElement;
349                         Assert.IsNotNull (foo, "#3");
350                         Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
351                 }
352
353                 [Test]
354                 public void AddJoinsStringAfterText ()
355                 {
356                         var el = XElement.Parse ("<foo>text1</foo>");
357                         el.Add ("text2");
358                         el.Add (new XText ("text3"));
359                         IEnumerator<XNode> e = el.Nodes ().GetEnumerator ();
360                         Assert.IsTrue (e.MoveNext (), "#1");
361                         Assert.AreEqual ("text1text2", e.Current.ToString (), "#2");
362                         Assert.IsTrue (e.MoveNext (), "#3");
363                         Assert.AreEqual ("text3", e.Current.ToString (), "#4");
364                         Assert.IsFalse (e.MoveNext (), "#5");
365                 }
366
367                 [Test]
368                 [ExpectedException (typeof (InvalidOperationException))]
369                 public void AddDuplicateAttribute ()
370                 {
371                         var el = new XElement ("foo",
372                                 new XAttribute ("bar", "baz"));
373                         el.Add (new XAttribute ("bar", "baz"));
374                 }
375
376                 [Test]
377                 public void RemoveElement_FromChildNode_ChangeTriggers()
378                 {
379                         var childChanging = false;
380                         var childChanged = false;
381                         var rootChanging = false;
382                         var rootChanged = false;
383                         
384                         var subchild = new XElement("subfoo");
385                         var child = new XElement("foo", subchild);
386                         var root = new XElement("root", child);
387                         
388                         child.Changing += (o, e) => {
389                                 Assert.IsFalse(childChanging, "#c1");
390                                 Assert.IsFalse(childChanged, "#c2");
391                                 Assert.IsFalse(rootChanging, "#c3");
392                                 Assert.IsFalse(rootChanged, "#c4");
393                                 Assert.AreSame(subchild, o, "#c5");
394                                 Assert.AreEqual(XObjectChange.Remove, e.ObjectChange, "#c6");
395                                 Assert.IsNotNull(subchild.Parent, "childChangingParent");
396                                 childChanging = true;
397                         };
398                         root.Changing += (o, e) => {
399                                 Assert.IsTrue(childChanging, "#r1");
400                                 Assert.IsFalse(childChanged, "#r2");
401                                 Assert.IsFalse(rootChanging, "#r3");
402                                 Assert.IsFalse(rootChanged, "#r4");
403                                 Assert.AreSame(subchild, o, "#r5");
404                                 Assert.AreEqual(XObjectChange.Remove, e.ObjectChange, "#r6");
405                                 Assert.IsNotNull(subchild.Parent, "rootChangingParent");
406                                 rootChanging = true;
407                         };
408                         child.Changed += (o, e) =>  {
409                                 Assert.IsTrue(childChanging, "#c7");
410                                 Assert.IsFalse(childChanged, "#c8");
411                                 Assert.IsTrue(rootChanging, "#c9");
412                                 Assert.IsFalse(rootChanged, "#c10");
413                                 Assert.AreSame(subchild, o, "#c11");
414                                 Assert.AreEqual(XObjectChange.Remove, e.ObjectChange, "#c12");
415                                 Assert.IsNull(subchild.Parent, "childChangedParent");
416                                 childChanged = true;
417                         };
418                         root.Changed += (o, e) => {
419                                 Assert.IsTrue(childChanging, "#r7");
420                                 Assert.IsTrue(childChanged, "#r8");
421                                 Assert.IsTrue(rootChanging, "#r9");
422                                 Assert.IsFalse(rootChanged, "#r10");
423                                 Assert.AreSame(subchild, o, "#11");
424                                 Assert.AreEqual(XObjectChange.Remove, e.ObjectChange, "#12");
425                                 Assert.IsNull(subchild.Parent, "rootChangedParent");
426                                 rootChanged = true;
427                         };
428                         
429                         subchild.Remove();
430                         Assert.IsTrue(childChanging, "#a");
431                         Assert.IsTrue(childChanged, "#b");
432                         Assert.IsTrue(rootChanging, "#c");
433                         Assert.IsTrue(rootChanged, "#d");
434                 }
435
436                 [Test]
437                 public void RemoveElement_FromRootNode_ChangeTriggers()
438                 {
439                         var childChanging = false;
440                         var childChanged = false;
441                         var rootChanging = false;
442                         var rootChanged = false;
443                         
444                         var child = new XElement ("foo");
445                         var root = new XElement ("root", child);
446                         child.Changing += (o, e) => childChanging = true;
447                         child.Changed += (o, e) => childChanged = true;
448                         
449                         root.Changing += (o, e) => {
450                                 Assert.IsFalse(rootChanging, "#1");
451                                 Assert.IsFalse(rootChanged, "#2");
452                                 Assert.AreSame (child, o, "#3");
453                                 Assert.AreEqual(XObjectChange.Remove, e.ObjectChange, "#4");
454                                 rootChanging = true;
455                         };
456                         root.Changed += (o, e) =>  {
457                                 Assert.IsFalse(rootChanged, "#5");
458                                 Assert.IsTrue(rootChanging, "#6");
459                                 Assert.AreSame(child, o, "#7");
460                                 Assert.AreEqual(XObjectChange.Remove, e.ObjectChange, "#8");
461                                 rootChanged = true;
462                         };
463                         
464                         child.Remove();
465                         Assert.IsFalse(childChanging, "#9");
466                         Assert.IsFalse(childChanged, "#10");
467                         Assert.IsTrue(rootChanging, "#11");
468                         Assert.IsTrue(rootChanged, "#12");
469                 }
470
471                 [Test]
472                 public void ReplaceWith ()
473                 {
474                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
475                         XNode fc = el.FirstNode;
476                         fc.ReplaceWith ("test");
477                         XText t = el.FirstNode as XText;
478                         Assert.IsNotNull (t, "#1");
479                         Assert.AreEqual ("test", t.Value, "#2");
480                 }
481
482                 [Test]
483                 public void ReplaceAll ()
484                 {
485                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
486                         el.ReplaceAll ("test");
487                         XText t = el.FirstNode as XText;
488                         Assert.IsNotNull (t, "#1");
489                         Assert.AreEqual ("test", t.Value, "#2");
490                         Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#3");
491                 }
492
493                 [Test]
494                 public void ReplaceAllList ()
495                 {
496                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
497                         el.ReplaceAll (
498                                 new XText ("test1"),
499                                 new XText ("test2"),
500                                 new XText ("test3"));
501                         XText t = el.FirstNode as XText;
502                         Assert.IsNotNull (t, "#1");
503                         Assert.AreEqual ("test1", t.Value, "#2");
504                         t = el.LastNode as XText;
505                         Assert.IsNotNull (t, "#3");
506                         Assert.AreEqual ("test3", t.Value, "#4");
507                         Assert.AreEqual (3, new List<XNode> (el.Nodes ()).Count, "#5");
508                 }
509
510                 [Test]
511                 public void ReplaceAttributes ()
512                 {
513                         XElement el = XElement.Parse ("<root x='y'><foo a='b'/></root>");
514                         Assert.IsTrue (el.Attributes ().GetEnumerator ().MoveNext (), "#0");
515                         el.ReplaceAttributes ("test");
516                         Assert.IsTrue (el.FirstNode is XElement, "#1");
517                         Assert.IsTrue (el.LastNode is XText, "#2");
518                         Assert.IsFalse (el.Attributes ().GetEnumerator ().MoveNext (), "#3");
519                 }
520
521                 [Test]
522                 public void GetDefaultNamespace ()
523                 {
524                         XElement el = XElement.Parse ("<root xmlns='urn:foo'><foo><xxx/></foo><x:bar xmlns:x='urn:bar'><yyy/></x:bar><baz xmlns=''><zzz /></baz></root>");
525                         XNamespace ns1 = XNamespace.Get ("urn:foo");
526                         Assert.AreEqual (ns1, el.GetDefaultNamespace (), "#1");
527                         XElement foo = (XElement) el.FirstNode;
528                         Assert.AreEqual (ns1, foo.GetDefaultNamespace (), "#2");
529                         Assert.AreEqual (ns1, ((XElement) foo.FirstNode).GetDefaultNamespace (), "#3");
530                         XElement bar = (XElement) foo.NextNode;
531                         Assert.AreEqual (ns1, bar.GetDefaultNamespace (), "#4");
532                         Assert.AreEqual (ns1, ((XElement) bar.FirstNode).GetDefaultNamespace (), "#5");
533                         XElement baz = (XElement) bar.NextNode;
534                         Assert.AreEqual (XNamespace.Get (String.Empty), baz.GetDefaultNamespace (), "#6");
535                         Assert.AreEqual (XNamespace.Get (String.Empty), ((XElement) baz.FirstNode).GetDefaultNamespace (), "#7");
536                 }
537
538                 [Test]
539                 public void GetPrefixNamespace ()
540                 {
541                         XElement el = XElement.Parse ("<x:root xmlns:x='urn:foo'><foo><xxx/></foo><x:bar xmlns:x='urn:bar'><yyy/></x:bar><baz xmlns=''><zzz /></baz></x:root>");
542                         XNamespace ns1 = XNamespace.Get ("urn:foo");
543                         XNamespace ns2 = XNamespace.Get ("urn:bar");
544                         Assert.AreEqual (ns1, el.GetNamespaceOfPrefix ("x"), "#1-1");
545                         Assert.AreEqual ("x", el.GetPrefixOfNamespace (ns1), "#1-2");
546                         XElement foo = (XElement) el.FirstNode;
547                         Assert.AreEqual (ns1, foo.GetNamespaceOfPrefix ("x"), "#2-1");
548                         Assert.AreEqual ("x", foo.GetPrefixOfNamespace (ns1), "#2-2");
549                         Assert.AreEqual (ns1, ((XElement) foo.FirstNode).GetNamespaceOfPrefix ("x"), "#3-1");
550                         Assert.AreEqual ("x", ((XElement) foo.FirstNode).GetPrefixOfNamespace (ns1), "#3-2");
551                         XElement bar = (XElement) foo.NextNode;
552                         Assert.AreEqual (ns2, bar.GetNamespaceOfPrefix ("x"), "#4-1");
553                         Assert.AreEqual ("x", bar.GetPrefixOfNamespace (ns2), "#4-2");
554                         Assert.AreEqual (null, bar.GetPrefixOfNamespace (ns1), "#4-3");
555                         Assert.AreEqual (ns2, ((XElement) bar.FirstNode).GetNamespaceOfPrefix ("x"), "#5-1");
556                         Assert.AreEqual ("x", ((XElement) bar.FirstNode).GetPrefixOfNamespace (ns2), "#5-2");
557                         Assert.AreEqual (null, ((XElement) bar.FirstNode).GetPrefixOfNamespace (ns1), "#5-3");
558                 }
559
560 #pragma warning disable 219
561                 [Test]
562                 public void CastNulls ()
563                 {
564                         const XElement a = null;
565
566                         Assert.AreEqual (null, (bool?) a, "bool?");
567                         Assert.AreEqual (null, (DateTime?) a, "DateTime?");
568                         Assert.AreEqual (null, (DateTimeOffset?) a, "DateTimeOffset?");
569                         Assert.AreEqual (null, (decimal?) a, "decimal?");
570                         Assert.AreEqual (null, (double?) a, "double?");
571                         Assert.AreEqual (null, (float?) a, "float?");
572                         Assert.AreEqual (null, (Guid?) a, "Guid?");
573                         Assert.AreEqual (null, (int?) a, "int?");
574                         Assert.AreEqual (null, (long?) a, "long?");
575                         Assert.AreEqual (null, (uint?) a, "uint?");
576                         Assert.AreEqual (null, (ulong?) a, "ulong?");
577                         Assert.AreEqual (null, (TimeSpan?) a, "TimeSpan?");
578                         Assert.AreEqual (null, (string) a, "string");
579                         AssertThrows<ArgumentNullException> (() => { bool z = (bool) a; }, "bool");
580                         AssertThrows<ArgumentNullException> (() => { DateTime z = (DateTime) a; }, "DateTime");
581                         AssertThrows<ArgumentNullException> (() => { DateTimeOffset z = (DateTimeOffset) a; }, "DateTimeOffset");
582                         AssertThrows<ArgumentNullException> (() => { decimal z = (decimal) a; }, "decimal");
583                         AssertThrows<ArgumentNullException> (() => { double z = (double) a; }, "double");
584                         AssertThrows<ArgumentNullException> (() => { float z = (float) a; }, "float");
585                         AssertThrows<ArgumentNullException> (() => { Guid z = (Guid) a; }, "Guid");
586                         AssertThrows<ArgumentNullException> (() => { int z = (int) a; }, "int");
587                         AssertThrows<ArgumentNullException> (() => { long z = (long) a; }, "long");
588                         AssertThrows<ArgumentNullException> (() => { uint z = (uint) a; }, "uint");
589                         AssertThrows<ArgumentNullException> (() => { ulong z = (ulong) a; }, "ulong");
590                         AssertThrows<ArgumentNullException> (() => { TimeSpan z = (TimeSpan) a; }, "TimeSpan");
591                 }
592
593                 /// <remarks>
594                 /// Provides functionality similar to Assert.Throws that is available on newer versions of NUnit.
595                 /// </remarks>
596                 private static T AssertThrows<T> (Action code, string message, params object[] args) where T : Exception
597                 {
598                         Exception actual = null;
599                         try {
600                                 code ();
601                         } catch (Exception exception) {
602                                 actual = exception;
603                         }
604                         Assert.That (actual, new NUnit.Framework.Constraints.ExactTypeConstraint (typeof (T)), message, args);
605                         return (T) actual;
606                 }
607
608                 [Test]
609                 public void CastEmpties ()
610                 {
611                         XElement a = new XElement ("a");
612
613                         // Verify expected "cloning" and "empty" behaviour as prerequisites
614                         Assert.IsTrue (a.IsEmpty, "#1-1");
615                         Assert.IsTrue (new XElement (a).IsEmpty, "#1-2");
616                         Assert.AreEqual (String.Empty, a.Value, "#2-1");
617                         Assert.AreEqual (String.Empty, new XElement (a).Value, "#2-2");
618                         Assert.AreNotSame (a, new XElement (a), "#3-1");
619                         Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2");
620                         Assert.AreEqual ("<a />", a.ToString (), "#3-3");
621                         Assert.AreEqual (a.ToString (), new XElement ("a", null).ToString (), "#3-4");
622
623                         // Execute the primary assertions of this test
624                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "bool?");
625                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "DateTime?");
626                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "DateTimeOffset?");
627                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "decimal?");
628                         AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "double?");
629                         AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "float?");
630                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "Guid?");
631                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "int?");
632                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "long?");
633                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "uint?");
634                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "ulong?");
635                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "TimeSpan?");
636                         Assert.AreEqual (String.Empty, (string) new XElement (a), "string");
637                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "bool");
638                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "DateTime");
639                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "DateTimeOffset");
640                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "decimal");
641                         AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "double");
642                         AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "float");
643                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "Guid");
644                         AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "int");
645                         AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "long");
646                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "uint");
647                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "ulong");
648                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "TimeSpan");
649                 }
650
651                 [Test]
652                 public void CastBlanks ()
653                 {
654                         XElement a = new XElement ("a", String.Empty);
655                         XElement b = new XElement ("b", new XCData (string.Empty));
656
657                         // Verify expected "cloning" and "blank" behaviour as prerequisites
658                         Assert.IsFalse (a.IsEmpty, "#1-1a");
659                         Assert.IsFalse (b.IsEmpty, "#1-1b");
660                         Assert.IsFalse (new XElement (a).IsEmpty, "#1-2a");
661                         Assert.IsFalse (new XElement (b).IsEmpty, "#1-2b");
662                         Assert.AreEqual (String.Empty, a.Value, "#2-1a");
663                         Assert.AreEqual (String.Empty, b.Value, "#2-1b");
664                         Assert.AreEqual (String.Empty, new XElement (a).Value, "#2-2a");
665                         Assert.AreEqual (String.Empty, new XElement (b).Value, "#2-2b");
666                         Assert.AreNotSame (a, new XElement (a), "#3-1a");
667                         Assert.AreNotSame (b, new XElement (b), "#3-1b");
668                         Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2a");
669                         Assert.AreEqual (b.ToString (), new XElement (b).ToString (), "#3-2b");
670                         Assert.AreEqual ("<a></a>", a.ToString (), "#3-3a");
671                         Assert.AreEqual ("<b><![CDATA[]]></b>", b.ToString (), "#3-3b");
672                         Assert.AreEqual (a.ToString (), new XElement ("a", "").ToString (), "#3-4a");
673                         Assert.AreEqual (b.ToString (), new XElement ("b", new XCData ("")).ToString (), "#3-4b");
674
675                         // Execute the primary assertions of this test
676                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
677                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
678                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
679                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
680                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
681                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
682                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "a:decimal?");
683                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (b); }, "b:decimal?");
684                         AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "a:double?");
685                         AssertThrows<FormatException> (() => { double? z = (double?) new XElement (b); }, "b:double?");
686                         AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "a:float?");
687                         AssertThrows<FormatException> (() => { float? z = (float?) new XElement (b); }, "b:float?");
688                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
689                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
690                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "a:int?");
691                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (b); }, "b:int?");
692                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "a:long?");
693                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (b); }, "b:long?");
694                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "a:uint?");
695                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (b); }, "b:uint?");
696                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "a:ulong?");
697                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (b); }, "b:ulong?");
698                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
699                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
700                         Assert.AreEqual (String.Empty, (string) new XElement (a), "a:string");
701                         Assert.AreEqual (String.Empty, (string) new XElement (b), "b:string");
702                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
703                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
704                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
705                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
706                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
707                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
708                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "a:decimal");
709                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (b); }, "b:decimal");
710                         AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "a:double");
711                         AssertThrows<FormatException> (() => { double z = (double) new XElement (b); }, "b:double");
712                         AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "a:float");
713                         AssertThrows<FormatException> (() => { float z = (float) new XElement (b); }, "b:float");
714                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
715                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
716                         AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "a:int");
717                         AssertThrows<FormatException> (() => { int z = (int) new XElement (b); }, "b:int");
718                         AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "a:long");
719                         AssertThrows<FormatException> (() => { long z = (long) new XElement (b); }, "b:long");
720                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "a:uint");
721                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (b); }, "b:uint");
722                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "a:ulong");
723                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (b); }, "b:ulong");
724                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
725                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
726                 }
727
728                 [Test]
729                 public void CastSpaces ()
730                 {
731                         XElement a = new XElement ("a", " ");
732                         XElement b = new XElement ("b", new XCData (" "));
733
734                         // Verify expected "cloning" and "space" behaviour as prerequisites
735                         Assert.IsFalse (a.IsEmpty, "#1-1a");
736                         Assert.IsFalse (b.IsEmpty, "#1-1b");
737                         Assert.IsFalse (new XElement (a).IsEmpty, "#1-2a");
738                         Assert.IsFalse (new XElement (b).IsEmpty, "#1-2b");
739                         Assert.AreEqual (" ", a.Value, "#2-1a");
740                         Assert.AreEqual (" ", b.Value, "#2-1b");
741                         Assert.AreEqual (" ", new XElement (a).Value, "#2-2a");
742                         Assert.AreEqual (" ", new XElement (b).Value, "#2-2b");
743                         Assert.AreNotSame (a, new XElement (a), "#3-1a");
744                         Assert.AreNotSame (b, new XElement (b), "#3-1b");
745                         Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2a");
746                         Assert.AreEqual (b.ToString (), new XElement (b).ToString (), "#3-2b");
747                         Assert.AreEqual ("<a> </a>", a.ToString (), "#3-3a");
748                         Assert.AreEqual ("<b><![CDATA[ ]]></b>", b.ToString (), "#3-3b");
749                         Assert.AreEqual (a.ToString (), new XElement ("a", ' ').ToString (), "#3-4");
750
751                         // Execute the primary assertions of this test
752                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
753                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
754                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
755                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
756                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
757                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
758                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "a:decimal?");
759                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (b); }, "b:decimal?");
760                         AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "a:double?");
761                         AssertThrows<FormatException> (() => { double? z = (double?) new XElement (b); }, "b:double?");
762                         AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "a:float?");
763                         AssertThrows<FormatException> (() => { float? z = (float?) new XElement (b); }, "b:float?");
764                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
765                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
766                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "a:int?");
767                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (b); }, "b:int?");
768                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "a:long?");
769                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (b); }, "b:long?");
770                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "a:uint?");
771                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (b); }, "b:uint?");
772                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "a:ulong?");
773                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (b); }, "b:ulong?");
774                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
775                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
776                         Assert.AreEqual (" ", (string) new XElement (a), "a:string");
777                         Assert.AreEqual (" ", (string) new XElement (b), "b:string");
778                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
779                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
780                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
781                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
782                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
783                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
784                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "a:decimal");
785                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (b); }, "b:decimal");
786                         AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "a:double");
787                         AssertThrows<FormatException> (() => { double z = (double) new XElement (b); }, "b:double");
788                         AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "a:float");
789                         AssertThrows<FormatException> (() => { float z = (float) new XElement (b); }, "b:float");
790                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
791                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
792                         AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "a:int");
793                         AssertThrows<FormatException> (() => { int z = (int) new XElement (b); }, "b:int");
794                         AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "a:long");
795                         AssertThrows<FormatException> (() => { long z = (long) new XElement (b); }, "b:long");
796                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "a:uint");
797                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (b); }, "b:uint");
798                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "a:ulong");
799                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (b); }, "b:ulong");
800                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
801                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
802                 }
803
804                 [Test]
805                 public void CastNumbers ()
806                 {
807                         XElement a = new XElement ("a", "7");
808                         XElement b = new XElement ("b", new XCData ("  42 "));
809                         XElement c = new XElement ("c", " \r\n   13 \t  ".Replace ("\r\n", Environment.NewLine));
810                         XElement d = new XElement ("d", -101);
811                         XElement o = new XElement ("o", "0");
812                         XElement l = new XElement ("l", "1");
813                         XElement I = new XElement ("I", "INF");
814                         XElement i = new XElement ("i", " Infinity  ");
815                         XElement M = new XElement ("M", "   -INF ");
816                         XElement m = new XElement ("m", "-Infinity");
817                         XElement n = new XElement ("n", "\t NaN   ");
818
819                         // Verify expected "cloning" and basic conversion behaviour as prerequisites
820                         Assert.IsFalse (a.IsEmpty, "#1-1");
821                         Assert.IsFalse (new XElement (b).IsEmpty, "#1-2");
822                         Assert.AreEqual (" \r\n   13 \t  ".Replace ("\r\n", Environment.NewLine), c.Value, "#2-1");
823                         Assert.AreEqual ("-101", new XElement (d).Value, "#2-2");
824                         Assert.AreNotSame (o, new XElement (o), "#3-1");
825                         Assert.AreEqual (l.ToString (), new XElement (l).ToString (), "#3-2");
826                         Assert.AreEqual ("<a>7</a>", a.ToString (), "#3-3a");
827                         Assert.AreEqual ("<b><![CDATA[  42 ]]></b>", b.ToString (), "#3-3b");
828                         Assert.AreEqual ("<c> \r\n   13 \t  </c>".Replace ("\r\n", Environment.NewLine), c.ToString (), "#3-3c");
829                         Assert.AreEqual ("<d>-101</d>", d.ToString (), "#3-3d");
830                         Assert.AreEqual ("<o>0</o>", new XElement ("o", 0.0).ToString (), "#3-3o");
831                         Assert.AreEqual ("<l>1</l>", new XElement ("l", 1.0f).ToString (), "#3-3l");
832                         Assert.AreEqual ("<n>NaN</n>", new XElement ("n", double.NaN).ToString (), "#3-3n");
833                         Assert.AreEqual (a.ToString (), new XElement ("a", '7').ToString (), "#3-4a");
834                         Assert.AreEqual (d.ToString (), new XElement ("d", "-101").ToString (), "#3-4d");
835                         Assert.AreEqual (o.ToString (), new XElement ("o", 0L).ToString (), "#3-4o");
836                         Assert.AreEqual (l.ToString (), new XElement ("l", 1m).ToString (), "#3-4l");
837
838                         // Execute the primary assertions of this test
839                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
840                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
841                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (c); }, "c:bool?");
842                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (d); }, "d:bool?");
843                         Assert.IsNotNull ((bool?) new XElement (o), "o:bool?:null");
844                         Assert.AreEqual (false, ((bool?) new XElement (o)).Value, "o:bool?:value");
845                         Assert.IsNotNull ((bool?) new XElement (l), "l:bool?:null");
846                         Assert.AreEqual (true, ((bool?) new XElement (l)).Value, "l:bool?:value");
847                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (I); }, "I:bool?");
848                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (i); }, "i:bool?");
849                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (M); }, "M:bool?");
850                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (m); }, "m:bool?");
851                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (n); }, "n:bool?");
852                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
853                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
854                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (c); }, "c:DateTime?");
855                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (d); }, "d:DateTime?");
856                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (o); }, "o:DateTime?");
857                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (l); }, "l:DateTime?");
858                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (I); }, "I:DateTime?");
859                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (i); }, "i:DateTime?");
860                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (M); }, "M:DateTime?");
861                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (m); }, "m:DateTime?");
862                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (n); }, "n:DateTime?");
863                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
864                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
865                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (c); }, "c:DateTimeOffset?");
866                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (d); }, "d:DateTimeOffset?");
867                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (o); }, "o:DateTimeOffset?");
868                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (l); }, "l:DateTimeOffset?");
869                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (I); }, "I:DateTimeOffset?");
870                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (i); }, "i:DateTimeOffset?");
871                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (M); }, "M:DateTimeOffset?");
872                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (m); }, "m:DateTimeOffset?");
873                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (n); }, "n:DateTimeOffset?");
874                         Assert.IsNotNull ((decimal?) new XElement (a), "a:decimal?:null");
875                         Assert.AreEqual (7m, ((decimal?) new XElement (a)).Value, "a:decimal?:value");
876                         Assert.IsNotNull ((decimal?) new XElement (b), "b:decimal?:null");
877                         Assert.AreEqual (42m, ((decimal?) new XElement (b)).Value, "b:decimal?:value");
878                         Assert.IsNotNull ((decimal?) new XElement (c), "c:decimal?:null");
879                         Assert.AreEqual (13m, ((decimal?) new XElement (c)).Value, "c:decimal?:value");
880                         Assert.IsNotNull ((decimal?) new XElement (d), "d:decimal?:null");
881                         Assert.AreEqual (-101m, ((decimal?) new XElement (d)).Value, "d:decimal?:value");
882                         Assert.IsNotNull ((decimal?) new XElement (o), "o:decimal?:null");
883                         Assert.AreEqual (0m, ((decimal?) new XElement (o)).Value, "o:decimal?:value");
884                         Assert.IsNotNull ((decimal?) new XElement (l), "l:decimal?:null");
885                         Assert.AreEqual (1m, ((decimal?) new XElement (l)).Value, "l:decimal?:value");
886                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (I); }, "I:decimal?");
887                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (i); }, "i:decimal?");
888                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (M); }, "M:decimal?");
889                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (m); }, "m:decimal?");
890                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (n); }, "n:decimal?");
891                         Assert.IsNotNull ((double?) new XElement (a), "a:double?:null");
892                         Assert.AreEqual (7d, ((double?) new XElement (a)).Value, "a:double?:value");
893                         Assert.IsNotNull ((double?) new XElement (b), "b:double?:null");
894                         Assert.AreEqual (42d, ((double?) new XElement (b)).Value, "b:double?:value");
895                         Assert.IsNotNull ((double?) new XElement (c), "c:double?:null");
896                         Assert.AreEqual (13d, ((double?) new XElement (c)).Value, "c:double?:value");
897                         Assert.IsNotNull ((double?) new XElement (d), "d:double?:null");
898                         Assert.AreEqual (-101d, ((double?) new XElement (d)).Value, "d:double?:value");
899                         Assert.IsNotNull ((double?) new XElement (o), "o:double?:null");
900                         Assert.AreEqual (0d, ((double?) new XElement (o)).Value, "o:double?:value");
901                         Assert.IsNotNull ((double?) new XElement (l), "l:double?:null");
902                         Assert.AreEqual (1d, ((double?) new XElement (l)).Value, "l:double?:value");
903                         Assert.IsNotNull ((double?) new XElement (I), "I:double?:null");
904                         Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (I)).Value, "I:double?:value");
905                         Assert.IsNotNull ((double?) new XElement (i), "i:double?:null");
906                         Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (i)).Value, "i:double?:value");
907                         Assert.IsNotNull ((double?) new XElement (M), "M:double?:null");
908                         Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (M)).Value, "M:double?:value");
909                         Assert.IsNotNull ((double?) new XElement (m), "m:double?:null");
910                         Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (m)).Value, "m:double?:value");
911                         Assert.IsNotNull ((double?) new XElement (n), "n:double?:null");
912                         Assert.AreEqual (double.NaN, ((double?) new XElement (n)).Value, "n:double?:value");
913                         Assert.IsNotNull ((float?) new XElement (a), "a:float?:null");
914                         Assert.AreEqual (7f, ((float?) new XElement (a)).Value, "a:float?:value");
915                         Assert.IsNotNull ((float?) new XElement (b), "b:float?:null");
916                         Assert.AreEqual (42f, ((float?) new XElement (b)).Value, "b:float?:value");
917                         Assert.IsNotNull ((float?) new XElement (c), "c:float?:null");
918                         Assert.AreEqual (13f, ((float?) new XElement (c)).Value, "c:float?:value");
919                         Assert.IsNotNull ((float?) new XElement (d), "d:float?:null");
920                         Assert.AreEqual (-101f, ((float?) new XElement (d)).Value, "d:float?:value");
921                         Assert.IsNotNull ((float?) new XElement (o), "o:float?:null");
922                         Assert.AreEqual (0f, ((float?) new XElement (o)).Value, "o:float?:value");
923                         Assert.IsNotNull ((float?) new XElement (l), "l:float?:null");
924                         Assert.AreEqual (1f, ((float?) new XElement (l)).Value, "l:float?:value");
925                         Assert.IsNotNull ((float?) new XElement (I), "I:float?:null");
926                         Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (I)).Value, "I:float?:value");
927                         Assert.IsNotNull ((float?) new XElement (i), "i:float?:null");
928                         Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (i)).Value, "i:float?:value");
929                         Assert.IsNotNull ((float?) new XElement (M), "M:float?:null");
930                         Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (M)).Value, "M:float?:value");
931                         Assert.IsNotNull ((float?) new XElement (m), "m:float?:null");
932                         Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (m)).Value, "m:float?:value");
933                         Assert.IsNotNull ((float?) new XElement (n), "n:float?:null");
934                         Assert.AreEqual (float.NaN, ((float?) new XElement (n)).Value, "n:float?:value");
935                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
936                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
937                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (c); }, "c:Guid?");
938                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (d); }, "d:Guid?");
939                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (o); }, "o:Guid?");
940                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (l); }, "l:Guid?");
941                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (I); }, "I:Guid?");
942                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (i); }, "i:Guid?");
943                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (M); }, "M:Guid?");
944                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (m); }, "m:Guid?");
945                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (n); }, "n:Guid?");
946                         Assert.IsNotNull ((int?) new XElement (a), "a:int?:null");
947                         Assert.AreEqual (7, ((int?) new XElement (a)).Value, "a:int?:value");
948                         Assert.IsNotNull ((int?) new XElement (b), "b:int?:null");
949                         Assert.AreEqual (42, ((int?) new XElement (b)).Value, "b:int?:value");
950                         Assert.IsNotNull ((int?) new XElement (c), "c:int?:null");
951                         Assert.AreEqual (13, ((int?) new XElement (c)).Value, "c:int?:value");
952                         Assert.IsNotNull ((int?) new XElement (d), "d:int?:null");
953                         Assert.AreEqual (-101, ((int?) new XElement (d)).Value, "d:int?:value");
954                         Assert.IsNotNull ((int?) new XElement (o), "o:int?:null");
955                         Assert.AreEqual (0, ((int?) new XElement (o)).Value, "o:int?:value");
956                         Assert.IsNotNull ((int?) new XElement (l), "l:int?:null");
957                         Assert.AreEqual (1, ((int?) new XElement (l)).Value, "l:int?:value");
958                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (I); }, "I:int?");
959                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (i); }, "i:int?");
960                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (M); }, "M:int?");
961                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (m); }, "m:int?");
962                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (n); }, "n:int?");
963                         Assert.IsNotNull ((long?) new XElement (a), "a:long?:null");
964                         Assert.AreEqual (7L, ((long?) new XElement (a)).Value, "a:long?:value");
965                         Assert.IsNotNull ((long?) new XElement (b), "b:long?:null");
966                         Assert.AreEqual (42L, ((long?) new XElement (b)).Value, "b:long?:value");
967                         Assert.IsNotNull ((long?) new XElement (c), "c:long?:null");
968                         Assert.AreEqual (13L, ((long?) new XElement (c)).Value, "c:long?:value");
969                         Assert.IsNotNull ((long?) new XElement (d), "d:long?:null");
970                         Assert.AreEqual (-101L, ((long?) new XElement (d)).Value, "d:long?:value");
971                         Assert.IsNotNull ((long?) new XElement (o), "o:long?:null");
972                         Assert.AreEqual (0L, ((long?) new XElement (o)).Value, "o:long?:value");
973                         Assert.IsNotNull ((long?) new XElement (l), "l:long?:null");
974                         Assert.AreEqual (1L, ((long?) new XElement (l)).Value, "l:long?:value");
975                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (I); }, "I:long?");
976                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (i); }, "i:long?");
977                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (M); }, "M:long?");
978                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (m); }, "m:long?");
979                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (n); }, "n:long?");
980                         Assert.IsNotNull ((uint?) new XElement (a), "a:uint?:null");
981                         Assert.AreEqual (7u, ((uint?) new XElement (a)).Value, "a:uint?:value");
982                         Assert.IsNotNull ((uint?) new XElement (b), "b:uint?:null");
983                         Assert.AreEqual (42u, ((uint?) new XElement (b)).Value, "b:uint?:value");
984                         Assert.IsNotNull ((uint?) new XElement (c), "c:uint?:null");
985                         Assert.AreEqual (13u, ((uint?) new XElement (c)).Value, "c:uint?:value");
986                         // LAMESPEC: see XmlConvertTests.ToUInt32().
987                         //AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (d); }, "d:uint?");
988                         Assert.IsNotNull ((uint?) new XElement (o), "o:uint?:null");
989                         Assert.AreEqual (0u, ((uint?) new XElement (o)).Value, "o:uint?:value");
990                         Assert.IsNotNull ((uint?) new XElement (l), "l:uint?:null");
991                         Assert.AreEqual (1u, ((uint?) new XElement (l)).Value, "l:uint?:value");
992                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (I); }, "I:uint?");
993                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (i); }, "i:uint?");
994                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (M); }, "M:uint?");
995                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (m); }, "m:uint?");
996                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (n); }, "n:uint?");
997                         Assert.IsNotNull ((ulong?) new XElement (a), "a:ulong?:null");
998                         Assert.AreEqual (7UL, ((ulong?) new XElement (a)).Value, "a:ulong?:value");
999                         Assert.IsNotNull ((ulong?) new XElement (b), "b:ulong?:null");
1000                         Assert.AreEqual (42UL, ((ulong?) new XElement (b)).Value, "b:ulong?:value");
1001                         Assert.IsNotNull ((ulong?) new XElement (c), "c:ulong?:null");
1002                         Assert.AreEqual (13UL, ((ulong?) new XElement (c)).Value, "c:ulong?:value");
1003                         // LAMESPEC: see XmlConvertTests.ToUInt64().
1004                         //AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (d); }, "d:ulong?");
1005                         Assert.IsNotNull ((ulong?) new XElement (o), "o:ulong?:null");
1006                         Assert.AreEqual (0UL, ((ulong?) new XElement (o)).Value, "o:ulong?:value");
1007                         Assert.IsNotNull ((ulong?) new XElement (l), "l:ulong?:null");
1008                         Assert.AreEqual (1UL, ((ulong?) new XElement (l)).Value, "l:ulong?:value");
1009                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (I); }, "I:ulong?");
1010                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (i); }, "i:ulong?");
1011                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (M); }, "M:ulong?");
1012                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (m); }, "m:ulong?");
1013                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (n); }, "n:ulong?");
1014                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
1015                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
1016                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (c); }, "c:TimeSpan?");
1017                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (d); }, "d:TimeSpan?");
1018                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (o); }, "o:TimeSpan?");
1019                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (l); }, "l:TimeSpan?");
1020                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (I); }, "I:TimeSpan?");
1021                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (i); }, "i:TimeSpan?");
1022                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (M); }, "M:TimeSpan?");
1023                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (m); }, "m:TimeSpan?");
1024                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (n); }, "n:TimeSpan?");
1025                         Assert.AreEqual ("7", (string) new XElement (a), "a:string");
1026                         Assert.AreEqual ("  42 ", (string) new XElement (b), "b:string");
1027                         Assert.AreEqual (" \r\n   13 \t  ".Replace ("\r\n", Environment.NewLine), (string) new XElement (c), "c:string");
1028                         Assert.AreEqual ("-101", (string) new XElement (d), "d:string");
1029                         Assert.AreEqual ("0", (string) new XElement (o), "o:string");
1030                         Assert.AreEqual ("1", (string) new XElement (l), "l:string");
1031                         Assert.AreEqual ("INF", (string) new XElement (I), "I:string");
1032                         Assert.AreEqual (" Infinity  ", (string) new XElement (i), "i:string");
1033                         Assert.AreEqual ("   -INF ", (string) new XElement (M), "M:string");
1034                         Assert.AreEqual ("-Infinity", (string) new XElement (m), "m:string");
1035                         Assert.AreEqual ("\t NaN   ", (string) new XElement (n), "n:string");
1036                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
1037                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
1038                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (c); }, "c:bool");
1039                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (d); }, "d:bool");
1040                         Assert.AreEqual (false, (bool) new XElement (o), "o:bool");
1041                         Assert.AreEqual (true, (bool) new XElement (l), "l:bool");
1042                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (I); }, "I:bool");
1043                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (i); }, "i:bool");
1044                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (M); }, "M:bool");
1045                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (m); }, "m:bool");
1046                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (n); }, "n:bool");
1047                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
1048                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
1049                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (c); }, "c:DateTime");
1050                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (d); }, "d:DateTime");
1051                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (o); }, "o:DateTime");
1052                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (l); }, "l:DateTime");
1053                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (I); }, "I:DateTime");
1054                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (i); }, "i:DateTime");
1055                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (M); }, "M:DateTime");
1056                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (m); }, "m:DateTime");
1057                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (n); }, "n:DateTime");
1058                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
1059                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
1060                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (c); }, "c:DateTimeOffset");
1061                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (d); }, "d:DateTimeOffset");
1062                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (o); }, "o:DateTimeOffset");
1063                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (l); }, "l:DateTimeOffset");
1064                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (I); }, "I:DateTimeOffset");
1065                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (i); }, "i:DateTimeOffset");
1066                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (M); }, "M:DateTimeOffset");
1067                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (m); }, "m:DateTimeOffset");
1068                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (n); }, "n:DateTimeOffset");
1069                         Assert.AreEqual (7m, (decimal) new XElement (a), "a:decimal");
1070                         Assert.AreEqual (42m, (decimal) new XElement (b), "b:decimal");
1071                         Assert.AreEqual (13m, (decimal) new XElement (c), "c:decimal");
1072                         Assert.AreEqual (-101m, (decimal) new XElement (d), "d:decimal");
1073                         Assert.AreEqual (0m, (decimal) new XElement (o), "o:decimal");
1074                         Assert.AreEqual (1m, (decimal) new XElement (l), "l:decimal");
1075                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (I); }, "I:decimal");
1076                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (i); }, "i:decimal");
1077                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (M); }, "M:decimal");
1078                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (m); }, "m:decimal");
1079                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (n); }, "n:decimal");
1080                         Assert.AreEqual (7d, (double) new XElement (a), "a:double");
1081                         Assert.AreEqual (42d, (double) new XElement (b), "b:double");
1082                         Assert.AreEqual (13d, (double) new XElement (c), "c:double");
1083                         Assert.AreEqual (-101d, (double) new XElement (d), "d:double");
1084                         Assert.AreEqual (0d, (double) new XElement (o), "o:double");
1085                         Assert.AreEqual (1d, (double) new XElement (l), "l:double");
1086                         Assert.AreEqual (double.PositiveInfinity, (double) new XElement (I), "I:double");
1087                         Assert.AreEqual (double.PositiveInfinity, (double) new XElement (i), "i:double");
1088                         Assert.AreEqual (double.NegativeInfinity, (double) new XElement (M), "M:double");
1089                         Assert.AreEqual (double.NegativeInfinity, (double) new XElement (m), "m:double");
1090                         Assert.AreEqual (double.NaN, ((double) new XElement (n)), "n:double");
1091                         Assert.AreEqual (7f, (float) new XElement (a), "a:float");
1092                         Assert.AreEqual (42f, (float) new XElement (b), "b:float");
1093                         Assert.AreEqual (13f, (float) new XElement (c), "c:float");
1094                         Assert.AreEqual (-101f, (float) new XElement (d), "d:float");
1095                         Assert.AreEqual (0f, (float) new XElement (o), "o:float");
1096                         Assert.AreEqual (1f, (float) new XElement (l), "l:float");
1097                         Assert.AreEqual (float.PositiveInfinity, (float) new XElement (I), "I:float");
1098                         Assert.AreEqual (float.PositiveInfinity, (float) new XElement (i), "i:float");
1099                         Assert.AreEqual (float.NegativeInfinity, (float) new XElement (M), "M:float");
1100                         Assert.AreEqual (float.NegativeInfinity, (float) new XElement (m), "m:float");
1101                         Assert.AreEqual (float.NaN, ((float) new XElement (n)), "n:float");
1102                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
1103                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
1104                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (c); }, "c:Guid");
1105                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (d); }, "d:Guid");
1106                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (o); }, "o:Guid");
1107                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (l); }, "l:Guid");
1108                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (I); }, "I:Guid");
1109                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (i); }, "i:Guid");
1110                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (M); }, "M:Guid");
1111                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (m); }, "m:Guid");
1112                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (n); }, "n:Guid");
1113                         Assert.AreEqual (7, (int) new XElement (a), "a:int");
1114                         Assert.AreEqual (42, (int) new XElement (b), "b:int");
1115                         Assert.AreEqual (13, (int) new XElement (c), "c:int");
1116                         Assert.AreEqual (-101, (int) new XElement (d), "d:int");
1117                         Assert.AreEqual (0, (int) new XElement (o), "o:int");
1118                         Assert.AreEqual (1, (int) new XElement (l), "l:int");
1119                         AssertThrows<FormatException> (() => { int z = (int) new XElement (I); }, "I:int");
1120                         AssertThrows<FormatException> (() => { int z = (int) new XElement (i); }, "i:int");
1121                         AssertThrows<FormatException> (() => { int z = (int) new XElement (M); }, "M:int");
1122                         AssertThrows<FormatException> (() => { int z = (int) new XElement (m); }, "m:int");
1123                         AssertThrows<FormatException> (() => { int z = (int) new XElement (n); }, "n:int");
1124                         Assert.AreEqual (7L, (long) new XElement (a), "a:long");
1125                         Assert.AreEqual (42L, (long) new XElement (b), "b:long");
1126                         Assert.AreEqual (13L, (long) new XElement (c), "c:long");
1127                         Assert.AreEqual (-101L, (long) new XElement (d), "d:long");
1128                         Assert.AreEqual (0L, (long) new XElement (o), "o:long");
1129                         Assert.AreEqual (1L, (long) new XElement (l), "l:long");
1130                         AssertThrows<FormatException> (() => { long z = (long) new XElement (I); }, "I:long");
1131                         AssertThrows<FormatException> (() => { long z = (long) new XElement (i); }, "i:long");
1132                         AssertThrows<FormatException> (() => { long z = (long) new XElement (M); }, "M:long");
1133                         AssertThrows<FormatException> (() => { long z = (long) new XElement (m); }, "m:long");
1134                         AssertThrows<FormatException> (() => { long z = (long) new XElement (n); }, "n:long");
1135                         Assert.AreEqual (7u, (uint) new XElement (a), "a:uint");
1136                         Assert.AreEqual (42u, (uint) new XElement (b), "b:uint");
1137                         Assert.AreEqual (13u, (uint) new XElement (c), "c:uint");
1138                         // LAMESPEC: see XmlConvertTests.ToUInt32().
1139                         //AssertThrows<FormatException> (() => { uint z = (uint) new XElement (d); }, "d:uint");
1140                         Assert.AreEqual (0u, (uint) new XElement (o), "o:uint");
1141                         Assert.AreEqual (1u, (uint) new XElement (l), "l:uint");
1142                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (I); }, "I:uint");
1143                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (i); }, "i:uint");
1144                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (M); }, "M:uint");
1145                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (m); }, "m:uint");
1146                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (n); }, "n:uint");
1147                         Assert.AreEqual (7UL, (ulong) new XElement (a), "a:ulong");
1148                         Assert.AreEqual (42UL, (ulong) new XElement (b), "b:ulong");
1149                         Assert.AreEqual (13UL, (ulong) new XElement (c), "c:ulong");
1150                         // LAMESPEC: see XmlConvertTests.ToUInt64().
1151                         //AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (d); }, "d:ulong");
1152                         Assert.AreEqual (0UL, (ulong) new XElement (o), "o:ulong");
1153                         Assert.AreEqual (1UL, (ulong) new XElement (l), "l:ulong");
1154                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (I); }, "I:ulong");
1155                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (i); }, "i:ulong");
1156                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (M); }, "M:ulong");
1157                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (m); }, "m:ulong");
1158                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (n); }, "n:ulong");
1159                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
1160                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
1161                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (c); }, "c:TimeSpan");
1162                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (d); }, "d:TimeSpan");
1163                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (o); }, "o:TimeSpan");
1164                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (l); }, "l:TimeSpan");
1165                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (I); }, "I:TimeSpan");
1166                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (i); }, "i:TimeSpan");
1167                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (M); }, "M:TimeSpan");
1168                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (m); }, "m:TimeSpan");
1169                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (n); }, "n:TimeSpan");
1170
1171                         // Perform some round-trip tests with numbers
1172                         XElement x;
1173                         const decimal @decimal = -41051609414188012238960097189m;
1174                         const double @double = 8.5506609919892972E+307d;
1175                         const float @float = -1.70151961E+37f;
1176                         const int @int = -1051251773;
1177                         const long @long = 4596767133891939716L;
1178                         const uint @uint = 4106628142u;
1179                         const ulong @ulong = 10713797297298255927UL;
1180                         x = new XElement ("x", @decimal);
1181                         Assert.IsNotNull ((decimal?) new XElement (x), "x:decimal?:null");
1182                         Assert.AreEqual (@decimal, ((decimal?) new XElement (x)).Value, "x:decimal?:value");
1183                         Assert.AreEqual (@decimal, (decimal) new XElement (x), "x:decimal");
1184                         x = new XElement ("x", @double);
1185                         Assert.IsNotNull ((double?) new XElement (x), "x:double?:null");
1186                         Assert.AreEqual (@double, ((double?) new XElement (x)).Value, "x:double?:value");
1187                         Assert.AreEqual (@double, (double) new XElement (x), "x:double");
1188                         x = new XElement ("x", @float);
1189                         Assert.IsNotNull ((float?) new XElement (x), "x:float?:null");
1190                         Assert.AreEqual (@float, ((float?) new XElement (x)).Value, "x:float?:value");
1191                         Assert.AreEqual (@float, (float) new XElement (x), "x:float");
1192                         x = new XElement ("x", @int);
1193                         Assert.IsNotNull ((int?) new XElement (x), "x:int?:null");
1194                         Assert.AreEqual (@int, ((int?) new XElement (x)).Value, "x:int?:value");
1195                         Assert.AreEqual (@int, (int) new XElement (x), "x:int");
1196                         x = new XElement ("x", @long);
1197                         Assert.IsNotNull ((long?) new XElement (x), "x:long?:null");
1198                         Assert.AreEqual (@long, ((long?) new XElement (x)).Value, "x:long?:value");
1199                         Assert.AreEqual (@long, (long) new XElement (x), "x:long");
1200                         x = new XElement ("x", @uint);
1201                         Assert.IsNotNull ((uint?) new XElement (x), "x:uint?:null");
1202                         Assert.AreEqual (@uint, ((uint?) new XElement (x)).Value, "x:uint?:value");
1203                         Assert.AreEqual (@uint, (uint) new XElement (x), "x:uint");
1204                         x = new XElement ("x", @ulong);
1205                         Assert.IsNotNull ((ulong?) new XElement (x), "x:ulong?:null");
1206                         Assert.AreEqual (@ulong, ((ulong?) new XElement (x)).Value, "x:ulong?:value");
1207                         Assert.AreEqual (@ulong, (ulong) new XElement (x), "x:ulong");
1208                         x = new XElement ("x", double.NaN);
1209                         Assert.IsNotNull ((double?) new XElement (x), "NaN:double?:null");
1210                         Assert.AreEqual (double.NaN, ((double?) new XElement (x)).Value, "NaN:double?:value");
1211                         Assert.AreEqual (double.NaN, (double) new XElement (x), "NaN:double");
1212                         x = new XElement ("x", float.NaN);
1213                         Assert.IsNotNull ((float?) new XElement (x), "NaN:float?:null");
1214                         Assert.AreEqual (float.NaN, ((float?) new XElement (x)).Value, "NaN:float?:value");
1215                         Assert.AreEqual (float.NaN, (float) new XElement (x), "NaN:float");
1216                         x = new XElement ("x", double.PositiveInfinity);
1217                         Assert.IsNotNull ((double?) new XElement (x), "+Inf:double?:null");
1218                         Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (x)).Value, "+Inf:double?:value");
1219                         Assert.AreEqual (double.PositiveInfinity, (double) new XElement (x), "+Inf:double");
1220                         x = new XElement ("x", float.PositiveInfinity);
1221                         Assert.IsNotNull ((float?) new XElement (x), "+Inf:float?:null");
1222                         Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (x)).Value, "+Inf:float?:value");
1223                         Assert.AreEqual (float.PositiveInfinity, (float) new XElement (x), "+Inf:float");
1224                         x = new XElement ("x", double.NegativeInfinity);
1225                         Assert.IsNotNull ((double?) new XElement (x), "-Inf:double?:null");
1226                         Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (x)).Value, "-Inf:double?:value");
1227                         Assert.AreEqual (double.NegativeInfinity, (double) new XElement (x), "-Inf:double");
1228                         x = new XElement ("x", float.NegativeInfinity);
1229                         Assert.IsNotNull ((float?) new XElement (x), "-Inf:float?:null");
1230                         Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (x)).Value, "-Inf:float?:value");
1231                         Assert.AreEqual (float.NegativeInfinity, (float) new XElement (x), "-Inf:float");
1232
1233                         // Perform overflow tests with numbers
1234                         AssertThrows<OverflowException> (() => { decimal z = (decimal) new XElement ("z", "91051609414188012238960097189"); }, "z:decimal");
1235                         AssertThrows<OverflowException> (() => { decimal? z = (decimal?) new XElement ("z", "91051609414188012238960097189"); }, "z:decimal?");
1236                         AssertThrows<OverflowException> (() => { double z = (double) new XElement ("z", "8.5506609919892972E+654"); }, "z:double");
1237                         AssertThrows<OverflowException> (() => { double? z = (double?) new XElement ("z", "8.5506609919892972E+654"); }, "z:double?");
1238                         AssertThrows<OverflowException> (() => { float z = (float) new XElement ("z", @double); }, "z:float");
1239                         AssertThrows<OverflowException> (() => { float? z = (float?) new XElement ("z", @double); }, "z:float?");
1240                         AssertThrows<OverflowException> (() => { int z = (int) new XElement ("z", @long); }, "z:int");
1241                         AssertThrows<OverflowException> (() => { int? z = (int?) new XElement ("z", @long); }, "z:int?");
1242                         AssertThrows<OverflowException> (() => { long z = (long) new XElement ("z", @decimal); }, "z:long");
1243                         AssertThrows<OverflowException> (() => { long? z = (long?) new XElement ("z", @decimal); }, "z:long?");
1244                         AssertThrows<OverflowException> (() => { uint z = (uint) new XElement ("z", @ulong); }, "z:uint");
1245                         AssertThrows<OverflowException> (() => { uint? z = (uint?) new XElement ("z", @ulong); }, "z:uint?");
1246                         AssertThrows<OverflowException> (() => { ulong z = (ulong) new XElement ("z", -@decimal); }, "z:ulong");
1247                         AssertThrows<OverflowException> (() => { ulong? z = (ulong?) new XElement ("z", -@decimal); }, "z:ulong?");
1248                 }
1249
1250                 [Test]
1251                 public void CastExtremes ()
1252                 {
1253                         // Test extremes/constants where round-trips should work in specific ways
1254                         Assert.AreEqual (decimal.MaxValue, (decimal) new XElement ("k", decimal.MaxValue), "MaxValue:decimal");
1255                         Assert.AreEqual (decimal.MinValue, (decimal) new XElement ("k", decimal.MinValue), "MinValue:decimal");
1256                         Assert.AreEqual (decimal.MinusOne, (decimal) new XElement ("k", decimal.MinusOne), "MinusOne:decimal");
1257                         Assert.AreEqual (decimal.One, (decimal) new XElement ("k", decimal.One), "One:decimal");
1258                         Assert.AreEqual (decimal.Zero, (decimal) new XElement ("k", decimal.Zero), "Zero:decimal");
1259                         Assert.AreEqual (double.MaxValue, (double) new XElement ("k", double.MaxValue), "MaxValue:double");
1260                         Assert.AreEqual (double.MinValue, (double) new XElement ("k", double.MinValue), "MinValue:double");
1261                         Assert.AreEqual (double.Epsilon, (double) new XElement ("k", double.Epsilon), "Epsilon:double");
1262                         Assert.AreEqual (double.NaN, (double) new XElement ("k", double.NaN), "NaN:double");
1263                         Assert.AreEqual (double.NegativeInfinity, (double) new XElement ("k", double.NegativeInfinity), "-Inf:double");
1264                         Assert.AreEqual (double.PositiveInfinity, (double) new XElement ("k", double.PositiveInfinity), "+Inf:double");
1265                         Assert.AreEqual (float.MaxValue, (float) new XElement ("k", float.MaxValue), "MaxValue:float");
1266                         Assert.AreEqual (float.MinValue, (float) new XElement ("k", float.MinValue), "MinValue:float");
1267                         Assert.AreEqual (float.Epsilon, (float) new XElement ("k", float.Epsilon), "Epsilon:float");
1268                         Assert.AreEqual (float.NaN, (float) new XElement ("k", float.NaN), "NaN:float");
1269                         Assert.AreEqual (float.NegativeInfinity, (float) new XElement ("k", float.NegativeInfinity), "-Inf:float");
1270                         Assert.AreEqual (float.PositiveInfinity, (float) new XElement ("k", float.PositiveInfinity), "+Inf:float");
1271                         Assert.AreEqual (int.MaxValue, (int) new XElement ("k", int.MaxValue), "MaxValue:int");
1272                         Assert.AreEqual (int.MinValue, (int) new XElement ("k", int.MinValue), "MinValue:int");
1273                         Assert.AreEqual (long.MaxValue, (long) new XElement ("k", long.MaxValue), "MaxValue:long");
1274                         Assert.AreEqual (long.MinValue, (long) new XElement ("k", long.MinValue), "MinValue:long");
1275                         Assert.AreEqual (uint.MaxValue, (uint) new XElement ("k", uint.MaxValue), "MaxValue:uint");
1276                         Assert.AreEqual (uint.MinValue, (uint) new XElement ("k", uint.MinValue), "MinValue:uint");
1277                         Assert.AreEqual (ulong.MaxValue, (ulong) new XElement ("k", ulong.MaxValue), "MaxValue:ulong");
1278                         Assert.AreEqual (ulong.MinValue, (ulong) new XElement ("k", ulong.MinValue), "MinValue:ulong");
1279                         Assert.AreEqual (decimal.MaxValue, (decimal?) new XElement ("k", decimal.MaxValue), "MaxValue:decimal?");
1280                         Assert.AreEqual (decimal.MinValue, (decimal?) new XElement ("k", decimal.MinValue), "MinValue:decimal?");
1281                         Assert.AreEqual (decimal.MinusOne, (decimal?) new XElement ("k", decimal.MinusOne), "MinusOne:decimal?");
1282                         Assert.AreEqual (decimal.One, (decimal?) new XElement ("k", decimal.One), "One:decimal?");
1283                         Assert.AreEqual (decimal.Zero, (decimal?) new XElement ("k", decimal.Zero), "Zero:decimal?");
1284                         Assert.AreEqual (double.MaxValue, (double?) new XElement ("k", double.MaxValue), "MaxValue:double?");
1285                         Assert.AreEqual (double.MinValue, (double?) new XElement ("k", double.MinValue), "MinValue:double?");
1286                         Assert.AreEqual (double.Epsilon, (double?) new XElement ("k", double.Epsilon), "Epsilon:double?");
1287                         Assert.AreEqual (double.NaN, (double?) new XElement ("k", double.NaN), "NaN:double?");
1288                         Assert.AreEqual (double.NegativeInfinity, (double?) new XElement ("k", double.NegativeInfinity), "-Inf:double?");
1289                         Assert.AreEqual (double.PositiveInfinity, (double?) new XElement ("k", double.PositiveInfinity), "+Inf:double?");
1290                         Assert.AreEqual (float.MaxValue, (float?) new XElement ("k", float.MaxValue), "MaxValue:float?");
1291                         Assert.AreEqual (float.MinValue, (float?) new XElement ("k", float.MinValue), "MinValue:float?");
1292                         Assert.AreEqual (float.Epsilon, (float?) new XElement ("k", float.Epsilon), "Epsilon:float?");
1293                         Assert.AreEqual (float.NaN, (float?) new XElement ("k", float.NaN), "NaN:float?");
1294                         Assert.AreEqual (float.NegativeInfinity, (float?) new XElement ("k", float.NegativeInfinity), "-Inf:float?");
1295                         Assert.AreEqual (float.PositiveInfinity, (float?) new XElement ("k", float.PositiveInfinity), "+Inf:float?");
1296                         Assert.AreEqual (int.MaxValue, (int?) new XElement ("k", int.MaxValue), "MaxValue:int?");
1297                         Assert.AreEqual (int.MinValue, (int?) new XElement ("k", int.MinValue), "MinValue:int?");
1298                         Assert.AreEqual (long.MaxValue, (long?) new XElement ("k", long.MaxValue), "MaxValue:long?");
1299                         Assert.AreEqual (long.MinValue, (long?) new XElement ("k", long.MinValue), "MinValue:long?");
1300                         Assert.AreEqual (uint.MaxValue, (uint?) new XElement ("k", uint.MaxValue), "MaxValue:uint?");
1301                         Assert.AreEqual (uint.MinValue, (uint?) new XElement ("k", uint.MinValue), "MinValue:uint?");
1302                         Assert.AreEqual (ulong.MaxValue, (ulong?) new XElement ("k", ulong.MaxValue), "MaxValue:ulong?");
1303                         Assert.AreEqual (ulong.MinValue, (ulong?) new XElement ("k", ulong.MinValue), "MinValue:ulong?");
1304                         Assert.AreEqual (DateTime.MaxValue, (DateTime) new XElement ("k", DateTime.MaxValue), "MaxValue:DateTime");
1305                         Assert.AreEqual (DateTime.MinValue, (DateTime) new XElement ("k", DateTime.MinValue), "MinValue:DateTime");
1306                         Assert.AreEqual (DateTime.MaxValue, (DateTime?) new XElement ("k", DateTime.MaxValue), "MaxValue:DateTime?");
1307                         Assert.AreEqual (DateTime.MinValue, (DateTime?) new XElement ("k", DateTime.MinValue), "MinValue:DateTime?");
1308                         Assert.AreEqual (DateTimeOffset.MaxValue, (DateTimeOffset) new XElement ("k", DateTimeOffset.MaxValue), "MaxValue:DateTimeOffset");
1309                         Assert.AreEqual (DateTimeOffset.MinValue, (DateTimeOffset) new XElement ("k", DateTimeOffset.MinValue), "MinValue:DateTimeOffset");
1310                         Assert.AreEqual (DateTimeOffset.MaxValue, (DateTimeOffset?) new XElement ("k", DateTimeOffset.MaxValue), "MaxValue:DateTimeOffset?");
1311                         Assert.AreEqual (DateTimeOffset.MinValue, (DateTimeOffset?) new XElement ("k", DateTimeOffset.MinValue), "MinValue:DateTimeOffset?");
1312                         Assert.AreEqual (TimeSpan.MaxValue, (TimeSpan) new XElement ("k", TimeSpan.MaxValue), "MaxValue:TimeSpan");
1313                         Assert.AreEqual (TimeSpan.MinValue, (TimeSpan) new XElement ("k", TimeSpan.MinValue), "MinValue:TimeSpan");
1314                         Assert.AreEqual (TimeSpan.MaxValue, (TimeSpan?) new XElement ("k", TimeSpan.MaxValue), "MaxValue:TimeSpan?");
1315                         Assert.AreEqual (TimeSpan.MinValue, (TimeSpan?) new XElement ("k", TimeSpan.MinValue), "MinValue:TimeSpan?");
1316                 }
1317
1318                 [Test]
1319                 public void CastBooleans ()
1320                 {
1321                         Assert.IsNotNull ((bool?) new XElement ("fq", "false"), "#1a");
1322                         Assert.AreEqual (false, ((bool?) new XElement ("fq", "false")).Value, "#1b");
1323                         Assert.IsNotNull ((bool?) new XElement ("tq", "true"), "#2a");
1324                         Assert.AreEqual (true, ((bool?) new XElement ("tq", "true")).Value, "#2b");
1325                         Assert.IsNotNull ((bool?) new XElement ("Fq", "False"), "#3a");
1326                         Assert.AreEqual (false, ((bool?) new XElement ("Fq", "False")).Value, "#3b");
1327                         Assert.IsNotNull ((bool?) new XElement ("Tq", "True"), "#4a");
1328                         Assert.AreEqual (true, ((bool?) new XElement ("Tq", "True")).Value, "#4b");
1329                         Assert.IsNotNull ((bool?) new XElement ("Fs", "   False \t \r "), "#5a");
1330                         Assert.AreEqual (false, ((bool?) new XElement ("Fs", "   False \t \r ")).Value, "#5b");
1331                         Assert.IsNotNull ((bool?) new XElement ("Ts", " \t True  \n  "), "#6a");
1332                         Assert.AreEqual (true, ((bool?) new XElement ("Ts", " \t True  \n  ")).Value, "#6b");
1333                         Assert.AreEqual (false, (bool) new XElement ("f", "false"), "#7");
1334                         Assert.AreEqual (true, (bool) new XElement ("t", "true"), "#8");
1335                         Assert.AreEqual (false, (bool) new XElement ("F", "False"), "#9");
1336                         Assert.AreEqual (true, (bool) new XElement ("T", "True"), "#10");
1337                         Assert.AreEqual (false, (bool)new XElement ("fs", " false  "), "#11");
1338                         Assert.AreEqual (true, (bool)new XElement ("ts", "  true "), "#12");
1339                         Assert.IsNotNull ((bool?) new XElement ("Tc", new XCData (" \t True  \n  ")), "#13a");
1340                         Assert.AreEqual (true, ((bool?) new XElement ("Tc", new XCData (" \t True  \n  "))).Value, "#13b");
1341                         Assert.AreEqual (false, (bool)new XElement ("fc", new XCData (" false  ")), "#14");
1342                         Assert.IsNotNull ((bool?) new XElement ("x", true), "#15a");
1343                         Assert.IsTrue (((bool?) new XElement ("x", true)).Value, "#15b");
1344                         Assert.IsTrue ((bool) new XElement ("x", true), "#15c");
1345                         Assert.IsNotNull ((bool?) new XElement ("x", false), "#16a");
1346                         Assert.IsFalse (((bool?) new XElement ("x", false)).Value, "#16b");
1347                         Assert.IsFalse ((bool) new XElement ("x", false), "#16c");
1348                         Assert.IsTrue ((bool) new XElement ("x", bool.TrueString), "#17a");
1349                         Assert.IsFalse ((bool) new XElement ("x", bool.FalseString), "#17b");
1350                         Assert.IsTrue ((bool) new XElement ("x", new XCData (bool.TrueString)), "#18a");
1351                         Assert.IsFalse ((bool) new XElement ("x", new XCData (bool.FalseString)), "#18b");
1352                 }
1353
1354                 [Test]
1355                 public void CastGuids ()
1356                 {
1357                         Guid rb = new Guid (new byte[16] { 0x9A, 0xBF, 0xCE, 0x7E, 0x07, 0x29, 0x9C, 0x43, 0x80, 0x7D, 0x48, 0x20, 0xB9, 0x19, 0xEA, 0x57 });
1358                         Guid rd = new Guid (new byte[16] { 0x21, 0x5B, 0x57, 0x26, 0xCD, 0x14, 0x5E, 0x44, 0x8F, 0xFA, 0xE2, 0xBC, 0x24, 0x7B, 0x2E, 0xC9 });
1359                         Guid rn = new Guid (new byte[16] { 0xF9, 0x46, 0x41, 0xA8, 0xA5, 0x03, 0xF1, 0x4A, 0xAD, 0x97, 0x7B, 0xC7, 0x79, 0x57, 0x2B, 0x79 });
1360                         Guid rp = new Guid (new byte[16] { 0x51, 0x6B, 0x8A, 0x17, 0xEF, 0x11, 0xFB, 0x48, 0x83, 0xBD, 0x57, 0xB4, 0x99, 0xF9, 0xC1, 0xE6 });
1361                         Guid rz = Guid.Empty;
1362                         Guid rx = Guid.NewGuid ();
1363
1364                         XElement b = new XElement ("b", "  {7ECEBF9A-2907-439c-807D-4820B919EA57}");
1365                         XElement d = new XElement ("d", "26575b21-14cd-445e-8ffa-e2bc247b2ec9");
1366                         XElement n = new XElement ("n", "a84146f903A54af1ad977bC779572b79\r\n");
1367                         XElement p = new XElement ("p", "  (178a6b51-11ef-48fb-83bd-57b499f9c1e6)  \t ");
1368                         XElement z = new XElement ("z", " \t \n 00000000-0000-0000-0000-000000000000 ");
1369                         XElement x = new XElement ("x", rx);
1370
1371                         Assert.IsNotNull ((Guid?) new XElement (b), "#1a");
1372                         Assert.AreEqual (rb, ((Guid?) new XElement (b)).Value, "#1b");
1373                         Assert.AreEqual (rb, (Guid) new XElement (b), "#1c");
1374                         Assert.AreEqual (rb, (Guid) new XElement ("r", rb), "#1d");
1375                         Assert.IsNotNull ((Guid?) new XElement ("r", rb), "#1e");
1376                         Assert.AreEqual (rb, ((Guid?) new XElement ("r", rb)).Value, "#1f");
1377
1378                         Assert.IsNotNull ((Guid?) new XElement (d), "#2a");
1379                         Assert.AreEqual (rd, ((Guid?) new XElement (d)).Value, "#2b");
1380                         Assert.AreEqual (rd, (Guid) new XElement (d), "#2c");
1381                         Assert.AreEqual (rd, (Guid) new XElement ("r", rd), "#2d");
1382                         Assert.IsNotNull ((Guid?) new XElement ("r", rd), "#2e");
1383                         Assert.AreEqual (rd, ((Guid?) new XElement ("r", rd)).Value, "#2f");
1384
1385                         Assert.IsNotNull ((Guid?) new XElement (n), "#3a");
1386                         Assert.AreEqual (rn, ((Guid?) new XElement (n)).Value, "#3b");
1387                         Assert.AreEqual (rn, (Guid) new XElement (n), "#3c");
1388                         Assert.AreEqual (rn, (Guid) new XElement ("r", rn), "#3d");
1389                         Assert.IsNotNull ((Guid?) new XElement ("r", rn), "#3e");
1390                         Assert.AreEqual (rn, ((Guid?) new XElement ("r", rn)).Value, "#3f");
1391
1392                         Assert.IsNotNull ((Guid?) new XElement (p), "#4a");
1393                         Assert.AreEqual (rp, ((Guid?) new XElement (p)).Value, "#4b");
1394                         Assert.AreEqual (rp, (Guid) new XElement (p), "#4c");
1395                         Assert.AreEqual (rp, (Guid) new XElement ("r", rp), "#4d");
1396                         Assert.IsNotNull ((Guid?) new XElement ("r", rp), "#4e");
1397                         Assert.AreEqual (rp, ((Guid?) new XElement ("r", rp)).Value, "#4f");
1398
1399                         Assert.IsNotNull ((Guid?) new XElement (z), "#5a");
1400                         Assert.AreEqual (rz, ((Guid?) new XElement (z)).Value, "#5b");
1401                         Assert.AreEqual (rz, (Guid) new XElement (z), "#5c");
1402
1403                         Assert.IsNotNull ((Guid?) new XElement (x), "#6a");
1404                         Assert.AreEqual (rx, ((Guid?) new XElement (x)).Value, "#6b");
1405                         Assert.AreEqual (rx, (Guid) new XElement (x), "#6c");
1406                 }
1407
1408                 [Test]
1409                 public void CastDateTimes ()
1410                 {
1411                         DateTime ra = new DateTime (1987, 1, 23, 21, 45, 36, 89, DateTimeKind.Unspecified);
1412                         DateTime rb = new DateTime (2001, 2, 3, 4, 5, 6, 789, DateTimeKind.Local);
1413                         DateTime rc = new DateTime (2010, 1, 2, 0, 0, 0, 0, DateTimeKind.Utc);
1414                         DateTime rd = new DateTime (1956, 11, 2, 0, 34, 0);
1415                         DateTime re = new DateTime (635085111683456297L, DateTimeKind.Utc);
1416                         DateTime rf = re.ToLocalTime ();
1417                         DateTime rx = DateTime.Now;
1418                         DateTime rz = DateTime.UtcNow;
1419
1420                         XElement a = new XElement ("a", "1987-01-23T21:45:36.089");
1421                         XElement b = new XElement ("b", "2001-02-03T04:05:06.789" + rb.ToString ("zzz"));
1422                         XElement c = new XElement ("c", "2010-01-02T00:00:00Z");
1423                         XElement d = new XElement ("d", "  Nov 2, 1956  12:34 AM \r\n   \t");
1424                         XElement e = new XElement ("e", "  2013-07-04T05:06:08.3456297Z   ");  // UTC, all the way
1425                         XElement f = new XElement ("f", "  2013-07-04T05:06:08.3456297+00:00   ");  // UTC initially, but should be converted automatically to local time
1426                         XElement x = new XElement ("x", rx);
1427                         XElement z = new XElement ("z", rz);
1428
1429                         Assert.IsNotNull ((DateTime?) new XElement (a), "#1a");
1430                         Assert.AreEqual (ra, ((DateTime?) new XElement (a)).Value, "#1b");
1431                         Assert.AreEqual (ra, (DateTime) new XElement (a), "#1c");
1432                         Assert.AreEqual (ra, (DateTime) new XElement ("r", ra), "#1d");
1433                         Assert.IsNotNull ((DateTime?) new XElement ("r", ra), "#1e");
1434                         Assert.AreEqual (ra, ((DateTime?) new XElement ("r", ra)).Value, "#1f");
1435
1436                         Assert.IsNotNull ((DateTime?) new XElement (b), "#2a");
1437                         Assert.AreEqual (rb, ((DateTime?) new XElement (b)).Value, "#2b");
1438                         Assert.AreEqual (rb, (DateTime) new XElement (b), "#2c");
1439                         Assert.AreEqual (rb, (DateTime) new XElement ("r", rb), "#2d");
1440                         Assert.IsNotNull ((DateTime?) new XElement ("r", rb), "#2e");
1441                         Assert.AreEqual (rb, ((DateTime?) new XElement ("r", rb)).Value, "#2f");
1442
1443                         Assert.IsNotNull ((DateTime?) new XElement (c), "#3a");
1444                         Assert.AreEqual (rc, ((DateTime?) new XElement (c)).Value, "#3b");
1445                         Assert.AreEqual (rc, (DateTime) new XElement (c), "#3c");
1446                         Assert.AreEqual (rc, (DateTime) new XElement ("r", rc), "#3d");
1447                         Assert.IsNotNull ((DateTime?) new XElement ("r", rc), "#3e");
1448                         Assert.AreEqual (rc, ((DateTime?) new XElement ("r", rc)).Value, "#3f");
1449
1450                         Assert.IsNotNull ((DateTime?) new XElement (d), "#4a");
1451                         Assert.AreEqual (rd, ((DateTime?) new XElement (d)).Value, "#4b");
1452                         Assert.AreEqual (rd, (DateTime) new XElement (d), "#4c");
1453                         Assert.AreEqual (rd, (DateTime) new XElement ("r", rd), "#4d");
1454                         Assert.IsNotNull ((DateTime?) new XElement ("r", rd), "#4e");
1455                         Assert.AreEqual (rd, ((DateTime?) new XElement ("r", rd)).Value, "#4f");
1456
1457                         Assert.IsNotNull ((DateTime?) new XElement (x), "#5a");
1458                         Assert.AreEqual (rx, ((DateTime?) new XElement (x)).Value, "#5b");
1459                         Assert.AreEqual (rx, (DateTime) new XElement (x), "#5c");
1460
1461                         Assert.IsNotNull ((DateTime?) new XElement (z), "#6a");
1462                         Assert.AreEqual (rz, ((DateTime?) new XElement (z)).Value, "#6b");
1463                         Assert.AreEqual (rz, (DateTime) new XElement (z), "#6c");
1464
1465                         Assert.IsNotNull ((DateTime?) new XElement (e), "#7a");
1466                         Assert.AreEqual (re, ((DateTime?) new XElement (e)).Value, "#7b");
1467                         Assert.AreEqual (re, (DateTime) new XElement (e), "#7c");
1468                         Assert.AreEqual (re, (DateTime) new XElement ("r", re), "#7d");
1469                         Assert.IsNotNull ((DateTime?) new XElement ("r", re), "#7e");
1470                         Assert.AreEqual (re, ((DateTime?) new XElement ("r", re)).Value, "#7f");
1471
1472                         Assert.IsNotNull ((DateTime?) new XElement (f), "#8a");
1473                         Assert.AreEqual (rf, ((DateTime?) new XElement (f)).Value, "#8b");
1474                         Assert.AreEqual (rf, (DateTime) new XElement (f), "#8c");
1475                         Assert.AreEqual (rf, (DateTime) new XElement ("r", rf), "#8d");
1476                         Assert.IsNotNull ((DateTime?) new XElement ("r", rf), "#8e");
1477                         Assert.AreEqual (rf, ((DateTime?) new XElement ("r", rf)).Value, "#8f");
1478                 }
1479
1480                 [Test]
1481                 public void CastDateTimeOffsets ()
1482                 {
1483                         DateTimeOffset ra = new DateTimeOffset (1987, 1, 23, 21, 45, 36, 89, TimeSpan.FromHours (+13.75));  // e.g., Chatham Islands (daylight-savings time)
1484                         DateTimeOffset rb = new DateTimeOffset (2001, 2, 3, 4, 5, 6, 789, DateTimeOffset.Now.Offset);  // Local time
1485                         DateTimeOffset rc = new DateTimeOffset (2010, 1, 2, 0, 0, 0, 0, TimeSpan.Zero);  // UTC
1486                         DateTimeOffset rd = new DateTimeOffset (1956, 11, 2, 12, 34, 10, TimeSpan.FromHours (-3.5));
1487                         DateTimeOffset re = new DateTimeOffset (630646468235678363, TimeSpan.FromHours (-1));  // UTC-1, also with full resolution and a fractional second that might lose a tick on Mono 2.6.1
1488                         DateTimeOffset rx = DateTimeOffset.Now;
1489                         DateTimeOffset rz = DateTimeOffset.UtcNow;
1490
1491                         XElement a = new XElement ("a", "1987-01-23T21:45:36.089+13:45");
1492                         XElement b = new XElement ("b", "2001-02-03T04:05:06.789" + DateTimeOffset.Now.ToString ("zzz"));
1493                         XElement c = new XElement ("c", "2010-01-02T00:00:00Z");
1494                         XElement d = new XElement ("d", "  Nov 2, 1956  12:34:10 PM   -3:30 \r\n   \t");
1495                         XElement e = new XElement ("e", " \t   \n  1999-06-10T21:27:03.5678363-01:00 ");
1496                         XElement x = new XElement ("x", rx);
1497                         XElement z = new XElement ("z", rz);
1498
1499                         Assert.IsNotNull ((DateTimeOffset?) new XElement (a), "#1a");
1500                         Assert.AreEqual (ra, ((DateTimeOffset?) new XElement (a)).Value, "#1b");
1501                         Assert.AreEqual (ra, (DateTimeOffset) new XElement (a), "#1c");
1502                         Assert.AreEqual (ra, (DateTimeOffset) new XElement ("r", ra), "#1d");
1503                         Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", ra), "#1e");
1504                         Assert.AreEqual (ra, ((DateTimeOffset?) new XElement ("r", ra)).Value, "#1f");
1505
1506                         Assert.IsNotNull ((DateTimeOffset?) new XElement (b), "#2a");
1507                         Assert.AreEqual (rb, ((DateTimeOffset?) new XElement (b)).Value, "#2b");
1508                         Assert.AreEqual (rb, (DateTimeOffset) new XElement (b), "#2c");
1509                         Assert.AreEqual (rb, (DateTimeOffset) new XElement ("r", rb), "#2d");
1510                         Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", rb), "#2e");
1511                         Assert.AreEqual (rb, ((DateTimeOffset?) new XElement ("r", rb)).Value, "#2f");
1512
1513                         Assert.IsNotNull ((DateTimeOffset?) new XElement (c), "#3a");
1514                         Assert.AreEqual (rc, ((DateTimeOffset?) new XElement (c)).Value, "#3b");
1515                         Assert.AreEqual (rc, (DateTimeOffset) new XElement (c), "#3c");
1516                         Assert.AreEqual (rc, (DateTimeOffset) new XElement ("r", rc), "#3d");
1517                         Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", rc), "#3e");
1518                         Assert.AreEqual (rc, ((DateTimeOffset?) new XElement ("r", rc)).Value, "#3f");
1519
1520                         AssertThrows<FormatException> (() => { DateTimeOffset? r = (DateTimeOffset?) new XElement (d); }, "#4a");
1521                         AssertThrows<FormatException> (() => { DateTimeOffset r = (DateTimeOffset) new XElement (d); }, "#4b");
1522                         Assert.AreEqual (rd, DateTimeOffset.Parse (d.Value), "#4c");  // Sanity check: Okay for standalone DateTimeOffset but not as XML as in above
1523
1524                         Assert.IsNotNull ((DateTimeOffset?) new XElement (x), "#5a");
1525                         Assert.AreEqual (rx, ((DateTimeOffset?) new XElement (x)).Value, "#5b");
1526                         Assert.AreEqual (rx, (DateTimeOffset) new XElement (x), "#5c");
1527
1528                         Assert.IsNotNull ((DateTimeOffset?) new XElement (z), "#6a");
1529                         Assert.AreEqual (rz, ((DateTimeOffset?) new XElement (z)).Value, "#6b");
1530                         Assert.AreEqual (rz, (DateTimeOffset) new XElement (z), "#6c");
1531
1532                         Assert.IsNotNull ((DateTimeOffset?) new XElement (e), "#7a");
1533                         Assert.AreEqual (re, ((DateTimeOffset?) new XElement (e)).Value, "#7b");
1534                         Assert.AreEqual (re, (DateTimeOffset) new XElement (e), "#7c");
1535                         Assert.AreEqual (re, (DateTimeOffset) new XElement ("r", re), "#7d");
1536                         Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", re), "#7e");
1537                         Assert.AreEqual (re, ((DateTimeOffset?) new XElement ("r", re)).Value, "#7f");
1538                 }
1539
1540                 [Test]
1541                 public void CastTimeSpans ()
1542                 {
1543                         TimeSpan ra = new TimeSpan (23, 21, 45, 36, 89);
1544                         TimeSpan rb = -new TimeSpan (3, 4, 5, 6, 789);
1545                         TimeSpan rc = new TimeSpan (2, 0, 0, 0, 0);
1546                         TimeSpan rd = new TimeSpan (0, 0, 0, 1);
1547                         TimeSpan re = new TimeSpan (1L);  // one tick, the smallest interval
1548                         TimeSpan rx = DateTimeOffset.Now.Offset;
1549                         TimeSpan rz = TimeSpan.Zero;
1550
1551                         XElement a = new XElement ("a", "P23DT21H45M36.089S");
1552                         XElement b = new XElement ("b", "-P3DT4H5M6.789S");
1553                         XElement c = new XElement ("c", "P2D");
1554                         XElement d = new XElement ("d", "PT1S");
1555                         XElement e = new XElement ("e", "     PT0.0000001S  \t \n   ");
1556                         XElement x = new XElement ("x", rx);
1557                         XElement z = new XElement ("z", rz);
1558
1559                         Assert.IsNotNull ((TimeSpan?) new XElement (a), "#1a");
1560                         Assert.AreEqual (ra, ((TimeSpan?) new XElement (a)).Value, "#1b");
1561                         Assert.AreEqual (ra, (TimeSpan) new XElement (a), "#1c");
1562                         Assert.AreEqual (ra, (TimeSpan) new XElement ("r", ra), "#1d");
1563                         Assert.IsNotNull ((TimeSpan?) new XElement ("r", ra), "#1e");
1564                         Assert.AreEqual (ra, ((TimeSpan?) new XElement ("r", ra)).Value, "#1f");
1565
1566                         Assert.IsNotNull ((TimeSpan?) new XElement (b), "#2a");
1567                         Assert.AreEqual (rb, ((TimeSpan?) new XElement (b)).Value, "#2b");
1568                         Assert.AreEqual (rb, (TimeSpan) new XElement (b), "#2c");
1569                         Assert.AreEqual (rb, (TimeSpan) new XElement ("r", rb), "#2d");
1570                         Assert.IsNotNull ((TimeSpan?) new XElement ("r", rb), "#2e");
1571                         Assert.AreEqual (rb, ((TimeSpan?) new XElement ("r", rb)).Value, "#2f");
1572
1573                         Assert.IsNotNull ((TimeSpan?) new XElement (c), "#3a");
1574                         Assert.AreEqual (rc, ((TimeSpan?) new XElement (c)).Value, "#3b");
1575                         Assert.AreEqual (rc, (TimeSpan) new XElement (c), "#3c");
1576                         Assert.AreEqual (rc, (TimeSpan) new XElement ("r", rc), "#3d");
1577                         Assert.IsNotNull ((TimeSpan?) new XElement ("r", rc), "#3e");
1578                         Assert.AreEqual (rc, ((TimeSpan?) new XElement ("r", rc)).Value, "#3f");
1579
1580                         Assert.IsNotNull ((TimeSpan?) new XElement (d), "#4a");
1581                         Assert.AreEqual (rd, ((TimeSpan?) new XElement (d)).Value, "#4b");
1582                         Assert.AreEqual (rd, (TimeSpan) new XElement (d), "#4c");
1583                         Assert.AreEqual (rd, (TimeSpan) new XElement ("r", rd), "#4d");
1584                         Assert.IsNotNull ((TimeSpan?) new XElement ("r", rd), "#4e");
1585                         Assert.AreEqual (rd, ((TimeSpan?) new XElement ("r", rd)).Value, "#4f");
1586
1587                         Assert.IsNotNull ((TimeSpan?) new XElement (x), "#5a");
1588                         Assert.AreEqual (rx, ((TimeSpan?) new XElement (x)).Value, "#5b");
1589                         Assert.AreEqual (rx, (TimeSpan) new XElement (x), "#5c");
1590
1591                         Assert.IsNotNull ((TimeSpan?) new XElement (z), "#6a");
1592                         Assert.AreEqual (rz, ((TimeSpan?) new XElement (z)).Value, "#6b");
1593                         Assert.AreEqual (rz, (TimeSpan) new XElement (z), "#6c");
1594
1595                         Assert.IsNotNull ((TimeSpan?) new XElement (e), "#7a");
1596                         Assert.AreEqual (re, ((TimeSpan?) new XElement (e)).Value, "#7b");
1597                         Assert.AreEqual (re, (TimeSpan) new XElement (e), "#7c");
1598                         Assert.AreEqual (re, (TimeSpan) new XElement ("r", re), "#7d");
1599                         Assert.IsNotNull ((TimeSpan?) new XElement ("r", re), "#7e");
1600                         Assert.AreEqual (re, ((TimeSpan?) new XElement ("r", re)).Value, "#7f");
1601                 }
1602 #pragma warning restore 219
1603
1604                 [Test]
1605                 public void Value ()
1606                 {
1607                         // based on bug #360858
1608                         XElement a = new XElement("root",
1609                                 new XElement ("foo"),
1610                                 "Linux&Windows",
1611                                 new XComment ("comment"),
1612                                 new XElement ("bar"));
1613                         Assert.AreEqual ("Linux&Windows", a.Value);
1614                 }
1615
1616                 [Test]
1617                 [ExpectedException (typeof (ArgumentException))]
1618                 public void SetValueXAttribute ()
1619                 {
1620                         new XElement ("foo").SetValue (new XAttribute ("foo", "bar"));
1621                 }
1622
1623                 [Test]
1624                 [ExpectedException (typeof (ArgumentException))]
1625                 public void SetValueXDocumnent ()
1626                 {
1627                         new XElement ("foo").SetValue (new XDocument ());
1628                 }
1629
1630                 [Test]
1631                 public void SetValue_ChangeTriggers()
1632                 {
1633                         bool changed = false;
1634                         bool changing = false;
1635                         
1636                         var element = new XElement("foo");
1637                         element.Changing += (o, e) => {
1638                                 Assert.IsFalse(changing, "#1");
1639                                 Assert.IsFalse(changed, "#2");
1640                                 Assert.IsTrue (o is XText, "#3");
1641                                 Assert.AreEqual("bar", ((XText)o).Value, "#4");
1642                                 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#5");
1643                                 changing = true;
1644                         };
1645                         element.Changed += (o, e) => {
1646                                 Assert.IsTrue(changing, "#5");
1647                                 Assert.IsFalse(changed, "#6");
1648                                 Assert.IsTrue (o is XText, "#7");
1649                                 Assert.AreEqual("bar", ((XText)o).Value, "#8");
1650                                 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#9");
1651                                 changed = true;
1652                         };
1653                         
1654                         element.SetValue("bar");
1655                         Assert.IsTrue(changed, "#changed");
1656                 }
1657
1658                 [Test]
1659                 // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
1660                 [ExpectedException (typeof (ArgumentException))]
1661                 [Category ("NotDotNet")]
1662                 [Ignore ("see code comment")]
1663                 public void SetValueXDeclaration ()
1664                 {
1665                         var el = new XElement ("foo");
1666                         el.SetValue (new XDeclaration ("1.0", null, null));
1667                         Assert.AreEqual ("<?xml version=\"1.0\"?>", el.Value);
1668                 }
1669
1670                 [Test]
1671                 [ExpectedException (typeof (ArgumentNullException))]
1672                 public void SetValueNull ()
1673                 {
1674                         new XElement ("foo", "text").SetValue (null);
1675                 }
1676
1677                 [Test]
1678                 public void AddSameInstance () // bug #392063
1679                 {
1680                         XElement root = new XElement (XName.Get ("Root", ""));
1681                         XElement child = new XElement (XName.Get ("Child", ""));
1682                         
1683                         root.Add (child);
1684                         root.Add (child);
1685                         Assert.AreEqual(2, root.Elements().Count(), "#1");
1686                         child.Remove ();
1687                         Assert.AreEqual(1, root.Elements().Count(), "#2");
1688                         AssertThrows<InvalidOperationException>(() => child.Remove(), "#3");
1689                 }
1690
1691                 [Test]
1692                 [ExpectedException (typeof (InvalidOperationException))]
1693                 public void AddSameInstance2 ()
1694                 {
1695                         XElement root = new XElement (XName.Get ("Root"));
1696                         XAttribute attr = new XAttribute (XName.Get ("a"), "v");
1697
1698                         root.Add (attr);
1699                         root.Add (attr); // duplicate attribute
1700                         Assert.AreEqual(2, root.Attributes().Count(), "#1");
1701                 }
1702
1703                 [Test]
1704                 public void AddAttributeFromDifferentTree ()
1705                 {
1706                         XElement e1 = new XElement (XName.Get ("e1"));
1707                         XElement e2 = new XElement (XName.Get ("e2"));
1708                         XAttribute attr = new XAttribute (XName.Get ("a"), "v");
1709
1710                         e1.Add (attr);
1711                         e2.Add (attr);
1712                         Assert.AreEqual ("<e1 a=\"v\" />", e1.ToString (), "#1");
1713                         Assert.AreEqual ("<e2 a=\"v\" />", e2.ToString (), "#2");
1714                 }
1715
1716                 [Test]
1717                 public void SavePreservePrefixes ()
1718                 {
1719                         var x = XDocument.Parse (@"
1720                         <xxx:a xmlns:xxx='http://www.foobar.com'>
1721     <xxx:b>blah blah blah</xxx:b>
1722 </xxx:a>");
1723                         StringWriter sw = new StringWriter ();
1724                         x.Save (sw, SaveOptions.DisableFormatting);
1725                         Assert.AreEqual (@"<?xml version=""1.0"" encoding=""utf-16""?><xxx:a xmlns:xxx=""http://www.foobar.com""><xxx:b>blah blah blah</xxx:b></xxx:a>", sw.ToString ());
1726                 }
1727
1728                 [Test]
1729                 public void LoadFromXmlTextReader ()
1730                 {
1731                         var foo = XElement.Load (new XmlTextReader (new StringReader ("<foo></foo>")));
1732                         Assert.IsNotNull (foo);
1733                 }
1734
1735                 [Test]
1736                 public void ReplaceNodes ()
1737                 {
1738                         var inputXml = "<Foo><C><Three>3</Three><Two></Two><One/></C><B><Aaa/><Yyy/><fff/></B><A Attrib=\"Hello World\"/></Foo>";
1739                         var reader = XmlReader.Create (new StringReader (inputXml), new XmlReaderSettings ());
1740                         XDocument doc = XDocument.Load (reader);
1741                         var result = doc.Root.Elements ().OrderBy (el => el.Name.ToString());
1742                         Assert.AreEqual (3, result.Count (), "#1");
1743                         doc.Root.FirstNode.Remove ();
1744                         Assert.AreEqual (2, result.Count (), "#2");
1745
1746                         XContainer container = doc.Root;
1747                         container.ReplaceNodes (result);
1748
1749                         Assert.AreEqual (2, container.Elements ().Count (), "#3");
1750                 }
1751
1752                 [Test]
1753                 public void ReplaceCreatesSnapshotBeforeRemoval ()
1754                 {
1755                         // bug #592435
1756                         XElement data1 = new XElement ("A");
1757                         XElement data3 = new XElement ("C");
1758                         XElement data4 = new XElement ("D");
1759                         XElement root = new XElement ("rt", 
1760                                                       new XElement ("z", new XElement ("Name", data1), new XElement ("Desc", data4)), data3);
1761                         var elements = root.Elements ().Elements ();
1762                         root.ReplaceNodes (elements);
1763                         root.Add (elements);
1764                         string xml = @"<rt>
1765   <Name>
1766     <A />
1767   </Name>
1768   <Desc>
1769     <D />
1770   </Desc>
1771   <A />
1772   <D />
1773 </rt>";
1774                         Assert.AreEqual (xml.NormalizeNewline (), root.ToString ().NormalizeNewline (), "#1");
1775                 }
1776
1777                 [Test]
1778                 public void AddBefore_ChildNode_ChangeTriggers()
1779                 {
1780                         int changed = 0;
1781                         int changing = 0;
1782                         var child = new XElement("child");
1783                         var root = new XElement("root", child);
1784                         root.Changed += (o, e) => changed++;
1785                         root.Changing += (o, e) => changing++;
1786
1787                         child.AddAfterSelf(new XElement("a"));
1788                         Assert.AreEqual(1, changed, "#1");
1789                         Assert.AreEqual(1, changing, "#2");
1790
1791                         child.AddBeforeSelf(new XElement("b"));
1792                         Assert.AreEqual(2, changed, "#3");
1793                         Assert.AreEqual(2, changing, "#4");
1794
1795                         child.AddFirst(new XElement("c"));
1796                         Assert.AreEqual(3, changed, "#5");
1797                         Assert.AreEqual(3, changing, "#6");
1798                 }
1799
1800                 [Test]
1801                 public void AddAttribute_ToRootNode_ChangeTriggers()
1802                 {
1803                         int changed = 0;
1804                         int changing = 0;
1805                         var node = new XElement("foo");
1806                         node.Changed += (o, e) => changed ++;
1807                         node.Changing += (o, e) => changing++;
1808
1809                         node.Add(new XAttribute("foo", "bar"));
1810                         Assert.AreEqual(1, changing, "#1");
1811                         Assert.AreEqual(1, changed, "#2");
1812
1813                         node.Add(new XAttribute("foo2", "bar2"));
1814                         Assert.AreEqual(2, changing, "#3");
1815                         Assert.AreEqual(2, changed, "#4");
1816                 }
1817
1818                 [Test]
1819                 public void SetAttributeValue_ToRootNode_ChangeTriggers()
1820                 {
1821                         int changed = 0;
1822                         int changing = 0;
1823                         var node = new XElement("foo");
1824                         node.Changed += (o, e) => changed++;
1825                         node.Changing += (o, e) => changing++;
1826
1827                         node.SetAttributeValue("foo", "bar");
1828                         Assert.AreEqual(1, changing, "#1");
1829                         Assert.AreEqual(1, changed, "#2");
1830
1831                         node.SetAttributeValue("foo2", "bar2");
1832                         Assert.AreEqual(2, changing, "#3");
1833                         Assert.AreEqual(2, changed, "#4");
1834
1835                         node.SetAttributeValue("foo2", null);
1836                         Assert.AreEqual(3, changing, "#7");
1837                         Assert.AreEqual(3, changed, "#8");
1838
1839                         node.SetAttributeValue("foo52", null);
1840                         Assert.AreEqual(3, changing, "#9");
1841                         Assert.AreEqual(3, changed, "#10");
1842                 }
1843
1844                 [Test]
1845                 public void RemoveAttributes_FromRootNode_ChangeTriggers()
1846                 {
1847                         int changed = 0;
1848                         int changing = 0;
1849                         var node = new XElement("foo", new XAttribute("foo", "bar"), new XAttribute("foo2", "bar2"), new XElement ("Barry"));
1850                         node.Changed += (o, e) => changed++;
1851                         node.Changing += (o, e) => changing++;
1852
1853                         node.RemoveAttributes();
1854                         Assert.AreEqual(2, changing, "#1");
1855                         Assert.AreEqual(2, changed, "#2");
1856                 }
1857
1858                 [Test]
1859                 public void RemoveNodes_FromRootNode_ChangeTriggers()
1860                 {
1861                         int changed = 0;
1862                         int changing = 0;
1863                         var node = new XElement("foo", new XAttribute("foo", "bar"), new XAttribute("foo2", "bar2"), new XElement("Barry"));
1864                         node.Changed += (o, e) => changed++;
1865                         node.Changing += (o, e) => changing++;
1866
1867                         node.RemoveNodes();
1868                         Assert.AreEqual(1, changing, "#1");
1869                         Assert.AreEqual(1, changed, "#2");
1870                 }
1871
1872                 [Test]
1873                 public void RemoveAll_FromRootNode_ChangeTriggers()
1874                 {
1875                         int changed = 0;
1876                         int changing = 0;
1877                         var node = new XElement("foo", new XAttribute("foo", "bar"), new XAttribute("foo2", "bar2"), new XElement("Barry"));
1878                         node.Changed += (o, e) => changed++;
1879                         node.Changing += (o, e) => changing++;
1880
1881                         node.RemoveAll ();
1882                         Assert.AreEqual(3, changing, "#1");
1883                         Assert.AreEqual(3, changed, "#2");
1884                 }
1885
1886                 [Test]
1887                 public void AddElement_ToRootNode_ChangeTriggers()
1888                 {
1889                         var childChanging = false;
1890                         var childChanged = false;
1891                         var rootChanging = false;
1892                         var rootChanged = false;
1893                         
1894                         var child = new XElement("foo");
1895                         var root = new XElement("root");
1896                         child.Changing += (o, e) => childChanging = true;
1897                         child.Changed += (o, e) => childChanged = true;
1898                         
1899                         root.Changing += (o, e) => {
1900                                 Assert.IsFalse(rootChanging, "#1");
1901                                 Assert.IsFalse(rootChanged, "#2");
1902                                 Assert.AreSame(child, o, "#3");
1903                                 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#4");
1904                                 rootChanging = true;
1905                         };
1906                         root.Changed += (o, e) => {
1907                                 Assert.IsFalse(rootChanged, "#5");
1908                                 Assert.IsTrue(rootChanging, "#6");
1909                                 Assert.AreSame(child, o, "#7");
1910                                 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#8");
1911                                 rootChanged = true;
1912                         };
1913                         
1914                         root.Add (child);
1915                         Assert.IsFalse(childChanging, "#9");
1916                         Assert.IsFalse(childChanged, "#10");
1917                         Assert.IsTrue(rootChanging, "#11");
1918                         Assert.IsTrue(rootChanged, "#12");
1919                 }
1920
1921                 [Test]
1922                 public void AddElement_ToChildNode_ChangeTriggers()
1923                 {
1924                         var childChanging = false;
1925                         var childChanged = false;
1926                         var rootChanging = false;
1927                         var rootChanged = false;
1928                         
1929                         var subchild = new XElement("subfoo");
1930                         var child = new XElement("foo");
1931                         var root = new XElement("root", child);
1932                         
1933                         child.Changing += (o, e) =>
1934                         {
1935                                 Assert.IsFalse(childChanging, "#c1");
1936                                 Assert.IsFalse(childChanged, "#c2");
1937                                 Assert.IsFalse(rootChanging, "#c3");
1938                                 Assert.IsFalse(rootChanged, "#c4");
1939                                 Assert.AreSame(subchild, o, "#c5");
1940                                 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#c6");
1941                                 Assert.IsNull(subchild.Parent, "childChangingParent");
1942                                 childChanging = true;
1943                         };
1944                         root.Changing += (o, e) =>
1945                         {
1946                                 Assert.IsTrue(childChanging, "#r1");
1947                                 Assert.IsFalse(childChanged, "#r2");
1948                                 Assert.IsFalse(rootChanging, "#r3");
1949                                 Assert.IsFalse(rootChanged, "#r4");
1950                                 Assert.AreSame(subchild, o, "#r5");
1951                                 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#r6");
1952                                 Assert.IsNull(subchild.Parent, "rootChangingParent");
1953                                 rootChanging = true;
1954                         };
1955                         child.Changed += (o, e) =>
1956                         {
1957                                 Assert.IsTrue(childChanging, "#c7");
1958                                 Assert.IsFalse(childChanged, "#c8");
1959                                 Assert.IsTrue(rootChanging, "#c9");
1960                                 Assert.IsFalse(rootChanged, "#c10");
1961                                 Assert.AreSame(subchild, o, "#c11");
1962                                 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#c12");
1963                                 Assert.IsNotNull(subchild.Parent, "childChangedParent");
1964                                 childChanged = true;
1965                         };
1966                         root.Changed += (o, e) =>
1967                         {
1968                                 Assert.IsTrue(childChanging, "#r7");
1969                                 Assert.IsTrue(childChanged, "#r8");
1970                                 Assert.IsTrue(rootChanging, "#r9");
1971                                 Assert.IsFalse(rootChanged, "#r10");
1972                                 Assert.AreSame(subchild, o, "#11");
1973                                 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#12");
1974                                 Assert.IsNotNull(subchild.Parent, "rootChangedParent");
1975                                 rootChanged = true;
1976                         };
1977                         
1978                         child.Add (subchild);
1979                         Assert.IsTrue(childChanging, "#a");
1980                         Assert.IsTrue(childChanged, "#b");
1981                         Assert.IsTrue(rootChanging, "#c");
1982                         Assert.IsTrue(rootChanged, "#d");
1983                 }
1984
1985                 [Test]
1986                 public void SetElementValue () // #699242
1987                 {
1988                         var element = XElement.Parse ("<foo><bar>bar</bar><baz>baz</baz></foo>");
1989                         element.SetElementValue ("bar", "babar");
1990                         element.SetElementValue ("baz", "babaz");
1991                         element.SetElementValue ("gaz", "gazonk");
1992
1993                         Assert.AreEqual ("<foo><bar>babar</bar><baz>babaz</baz><gaz>gazonk</gaz></foo>", element.ToString (SaveOptions.DisableFormatting));
1994
1995                         element.SetElementValue ("gaz", null);
1996                         Assert.AreEqual ("<foo><bar>babar</bar><baz>babaz</baz></foo>", element.ToString (SaveOptions.DisableFormatting));
1997                 }
1998
1999                 [Test]
2000                 public void Bug3137 ()
2001                 {
2002                         CultureInfo current = Thread.CurrentThread.CurrentCulture;
2003                         try {
2004                                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
2005                                 var element1 = new XElement ("Property1", new XAttribute ("type", "number"), 1.2343445);
2006                                 Assert.AreEqual ("<Property1 type=\"number\">1.2343445</Property1>", element1.ToString (), "en-US");
2007                                 
2008                                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("de-DE");
2009                                 // this was already working because the element was created with en-US
2010                                 Assert.AreEqual ("<Property1 type=\"number\">1.2343445</Property1>", element1.ToString (), "de-DE/1");
2011                                 // however creating a new, identical, element under de-DE return*ed* a different string
2012                                 var element2 = new XElement ("Property1", new XAttribute ("type", "number"), 1.2343445);
2013                                 Assert.AreEqual ("<Property1 type=\"number\">1.2343445</Property1>", element2.ToString (), "de-DE/2");
2014                         }
2015                         finally {
2016                                 Thread.CurrentThread.CurrentCulture = current;
2017                         }
2018                 }
2019
2020                 [Test]
2021                 public void DecimalFormatting () // bug #3634
2022                 {
2023                         var data = 5.5M;
2024                         var bak = Thread.CurrentThread.CurrentCulture;
2025                         Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfoByIetfLanguageTag("nl-NL");
2026                         try {
2027                                 var element = new XElement ("Demo", data);
2028                                 Assert.AreEqual ("<Demo>5.5</Demo>", element.ToString (), "#1");
2029                         } finally {
2030                                 Thread.CurrentThread.CurrentCulture = bak;
2031                         }
2032                 }
2033                 
2034                 [Test] // bug #3972
2035                 public void UseGetPrefixOfNamespaceForToString ()
2036                 {
2037                         string xml = @"
2038                         <xsi:Event
2039                           xsi1:type='xsi:SubscriptionEvent'
2040                           xmlns:xsi='http://relevo.se/xsi'
2041                           xmlns:xsi1='http://www.w3.org/2001/XMLSchema-instance'>
2042                           <xsi:eventData xsi1:type='xsi:CallSubscriptionEvent'/>
2043                         </xsi:Event>";
2044                         var e = XElement.Parse (xml);
2045                         string expected = @"<xsi:eventData xsi1:type='xsi:CallSubscriptionEvent' xmlns:xsi1='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsi='http://relevo.se/xsi' />".Replace ('\'', '"');
2046                         Assert.AreEqual (expected, e.Nodes ().First ().ToString (), "#1");
2047                 }
2048                 
2049                 [Test] // bug #5519
2050                 public void DoUseEmptyNamespacePrefixWhenApplicable ()
2051                 {
2052                         XNamespace ns = "http://jabber.org/protocol/geoloc";
2053                         XElement newElement = new XElement(ns + "geoloc");
2054                         Assert.AreEqual ("<geoloc xmlns=\"http://jabber.org/protocol/geoloc\" />", newElement.ToString (), "#1");
2055                 }
2056                 
2057                 [Test] // bug #10194
2058                 public void SetElementValueNullOnNonExistingElement ()
2059                 {
2060                         var xd = XDocument.Parse ("<foo />");
2061                         xd.Root.SetElementValue (XName.Get ("bar"), null);
2062                 }
2063                 
2064                 [Test] // bug #11298
2065                 public void ReplaceAttributesIteratesContentsFirstThenRemove ()
2066                 {
2067                         var xmlString = "<Class Id='1' Name='' Cluster='' xmlns='urn:x' />";
2068                         var e = XDocument.Parse (xmlString).Root;
2069                         var attrs = e.Attributes ()
2070                                 .Where (a => !a.IsNamespaceDeclaration)
2071                                 .Select (a => a.Name.Namespace != XNamespace.None ?
2072                                                  new XAttribute (XName.Get(a.Name.LocalName), a.Value) : a);
2073                         e.ReplaceAttributes (attrs);
2074                         Assert.IsNotNull (e.Attribute ("Id"), "#1");
2075                         Assert.IsNotNull (e.Attribute ("Name"), "#2");
2076                         Assert.IsNotNull (e.Attribute ("Cluster"), "#3");
2077                 }
2078
2079                 [XmlType ("Root")]
2080                 public class SerializableClass
2081                 {
2082                         [XmlAnyElement]
2083                         public XElement Content;
2084                 }
2085
2086 #if NET_4_5
2087                 [Test]
2088                 // Bug #12571
2089                 public void DeserializeXElement ()
2090                 {
2091                         var xmlString = "<Root><Data /></Root>";
2092
2093                         var serializer = new XmlSerializer (typeof (SerializableClass));
2094                         var res = serializer.Deserialize (new StringReader (xmlString));
2095
2096                         Assert.IsNotNull (res, "#1");
2097                         Assert.AreEqual (typeof (SerializableClass), res.GetType (), "#2");
2098                         var xe = (SerializableClass)res;
2099                         Assert.AreEqual (xe.Content.ToString (), "<Data />", "#3");
2100                 }
2101 #endif
2102
2103                 [Test] // Bug #20151
2104                 public void XElementFromArrayWithNullValuesAsObject ()
2105                 {
2106                         string[] content = {null, "content1", null, "content2"};
2107                         var el = new XElement ("test", (object)content);
2108                         Assert.AreEqual ("<test>content1content2</test>", el.ToString ());
2109                 }
2110         }
2111 }