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