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