2010-01-20 Zoltan Varga <vargaz@gmail.com>
[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.IO;
30 using System.Linq;
31 using System.Xml;
32 using System.Xml.Linq;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Xml.Linq
37 {
38         [TestFixture]
39         public class XElementTest
40         {
41                 [Test] // xml declaration is skipped.
42                 public void LoadWithXmldecl ()
43                 {
44                         string xml = "<?xml version='1.0'?><root />";
45                         XElement.Load (new StringReader (xml));
46                 }
47
48                 [Test]
49                 public void Load1 ()
50                 {
51                         string xml = "<root><foo/></root>";
52
53                         XElement el = XElement.Load (new StringReader (xml));
54                         XElement first = el.FirstNode as XElement;
55                         Assert.IsNotNull (first, "#1");
56                         Assert.IsTrue (el.LastNode is XElement, "#2");
57                         Assert.IsNull (el.NextNode, "#3");
58                         Assert.IsNull (el.PreviousNode, "#4");
59                         Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#5");
60                         Assert.AreEqual (el, first.Parent, "#6");
61                         Assert.AreEqual (first, el.LastNode, "#7");
62
63                         Assert.AreEqual ("root", el.Name.ToString (), "#8");
64                         Assert.AreEqual ("foo", first.Name.ToString (), "#9");
65                         Assert.IsFalse (el.Attributes ().GetEnumerator ().MoveNext (), "#10");
66                 }
67
68                 [Test]
69                 [ExpectedException (typeof (InvalidOperationException))]
70                 public void LoadInvalid ()
71                 {
72                         string xml = "text";
73                         XmlReaderSettings s = new XmlReaderSettings ();
74                         s.ConformanceLevel = ConformanceLevel.Fragment;
75
76                         XElement.Load (XmlReader.Create (new StringReader (xml), s));
77                 }
78
79                 [Test]
80                 public void PrecedingWhitespaces ()
81                 {
82                         string xml = "  <root/>";
83                         XmlReaderSettings s = new XmlReaderSettings ();
84                         s.ConformanceLevel = ConformanceLevel.Fragment;
85
86                         XElement.Load (XmlReader.Create (new StringReader (xml), s));
87                 }
88
89                 [Test]
90                 public void PrecedingWhitespaces2 ()
91                 {
92                         string xml = "  <root/>";
93                         XmlReaderSettings s = new XmlReaderSettings ();
94                         s.ConformanceLevel = ConformanceLevel.Fragment;
95
96                         XmlReader r = XmlReader.Create (new StringReader (xml), s);
97                         r.Read (); // at whitespace
98                         XElement.Load (r);
99                 }
100
101                 [Test]
102                 public void Load2 ()
103                 {
104                         string xml = "<root>foo</root>";
105
106                         XElement el = XElement.Load (new StringReader (xml));
107                         XText first = el.FirstNode as XText;
108                         Assert.IsNotNull (first, "#1");
109                         Assert.IsTrue (el.LastNode is XText, "#2");
110                         Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#3");
111                         Assert.AreEqual (el, first.Parent, "#4");
112                         Assert.AreEqual (first, el.LastNode, "#5");
113
114                         Assert.AreEqual ("foo", first.Value, "#6");
115                 }
116
117                 [Test]
118                 [ExpectedException (typeof (ArgumentException))]
119                 public void AddDocumentTypeToElement ()
120                 {
121                         XElement el = new XElement (XName.Get ("foo"));
122                         el.Add (new XDocumentType ("foo", null, null, null));
123                 }
124
125                 [Test]
126                 [ExpectedException (typeof (ArgumentException))]
127                 [Category ("NotDotNet")]
128                 public void AddXDeclarationToElement ()
129                 {
130                         XElement el = new XElement (XName.Get ("foo"));
131                         // LAMESPEC: in .NET, XDeclaration is not treated as
132                         // invalid, and converted to a string without error.
133                         el.Add (new XDeclaration ("1.0", null, null));
134                 }
135
136                 [Test]
137                 public void SetAttribute ()
138                 {
139                         XElement el = new XElement (XName.Get ("foo"));
140                         el.SetAttributeValue (XName.Get ("a1"), "v1");
141                         XAttribute a = el.FirstAttribute;
142                         Assert.IsNotNull (a, "#1-1");
143                         Assert.AreEqual (el, a.Parent, "#1-2");
144                         Assert.IsNotNull (el.LastAttribute, "#1-3");
145                         Assert.AreEqual (a, el.LastAttribute, "#1-4");
146                         Assert.AreEqual ("a1", a.Name.LocalName, "#1-5");
147                         Assert.AreEqual ("v1", a.Value, "#1-6");
148                         Assert.IsNull (a.PreviousAttribute, "#1-7");
149                         Assert.IsNull (a.NextAttribute, "#1-8");
150
151                         el.SetAttributeValue (XName.Get ("a2"), "v2");
152                         Assert.IsFalse (el.FirstAttribute == el.LastAttribute, "#2-1");
153                         Assert.AreEqual ("a2", el.LastAttribute.Name.LocalName, "#2-2");
154
155                         el.SetAttributeValue (XName.Get ("a1"), "v3");
156                         XAttribute b = el.FirstAttribute;
157                         Assert.IsNotNull (b, "#2-3");
158                         Assert.IsNotNull (el.LastAttribute, "#2-4");
159                         Assert.AreEqual ("a1", b.Name.LocalName, "#2-5");
160                         Assert.AreEqual ("v3", b.Value, "#2-6");
161                         Assert.AreEqual (a, b, "#2-7");
162                         XAttribute c = el.LastAttribute;
163                         Assert.AreEqual (a, c.PreviousAttribute, "#2-8");
164
165                         a.Remove ();
166                         Assert.IsNull (a.Parent, "#3-1");
167                         Assert.IsNull (a.PreviousAttribute, "#3-2");
168                         Assert.IsNull (a.NextAttribute, "#3-3");
169                         Assert.IsNull (c.PreviousAttribute, "#3-4");
170                         Assert.IsNull (c.NextAttribute, "#3-5");
171
172                         el.RemoveAttributes ();
173                         Assert.IsFalse (el.HasAttributes, "#4-1");
174                         Assert.IsNull (b.Parent, "#4-2");
175                         Assert.IsNull (c.Parent, "#4-3");
176                         Assert.IsNull (el.FirstAttribute, "#4-4");
177                         Assert.IsNull (el.LastAttribute, "#4-5");
178                 }
179
180                 [Test]
181                 public void AddAfterSelf ()
182                 {
183                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
184                         el.FirstNode.AddAfterSelf ("text");
185                         XText t = el.FirstNode.NextNode as XText;
186                         Assert.IsNotNull (t, "#1");
187                         Assert.AreEqual ("text", t.Value, "#2");
188                         XElement bar = t.NextNode as XElement;
189                         Assert.IsNotNull (bar, "#3");
190                         Assert.AreEqual ("bar", bar.Name.LocalName, "#4");
191                 }
192
193                 [Test]
194                 public void AddAfterSelfList ()
195                 {
196                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
197                         el.FirstNode.AddAfterSelf (new XText [] {
198                                 new XText ("t1"),
199                                 new XText ("t2"),
200                                 new XText ("t3")});
201                         XText t = el.FirstNode.NextNode as XText;
202                         Assert.IsNotNull (t, "#1");
203                         Assert.AreEqual ("t1", t.Value, "#2");
204                         Assert.AreEqual ("t2", ((XText) t.NextNode).Value, "#3");
205                         Assert.AreEqual ("t3", ((XText) t.NextNode.NextNode).Value, "#4");
206                         XElement bar = t.NextNode.NextNode.NextNode as XElement;
207                         Assert.IsNotNull (bar, "#5");
208                         Assert.AreEqual ("bar", bar.Name.LocalName, "#6");
209                 }
210
211                 [Test]
212                 [ExpectedException (typeof (ArgumentException))]
213                 public void AddAfterSelfAttribute ()
214                 {
215                         var el = new XElement ("root", new XElement ("child"));
216                         var el2 = el.FirstNode as XElement;
217                         el2.AddAfterSelf (new XAttribute ("foo", "bar"));
218                 }
219
220                 [Test]
221                 [ExpectedException (typeof (ArgumentException))]
222                 public void AddAfterSelfXDocument ()
223                 {
224                         var el = new XElement ("root", new XElement ("child"));
225                         var el2 = el.FirstNode as XElement;
226                         el2.AddAfterSelf (new XDocument ());
227                 }
228
229                 [Test]
230                 [ExpectedException (typeof (ArgumentException))]
231                 [Category ("NotDotNet")]
232                 [Category ("NotWorking")]
233                 // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
234                 public void AddAfterSelfXDeclaration ()
235                 {
236                         var el = new XElement ("root", new XElement ("child"));
237                         var el2 = el.FirstNode as XElement;
238                         el2.AddAfterSelf (new XDeclaration ("1.0", null, null));
239                 }
240
241                 [Test]
242                 public void AddAfterSelfCollection ()
243                 {
244                         var el = new XElement ("root", new XElement ("child"));
245                         el.FirstNode.AddAfterSelf (new List<XElement> (new XElement [] {new XElement ("foo"), new XElement ("bar")}));
246                         Assert.AreEqual ("<root><child /><foo /><bar /></root>", el.ToString (SaveOptions.DisableFormatting), "#1");
247                         Assert.AreEqual ("bar", (el.LastNode as XElement).Name.LocalName, "#2");
248                 }
249
250                 [Test]
251                 public void AddAfterSelfJoinsStringAfterText ()
252                 {
253                         var el = XElement.Parse ("<foo>text1</foo>");
254                         el.LastNode.AddAfterSelf ("text2");
255                         el.LastNode.AddAfterSelf (new XText ("text3"));
256                         IEnumerator<XNode> e = el.Nodes ().GetEnumerator ();
257                         Assert.IsTrue (e.MoveNext (), "#1");
258                         Assert.AreEqual ("text1text2", e.Current.ToString (), "#2");
259                         Assert.IsTrue (e.MoveNext (), "#3");
260                         Assert.AreEqual ("text3", e.Current.ToString (), "#4");
261                         Assert.IsFalse (e.MoveNext (), "#5");
262                 }
263
264                 [Test]
265                 public void AddBeforeSelf ()
266                 {
267                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
268                         el.FirstNode.AddBeforeSelf ("text");
269                         XText t = el.FirstNode as XText;
270                         Assert.IsNotNull (t, "#1");
271                         Assert.AreEqual ("text", t.Value, "#2");
272                         XElement foo = t.NextNode as XElement;
273                         Assert.IsNotNull (foo, "#3");
274                         Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
275                 }
276
277                 [Test]
278                 public void AddBeforeSelfList ()
279                 {
280                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
281                         el.FirstNode.AddBeforeSelf (new XText [] {
282                                 new XText ("t1"),
283                                 new XText ("t2"),
284                                 new XText ("t3")});
285                         XText t = el.FirstNode as XText;
286                         Assert.IsNotNull (t, "#1");
287                         Assert.AreEqual ("t1", t.Value, "#2");
288                         Assert.AreEqual ("t2", ((XText) t.NextNode).Value, "#3");
289                         Assert.AreEqual ("t3", ((XText) t.NextNode.NextNode).Value, "#4");
290                         XElement foo = t.NextNode.NextNode.NextNode as XElement;
291                         Assert.IsNotNull (foo, "#5");
292                         Assert.AreEqual ("foo", foo.Name.LocalName, "#6");
293                 }
294
295                 [Test]
296                 public void AddBeforeSelfList2 ()
297                 {
298                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
299                         el.FirstNode.AddBeforeSelf ("t1", "t2", "t3");
300                         XText t = el.FirstNode as XText;
301                         Assert.IsNotNull (t, "#1");
302                         Assert.AreEqual ("t1t2t3", t.Value, "#2");
303                         XElement foo = t.NextNode as XElement;
304                         Assert.IsNotNull (foo, "#3");
305                         Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
306                 }
307
308                 [Test]
309                 public void AddJoinsStringAfterText ()
310                 {
311                         var el = XElement.Parse ("<foo>text1</foo>");
312                         el.Add ("text2");
313                         el.Add (new XText ("text3"));
314                         IEnumerator<XNode> e = el.Nodes ().GetEnumerator ();
315                         Assert.IsTrue (e.MoveNext (), "#1");
316                         Assert.AreEqual ("text1text2", e.Current.ToString (), "#2");
317                         Assert.IsTrue (e.MoveNext (), "#3");
318                         Assert.AreEqual ("text3", e.Current.ToString (), "#4");
319                         Assert.IsFalse (e.MoveNext (), "#5");
320                 }
321
322                 [Test]
323                 [ExpectedException (typeof (InvalidOperationException))]
324                 public void AddDuplicateAttribute ()
325                 {
326                         var el = new XElement ("foo",
327                                 new XAttribute ("bar", "baz"));
328                         el.Add (new XAttribute ("bar", "baz"));
329                 }
330
331                 [Test]
332                 public void ReplaceWith ()
333                 {
334                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
335                         XNode fc = el.FirstNode;
336                         fc.ReplaceWith ("test");
337                         XText t = el.FirstNode as XText;
338                         Assert.IsNotNull (t, "#1");
339                         Assert.AreEqual ("test", t.Value, "#2");
340                 }
341
342                 [Test]
343                 public void ReplaceAll ()
344                 {
345                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
346                         el.ReplaceAll ("test");
347                         XText t = el.FirstNode as XText;
348                         Assert.IsNotNull (t, "#1");
349                         Assert.AreEqual ("test", t.Value, "#2");
350                         Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#3");
351                 }
352
353                 [Test]
354                 public void ReplaceAllList ()
355                 {
356                         XElement el = XElement.Parse ("<root><foo/><bar/></root>");
357                         el.ReplaceAll (
358                                 new XText ("test1"),
359                                 new XText ("test2"),
360                                 new XText ("test3"));
361                         XText t = el.FirstNode as XText;
362                         Assert.IsNotNull (t, "#1");
363                         Assert.AreEqual ("test1", t.Value, "#2");
364                         t = el.LastNode as XText;
365                         Assert.IsNotNull (t, "#3");
366                         Assert.AreEqual ("test3", t.Value, "#4");
367                         Assert.AreEqual (3, new List<XNode> (el.Nodes ()).Count, "#5");
368                 }
369
370                 [Test]
371                 public void ReplaceAttributes ()
372                 {
373                         XElement el = XElement.Parse ("<root x='y'><foo a='b'/></root>");
374                         Assert.IsTrue (el.Attributes ().GetEnumerator ().MoveNext (), "#0");
375                         el.ReplaceAttributes ("test");
376                         Assert.IsTrue (el.FirstNode is XElement, "#1");
377                         Assert.IsTrue (el.LastNode is XText, "#2");
378                         Assert.IsFalse (el.Attributes ().GetEnumerator ().MoveNext (), "#3");
379                 }
380
381                 [Test]
382                 public void GetDefaultNamespace ()
383                 {
384                         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>");
385                         XNamespace ns1 = XNamespace.Get ("urn:foo");
386                         Assert.AreEqual (ns1, el.GetDefaultNamespace (), "#1");
387                         XElement foo = (XElement) el.FirstNode;
388                         Assert.AreEqual (ns1, foo.GetDefaultNamespace (), "#2");
389                         Assert.AreEqual (ns1, ((XElement) foo.FirstNode).GetDefaultNamespace (), "#3");
390                         XElement bar = (XElement) foo.NextNode;
391                         Assert.AreEqual (ns1, bar.GetDefaultNamespace (), "#4");
392                         Assert.AreEqual (ns1, ((XElement) bar.FirstNode).GetDefaultNamespace (), "#5");
393                         XElement baz = (XElement) bar.NextNode;
394                         Assert.AreEqual (XNamespace.Get (String.Empty), baz.GetDefaultNamespace (), "#6");
395                         Assert.AreEqual (XNamespace.Get (String.Empty), ((XElement) baz.FirstNode).GetDefaultNamespace (), "#7");
396                 }
397
398                 [Test]
399                 public void GetPrefixNamespace ()
400                 {
401                         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>");
402                         XNamespace ns1 = XNamespace.Get ("urn:foo");
403                         XNamespace ns2 = XNamespace.Get ("urn:bar");
404                         Assert.AreEqual (ns1, el.GetNamespaceOfPrefix ("x"), "#1-1");
405                         Assert.AreEqual ("x", el.GetPrefixOfNamespace (ns1), "#1-2");
406                         XElement foo = (XElement) el.FirstNode;
407                         Assert.AreEqual (ns1, foo.GetNamespaceOfPrefix ("x"), "#2-1");
408                         Assert.AreEqual ("x", foo.GetPrefixOfNamespace (ns1), "#2-2");
409                         Assert.AreEqual (ns1, ((XElement) foo.FirstNode).GetNamespaceOfPrefix ("x"), "#3-1");
410                         Assert.AreEqual ("x", ((XElement) foo.FirstNode).GetPrefixOfNamespace (ns1), "#3-2");
411                         XElement bar = (XElement) foo.NextNode;
412                         Assert.AreEqual (ns2, bar.GetNamespaceOfPrefix ("x"), "#4-1");
413                         Assert.AreEqual ("x", bar.GetPrefixOfNamespace (ns2), "#4-2");
414                         Assert.AreEqual (null, bar.GetPrefixOfNamespace (ns1), "#4-3");
415                         Assert.AreEqual (ns2, ((XElement) bar.FirstNode).GetNamespaceOfPrefix ("x"), "#5-1");
416                         Assert.AreEqual ("x", ((XElement) bar.FirstNode).GetPrefixOfNamespace (ns2), "#5-2");
417                         Assert.AreEqual (null, ((XElement) bar.FirstNode).GetPrefixOfNamespace (ns1), "#5-3");
418                 }
419
420 #pragma warning disable 219
421                 [Test]
422                 public void CastNulls ()
423                 {
424                         const XElement a = null;
425
426                         Assert.AreEqual (null, (bool?) a, "bool?");
427                         Assert.AreEqual (null, (DateTime?) a, "DateTime?");
428                         Assert.AreEqual (null, (DateTimeOffset?) a, "DateTimeOffset?");
429                         Assert.AreEqual (null, (decimal?) a, "decimal?");
430                         Assert.AreEqual (null, (double?) a, "double?");
431                         Assert.AreEqual (null, (float?) a, "float?");
432                         Assert.AreEqual (null, (Guid?) a, "Guid?");
433                         Assert.AreEqual (null, (int?) a, "int?");
434                         Assert.AreEqual (null, (long?) a, "long?");
435                         Assert.AreEqual (null, (uint?) a, "uint?");
436                         Assert.AreEqual (null, (ulong?) a, "ulong?");
437                         Assert.AreEqual (null, (TimeSpan?) a, "TimeSpan?");
438                         Assert.AreEqual (null, (string) a, "string");
439                         AssertThrows<ArgumentNullException> (() => { bool z = (bool) a; }, "bool");
440                         AssertThrows<ArgumentNullException> (() => { DateTime z = (DateTime) a; }, "DateTime");
441                         AssertThrows<ArgumentNullException> (() => { DateTimeOffset z = (DateTimeOffset) a; }, "DateTimeOffset");
442                         AssertThrows<ArgumentNullException> (() => { decimal z = (decimal) a; }, "decimal");
443                         AssertThrows<ArgumentNullException> (() => { double z = (double) a; }, "double");
444                         AssertThrows<ArgumentNullException> (() => { float z = (float) a; }, "float");
445                         AssertThrows<ArgumentNullException> (() => { Guid z = (Guid) a; }, "Guid");
446                         AssertThrows<ArgumentNullException> (() => { int z = (int) a; }, "int");
447                         AssertThrows<ArgumentNullException> (() => { long z = (long) a; }, "long");
448                         AssertThrows<ArgumentNullException> (() => { uint z = (uint) a; }, "uint");
449                         AssertThrows<ArgumentNullException> (() => { ulong z = (ulong) a; }, "ulong");
450                         AssertThrows<ArgumentNullException> (() => { TimeSpan z = (TimeSpan) a; }, "TimeSpan");
451                 }
452
453                 /// <remarks>
454                 /// Provides functionality similar to Assert.Throws that is available on newer versions of NUnit.
455                 /// </remarks>
456                 private static T AssertThrows<T> (Action code, string message, params object[] args) where T : Exception
457                 {
458                         Exception actual = null;
459                         try {
460                                 code ();
461                         } catch (Exception exception) {
462                                 actual = exception;
463                         }
464                         Assert.That (actual, new NUnit.Framework.Constraints.ExactTypeConstraint (typeof (T)), message, args);
465                         return (T) actual;
466                 }
467
468                 [Test]
469                 public void CastEmpties ()
470                 {
471                         XElement a = new XElement ("a");
472
473                         // Verify expected "cloning" and "empty" behaviour as prerequisites
474                         Assert.IsTrue (a.IsEmpty, "#1-1");
475                         Assert.IsTrue (new XElement (a).IsEmpty, "#1-2");
476                         Assert.AreEqual (String.Empty, a.Value, "#2-1");
477                         Assert.AreEqual (String.Empty, new XElement (a).Value, "#2-2");
478                         Assert.AreNotSame (a, new XElement (a), "#3-1");
479                         Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2");
480                         Assert.AreEqual ("<a />", a.ToString (), "#3-3");
481                         Assert.AreEqual (a.ToString (), new XElement ("a", null).ToString (), "#3-4");
482
483                         // Execute the primary assertions of this test
484                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "bool?");
485                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "DateTime?");
486                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "DateTimeOffset?");
487                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "decimal?");
488                         AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "double?");
489                         AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "float?");
490                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "Guid?");
491                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "int?");
492                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "long?");
493                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "uint?");
494                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "ulong?");
495                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "TimeSpan?");
496                         Assert.AreEqual (String.Empty, (string) new XElement (a), "string");
497                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "bool");
498                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "DateTime");
499                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "DateTimeOffset");
500                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "decimal");
501                         AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "double");
502                         AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "float");
503                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "Guid");
504                         AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "int");
505                         AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "long");
506                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "uint");
507                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "ulong");
508                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "TimeSpan");
509                 }
510
511                 [Test]
512                 public void CastBlanks ()
513                 {
514                         XElement a = new XElement ("a", String.Empty);
515                         XElement b = new XElement ("b", new XCData (string.Empty));
516
517                         // Verify expected "cloning" and "blank" behaviour as prerequisites
518                         Assert.IsFalse (a.IsEmpty, "#1-1a");
519                         Assert.IsFalse (b.IsEmpty, "#1-1b");
520                         Assert.IsFalse (new XElement (a).IsEmpty, "#1-2a");
521                         Assert.IsFalse (new XElement (b).IsEmpty, "#1-2b");
522                         Assert.AreEqual (String.Empty, a.Value, "#2-1a");
523                         Assert.AreEqual (String.Empty, b.Value, "#2-1b");
524                         Assert.AreEqual (String.Empty, new XElement (a).Value, "#2-2a");
525                         Assert.AreEqual (String.Empty, new XElement (b).Value, "#2-2b");
526                         Assert.AreNotSame (a, new XElement (a), "#3-1a");
527                         Assert.AreNotSame (b, new XElement (b), "#3-1b");
528                         Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2a");
529                         Assert.AreEqual (b.ToString (), new XElement (b).ToString (), "#3-2b");
530                         Assert.AreEqual ("<a></a>", a.ToString (), "#3-3a");
531                         Assert.AreEqual ("<b><![CDATA[]]></b>", b.ToString (), "#3-3b");
532                         Assert.AreEqual (a.ToString (), new XElement ("a", "").ToString (), "#3-4a");
533                         Assert.AreEqual (b.ToString (), new XElement ("b", new XCData ("")).ToString (), "#3-4b");
534
535                         // Execute the primary assertions of this test
536                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
537                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
538                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
539                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
540                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
541                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
542                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "a:decimal?");
543                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (b); }, "b:decimal?");
544                         AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "a:double?");
545                         AssertThrows<FormatException> (() => { double? z = (double?) new XElement (b); }, "b:double?");
546                         AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "a:float?");
547                         AssertThrows<FormatException> (() => { float? z = (float?) new XElement (b); }, "b:float?");
548                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
549                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
550                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "a:int?");
551                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (b); }, "b:int?");
552                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "a:long?");
553                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (b); }, "b:long?");
554                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "a:uint?");
555                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (b); }, "b:uint?");
556                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "a:ulong?");
557                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (b); }, "b:ulong?");
558                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
559                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
560                         Assert.AreEqual (String.Empty, (string) new XElement (a), "a:string");
561                         Assert.AreEqual (String.Empty, (string) new XElement (b), "b:string");
562                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
563                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
564                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
565                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
566                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
567                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
568                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "a:decimal");
569                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (b); }, "b:decimal");
570                         AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "a:double");
571                         AssertThrows<FormatException> (() => { double z = (double) new XElement (b); }, "b:double");
572                         AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "a:float");
573                         AssertThrows<FormatException> (() => { float z = (float) new XElement (b); }, "b:float");
574                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
575                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
576                         AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "a:int");
577                         AssertThrows<FormatException> (() => { int z = (int) new XElement (b); }, "b:int");
578                         AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "a:long");
579                         AssertThrows<FormatException> (() => { long z = (long) new XElement (b); }, "b:long");
580                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "a:uint");
581                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (b); }, "b:uint");
582                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "a:ulong");
583                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (b); }, "b:ulong");
584                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
585                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
586                 }
587
588                 [Test]
589                 public void CastSpaces ()
590                 {
591                         XElement a = new XElement ("a", " ");
592                         XElement b = new XElement ("b", new XCData (" "));
593
594                         // Verify expected "cloning" and "space" behaviour as prerequisites
595                         Assert.IsFalse (a.IsEmpty, "#1-1a");
596                         Assert.IsFalse (b.IsEmpty, "#1-1b");
597                         Assert.IsFalse (new XElement (a).IsEmpty, "#1-2a");
598                         Assert.IsFalse (new XElement (b).IsEmpty, "#1-2b");
599                         Assert.AreEqual (" ", a.Value, "#2-1a");
600                         Assert.AreEqual (" ", b.Value, "#2-1b");
601                         Assert.AreEqual (" ", new XElement (a).Value, "#2-2a");
602                         Assert.AreEqual (" ", new XElement (b).Value, "#2-2b");
603                         Assert.AreNotSame (a, new XElement (a), "#3-1a");
604                         Assert.AreNotSame (b, new XElement (b), "#3-1b");
605                         Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2a");
606                         Assert.AreEqual (b.ToString (), new XElement (b).ToString (), "#3-2b");
607                         Assert.AreEqual ("<a> </a>", a.ToString (), "#3-3a");
608                         Assert.AreEqual ("<b><![CDATA[ ]]></b>", b.ToString (), "#3-3b");
609                         Assert.AreEqual (a.ToString (), new XElement ("a", ' ').ToString (), "#3-4");
610
611                         // Execute the primary assertions of this test
612                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
613                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
614                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
615                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
616                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
617                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
618                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "a:decimal?");
619                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (b); }, "b:decimal?");
620                         AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "a:double?");
621                         AssertThrows<FormatException> (() => { double? z = (double?) new XElement (b); }, "b:double?");
622                         AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "a:float?");
623                         AssertThrows<FormatException> (() => { float? z = (float?) new XElement (b); }, "b:float?");
624                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
625                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
626                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "a:int?");
627                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (b); }, "b:int?");
628                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "a:long?");
629                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (b); }, "b:long?");
630                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "a:uint?");
631                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (b); }, "b:uint?");
632                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "a:ulong?");
633                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (b); }, "b:ulong?");
634                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
635                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
636                         Assert.AreEqual (" ", (string) new XElement (a), "a:string");
637                         Assert.AreEqual (" ", (string) new XElement (b), "b:string");
638                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
639                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
640                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
641                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
642                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
643                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
644                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "a:decimal");
645                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (b); }, "b:decimal");
646                         AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "a:double");
647                         AssertThrows<FormatException> (() => { double z = (double) new XElement (b); }, "b:double");
648                         AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "a:float");
649                         AssertThrows<FormatException> (() => { float z = (float) new XElement (b); }, "b:float");
650                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
651                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
652                         AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "a:int");
653                         AssertThrows<FormatException> (() => { int z = (int) new XElement (b); }, "b:int");
654                         AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "a:long");
655                         AssertThrows<FormatException> (() => { long z = (long) new XElement (b); }, "b:long");
656                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "a:uint");
657                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (b); }, "b:uint");
658                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "a:ulong");
659                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (b); }, "b:ulong");
660                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
661                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
662                 }
663
664                 [Test]
665                 public void CastNumbers ()
666                 {
667                         XElement a = new XElement ("a", "7");
668                         XElement b = new XElement ("b", new XCData ("  42 "));
669                         XElement c = new XElement ("c", " \r\n   13 \t  ");
670                         XElement d = new XElement ("d", -101);
671                         XElement o = new XElement ("o", "0");
672                         XElement l = new XElement ("l", "1");
673                         XElement I = new XElement ("I", "INF");
674                         XElement i = new XElement ("i", " Infinity  ");
675                         XElement M = new XElement ("M", "   -INF ");
676                         XElement m = new XElement ("m", "-Infinity");
677                         XElement n = new XElement ("n", "\t NaN   ");
678
679                         // Verify expected "cloning" and basic conversion behaviour as prerequisites
680                         Assert.IsFalse (a.IsEmpty, "#1-1");
681                         Assert.IsFalse (new XElement (b).IsEmpty, "#1-2");
682                         Assert.AreEqual (" \r\n   13 \t  ", c.Value, "#2-1");
683                         Assert.AreEqual ("-101", new XElement (d).Value, "#2-2");
684                         Assert.AreNotSame (o, new XElement (o), "#3-1");
685                         Assert.AreEqual (l.ToString (), new XElement (l).ToString (), "#3-2");
686                         Assert.AreEqual ("<a>7</a>", a.ToString (), "#3-3a");
687                         Assert.AreEqual ("<b><![CDATA[  42 ]]></b>", b.ToString (), "#3-3b");
688                         Assert.AreEqual ("<c> \r\n   13 \t  </c>", c.ToString (), "#3-3c");
689                         Assert.AreEqual ("<d>-101</d>", d.ToString (), "#3-3d");
690                         Assert.AreEqual ("<o>0</o>", new XElement ("o", 0.0).ToString (), "#3-3o");
691                         Assert.AreEqual ("<l>1</l>", new XElement ("l", 1.0f).ToString (), "#3-3l");
692                         Assert.AreEqual ("<n>NaN</n>", new XElement ("n", double.NaN).ToString (), "#3-3n");
693                         Assert.AreEqual (a.ToString (), new XElement ("a", '7').ToString (), "#3-4a");
694                         Assert.AreEqual (d.ToString (), new XElement ("d", "-101").ToString (), "#3-4d");
695                         Assert.AreEqual (o.ToString (), new XElement ("o", 0L).ToString (), "#3-4o");
696                         Assert.AreEqual (l.ToString (), new XElement ("l", 1m).ToString (), "#3-4l");
697
698                         // Execute the primary assertions of this test
699                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
700                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
701                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (c); }, "c:bool?");
702                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (d); }, "d:bool?");
703                         Assert.IsNotNull ((bool?) new XElement (o), "o:bool?:null");
704                         Assert.AreEqual (false, ((bool?) new XElement (o)).Value, "o:bool?:value");
705                         Assert.IsNotNull ((bool?) new XElement (l), "l:bool?:null");
706                         Assert.AreEqual (true, ((bool?) new XElement (l)).Value, "l:bool?:value");
707                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (I); }, "I:bool?");
708                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (i); }, "i:bool?");
709                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (M); }, "M:bool?");
710                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (m); }, "m:bool?");
711                         AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (n); }, "n:bool?");
712                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
713                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
714                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (c); }, "c:DateTime?");
715                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (d); }, "d:DateTime?");
716                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (o); }, "o:DateTime?");
717                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (l); }, "l:DateTime?");
718                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (I); }, "I:DateTime?");
719                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (i); }, "i:DateTime?");
720                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (M); }, "M:DateTime?");
721                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (m); }, "m:DateTime?");
722                         AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (n); }, "n:DateTime?");
723                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
724                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
725                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (c); }, "c:DateTimeOffset?");
726                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (d); }, "d:DateTimeOffset?");
727                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (o); }, "o:DateTimeOffset?");
728                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (l); }, "l:DateTimeOffset?");
729                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (I); }, "I:DateTimeOffset?");
730                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (i); }, "i:DateTimeOffset?");
731                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (M); }, "M:DateTimeOffset?");
732                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (m); }, "m:DateTimeOffset?");
733                         AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (n); }, "n:DateTimeOffset?");
734                         Assert.IsNotNull ((decimal?) new XElement (a), "a:decimal?:null");
735                         Assert.AreEqual (7m, ((decimal?) new XElement (a)).Value, "a:decimal?:value");
736                         Assert.IsNotNull ((decimal?) new XElement (b), "b:decimal?:null");
737                         Assert.AreEqual (42m, ((decimal?) new XElement (b)).Value, "b:decimal?:value");
738                         Assert.IsNotNull ((decimal?) new XElement (c), "c:decimal?:null");
739                         Assert.AreEqual (13m, ((decimal?) new XElement (c)).Value, "c:decimal?:value");
740                         Assert.IsNotNull ((decimal?) new XElement (d), "d:decimal?:null");
741                         Assert.AreEqual (-101m, ((decimal?) new XElement (d)).Value, "d:decimal?:value");
742                         Assert.IsNotNull ((decimal?) new XElement (o), "o:decimal?:null");
743                         Assert.AreEqual (0m, ((decimal?) new XElement (o)).Value, "o:decimal?:value");
744                         Assert.IsNotNull ((decimal?) new XElement (l), "l:decimal?:null");
745                         Assert.AreEqual (1m, ((decimal?) new XElement (l)).Value, "l:decimal?:value");
746                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (I); }, "I:decimal?");
747                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (i); }, "i:decimal?");
748                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (M); }, "M:decimal?");
749                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (m); }, "m:decimal?");
750                         AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (n); }, "n:decimal?");
751                         Assert.IsNotNull ((double?) new XElement (a), "a:double?:null");
752                         Assert.AreEqual (7d, ((double?) new XElement (a)).Value, "a:double?:value");
753                         Assert.IsNotNull ((double?) new XElement (b), "b:double?:null");
754                         Assert.AreEqual (42d, ((double?) new XElement (b)).Value, "b:double?:value");
755                         Assert.IsNotNull ((double?) new XElement (c), "c:double?:null");
756                         Assert.AreEqual (13d, ((double?) new XElement (c)).Value, "c:double?:value");
757                         Assert.IsNotNull ((double?) new XElement (d), "d:double?:null");
758                         Assert.AreEqual (-101d, ((double?) new XElement (d)).Value, "d:double?:value");
759                         Assert.IsNotNull ((double?) new XElement (o), "o:double?:null");
760                         Assert.AreEqual (0d, ((double?) new XElement (o)).Value, "o:double?:value");
761                         Assert.IsNotNull ((double?) new XElement (l), "l:double?:null");
762                         Assert.AreEqual (1d, ((double?) new XElement (l)).Value, "l:double?:value");
763                         Assert.IsNotNull ((double?) new XElement (I), "I:double?:null");
764                         Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (I)).Value, "I:double?:value");
765                         Assert.IsNotNull ((double?) new XElement (i), "i:double?:null");
766                         Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (i)).Value, "i:double?:value");
767                         Assert.IsNotNull ((double?) new XElement (M), "M:double?:null");
768                         Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (M)).Value, "M:double?:value");
769                         Assert.IsNotNull ((double?) new XElement (m), "m:double?:null");
770                         Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (m)).Value, "m:double?:value");
771                         Assert.IsNotNull ((double?) new XElement (n), "n:double?:null");
772                         Assert.IsNaN (((double?) new XElement (n)).Value, "n:double?:value");
773                         Assert.IsNotNull ((float?) new XElement (a), "a:float?:null");
774                         Assert.AreEqual (7f, ((float?) new XElement (a)).Value, "a:float?:value");
775                         Assert.IsNotNull ((float?) new XElement (b), "b:float?:null");
776                         Assert.AreEqual (42f, ((float?) new XElement (b)).Value, "b:float?:value");
777                         Assert.IsNotNull ((float?) new XElement (c), "c:float?:null");
778                         Assert.AreEqual (13f, ((float?) new XElement (c)).Value, "c:float?:value");
779                         Assert.IsNotNull ((float?) new XElement (d), "d:float?:null");
780                         Assert.AreEqual (-101f, ((float?) new XElement (d)).Value, "d:float?:value");
781                         Assert.IsNotNull ((float?) new XElement (o), "o:float?:null");
782                         Assert.AreEqual (0f, ((float?) new XElement (o)).Value, "o:float?:value");
783                         Assert.IsNotNull ((float?) new XElement (l), "l:float?:null");
784                         Assert.AreEqual (1f, ((float?) new XElement (l)).Value, "l:float?:value");
785                         Assert.IsNotNull ((float?) new XElement (I), "I:float?:null");
786                         Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (I)).Value, "I:float?:value");
787                         Assert.IsNotNull ((float?) new XElement (i), "i:float?:null");
788                         Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (i)).Value, "i:float?:value");
789                         Assert.IsNotNull ((float?) new XElement (M), "M:float?:null");
790                         Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (M)).Value, "M:float?:value");
791                         Assert.IsNotNull ((float?) new XElement (m), "m:float?:null");
792                         Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (m)).Value, "m:float?:value");
793                         Assert.IsNotNull ((float?) new XElement (n), "n:float?:null");
794                         Assert.IsNaN (((float?) new XElement (n)).Value, "n:float?:value");
795                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
796                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
797                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (c); }, "c:Guid?");
798                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (d); }, "d:Guid?");
799                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (o); }, "o:Guid?");
800                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (l); }, "l:Guid?");
801                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (I); }, "I:Guid?");
802                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (i); }, "i:Guid?");
803                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (M); }, "M:Guid?");
804                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (m); }, "m:Guid?");
805                         AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (n); }, "n:Guid?");
806                         Assert.IsNotNull ((int?) new XElement (a), "a:int?:null");
807                         Assert.AreEqual (7, ((int?) new XElement (a)).Value, "a:int?:value");
808                         Assert.IsNotNull ((int?) new XElement (b), "b:int?:null");
809                         Assert.AreEqual (42, ((int?) new XElement (b)).Value, "b:int?:value");
810                         Assert.IsNotNull ((int?) new XElement (c), "c:int?:null");
811                         Assert.AreEqual (13, ((int?) new XElement (c)).Value, "c:int?:value");
812                         Assert.IsNotNull ((int?) new XElement (d), "d:int?:null");
813                         Assert.AreEqual (-101, ((int?) new XElement (d)).Value, "d:int?:value");
814                         Assert.IsNotNull ((int?) new XElement (o), "o:int?:null");
815                         Assert.AreEqual (0, ((int?) new XElement (o)).Value, "o:int?:value");
816                         Assert.IsNotNull ((int?) new XElement (l), "l:int?:null");
817                         Assert.AreEqual (1, ((int?) new XElement (l)).Value, "l:int?:value");
818                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (I); }, "I:int?");
819                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (i); }, "i:int?");
820                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (M); }, "M:int?");
821                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (m); }, "m:int?");
822                         AssertThrows<FormatException> (() => { int? z = (int?) new XElement (n); }, "n:int?");
823                         Assert.IsNotNull ((long?) new XElement (a), "a:long?:null");
824                         Assert.AreEqual (7L, ((long?) new XElement (a)).Value, "a:long?:value");
825                         Assert.IsNotNull ((long?) new XElement (b), "b:long?:null");
826                         Assert.AreEqual (42L, ((long?) new XElement (b)).Value, "b:long?:value");
827                         Assert.IsNotNull ((long?) new XElement (c), "c:long?:null");
828                         Assert.AreEqual (13L, ((long?) new XElement (c)).Value, "c:long?:value");
829                         Assert.IsNotNull ((long?) new XElement (d), "d:long?:null");
830                         Assert.AreEqual (-101L, ((long?) new XElement (d)).Value, "d:long?:value");
831                         Assert.IsNotNull ((long?) new XElement (o), "o:long?:null");
832                         Assert.AreEqual (0L, ((long?) new XElement (o)).Value, "o:long?:value");
833                         Assert.IsNotNull ((long?) new XElement (l), "l:long?:null");
834                         Assert.AreEqual (1L, ((long?) new XElement (l)).Value, "l:long?:value");
835                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (I); }, "I:long?");
836                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (i); }, "i:long?");
837                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (M); }, "M:long?");
838                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (m); }, "m:long?");
839                         AssertThrows<FormatException> (() => { long? z = (long?) new XElement (n); }, "n:long?");
840                         Assert.IsNotNull ((uint?) new XElement (a), "a:uint?:null");
841                         Assert.AreEqual (7u, ((uint?) new XElement (a)).Value, "a:uint?:value");
842                         Assert.IsNotNull ((uint?) new XElement (b), "b:uint?:null");
843                         Assert.AreEqual (42u, ((uint?) new XElement (b)).Value, "b:uint?:value");
844                         Assert.IsNotNull ((uint?) new XElement (c), "c:uint?:null");
845                         Assert.AreEqual (13u, ((uint?) new XElement (c)).Value, "c:uint?:value");
846                         // LAMESPEC: see XmlConvertTests.ToUInt32().
847                         //AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (d); }, "d:uint?");
848                         Assert.IsNotNull ((uint?) new XElement (o), "o:uint?:null");
849                         Assert.AreEqual (0u, ((uint?) new XElement (o)).Value, "o:uint?:value");
850                         Assert.IsNotNull ((uint?) new XElement (l), "l:uint?:null");
851                         Assert.AreEqual (1u, ((uint?) new XElement (l)).Value, "l:uint?:value");
852                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (I); }, "I:uint?");
853                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (i); }, "i:uint?");
854                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (M); }, "M:uint?");
855                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (m); }, "m:uint?");
856                         AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (n); }, "n:uint?");
857                         Assert.IsNotNull ((ulong?) new XElement (a), "a:ulong?:null");
858                         Assert.AreEqual (7UL, ((ulong?) new XElement (a)).Value, "a:ulong?:value");
859                         Assert.IsNotNull ((ulong?) new XElement (b), "b:ulong?:null");
860                         Assert.AreEqual (42UL, ((ulong?) new XElement (b)).Value, "b:ulong?:value");
861                         Assert.IsNotNull ((ulong?) new XElement (c), "c:ulong?:null");
862                         Assert.AreEqual (13UL, ((ulong?) new XElement (c)).Value, "c:ulong?:value");
863                         // LAMESPEC: see XmlConvertTests.ToUInt64().
864                         //AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (d); }, "d:ulong?");
865                         Assert.IsNotNull ((ulong?) new XElement (o), "o:ulong?:null");
866                         Assert.AreEqual (0UL, ((ulong?) new XElement (o)).Value, "o:ulong?:value");
867                         Assert.IsNotNull ((ulong?) new XElement (l), "l:ulong?:null");
868                         Assert.AreEqual (1UL, ((ulong?) new XElement (l)).Value, "l:ulong?:value");
869                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (I); }, "I:ulong?");
870                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (i); }, "i:ulong?");
871                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (M); }, "M:ulong?");
872                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (m); }, "m:ulong?");
873                         AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (n); }, "n:ulong?");
874                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
875                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
876                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (c); }, "c:TimeSpan?");
877                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (d); }, "d:TimeSpan?");
878                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (o); }, "o:TimeSpan?");
879                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (l); }, "l:TimeSpan?");
880                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (I); }, "I:TimeSpan?");
881                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (i); }, "i:TimeSpan?");
882                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (M); }, "M:TimeSpan?");
883                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (m); }, "m:TimeSpan?");
884                         AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (n); }, "n:TimeSpan?");
885                         Assert.AreEqual ("7", (string) new XElement (a), "a:string");
886                         Assert.AreEqual ("  42 ", (string) new XElement (b), "b:string");
887                         Assert.AreEqual (" \r\n   13 \t  ", (string) new XElement (c), "c:string");
888                         Assert.AreEqual ("-101", (string) new XElement (d), "d:string");
889                         Assert.AreEqual ("0", (string) new XElement (o), "o:string");
890                         Assert.AreEqual ("1", (string) new XElement (l), "l:string");
891                         Assert.AreEqual ("INF", (string) new XElement (I), "I:string");
892                         Assert.AreEqual (" Infinity  ", (string) new XElement (i), "i:string");
893                         Assert.AreEqual ("   -INF ", (string) new XElement (M), "M:string");
894                         Assert.AreEqual ("-Infinity", (string) new XElement (m), "m:string");
895                         Assert.AreEqual ("\t NaN   ", (string) new XElement (n), "n:string");
896                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
897                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
898                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (c); }, "c:bool");
899                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (d); }, "d:bool");
900                         Assert.AreEqual (false, (bool) new XElement (o), "o:bool");
901                         Assert.AreEqual (true, (bool) new XElement (l), "l:bool");
902                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (I); }, "I:bool");
903                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (i); }, "i:bool");
904                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (M); }, "M:bool");
905                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (m); }, "m:bool");
906                         AssertThrows<FormatException> (() => { bool z = (bool) new XElement (n); }, "n:bool");
907                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
908                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
909                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (c); }, "c:DateTime");
910                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (d); }, "d:DateTime");
911                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (o); }, "o:DateTime");
912                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (l); }, "l:DateTime");
913                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (I); }, "I:DateTime");
914                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (i); }, "i:DateTime");
915                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (M); }, "M:DateTime");
916                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (m); }, "m:DateTime");
917                         AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (n); }, "n:DateTime");
918                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
919                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
920                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (c); }, "c:DateTimeOffset");
921                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (d); }, "d:DateTimeOffset");
922                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (o); }, "o:DateTimeOffset");
923                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (l); }, "l:DateTimeOffset");
924                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (I); }, "I:DateTimeOffset");
925                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (i); }, "i:DateTimeOffset");
926                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (M); }, "M:DateTimeOffset");
927                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (m); }, "m:DateTimeOffset");
928                         AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (n); }, "n:DateTimeOffset");
929                         Assert.AreEqual (7m, (decimal) new XElement (a), "a:decimal");
930                         Assert.AreEqual (42m, (decimal) new XElement (b), "b:decimal");
931                         Assert.AreEqual (13m, (decimal) new XElement (c), "c:decimal");
932                         Assert.AreEqual (-101m, (decimal) new XElement (d), "d:decimal");
933                         Assert.AreEqual (0m, (decimal) new XElement (o), "o:decimal");
934                         Assert.AreEqual (1m, (decimal) new XElement (l), "l:decimal");
935                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (I); }, "I:decimal");
936                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (i); }, "i:decimal");
937                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (M); }, "M:decimal");
938                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (m); }, "m:decimal");
939                         AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (n); }, "n:decimal");
940                         Assert.AreEqual (7d, (double) new XElement (a), "a:double");
941                         Assert.AreEqual (42d, (double) new XElement (b), "b:double");
942                         Assert.AreEqual (13d, (double) new XElement (c), "c:double");
943                         Assert.AreEqual (-101d, (double) new XElement (d), "d:double");
944                         Assert.AreEqual (0d, (double) new XElement (o), "o:double");
945                         Assert.AreEqual (1d, (double) new XElement (l), "l:double");
946                         Assert.AreEqual (double.PositiveInfinity, (double) new XElement (I), "I:double");
947                         Assert.AreEqual (double.PositiveInfinity, (double) new XElement (i), "i:double");
948                         Assert.AreEqual (double.NegativeInfinity, (double) new XElement (M), "M:double");
949                         Assert.AreEqual (double.NegativeInfinity, (double) new XElement (m), "m:double");
950                         Assert.IsNaN (((double) new XElement (n)), "n:double");
951                         Assert.AreEqual (7f, (float) new XElement (a), "a:float");
952                         Assert.AreEqual (42f, (float) new XElement (b), "b:float");
953                         Assert.AreEqual (13f, (float) new XElement (c), "c:float");
954                         Assert.AreEqual (-101f, (float) new XElement (d), "d:float");
955                         Assert.AreEqual (0f, (float) new XElement (o), "o:float");
956                         Assert.AreEqual (1f, (float) new XElement (l), "l:float");
957                         Assert.AreEqual (float.PositiveInfinity, (float) new XElement (I), "I:float");
958                         Assert.AreEqual (float.PositiveInfinity, (float) new XElement (i), "i:float");
959                         Assert.AreEqual (float.NegativeInfinity, (float) new XElement (M), "M:float");
960                         Assert.AreEqual (float.NegativeInfinity, (float) new XElement (m), "m:float");
961                         Assert.IsNaN (((float) new XElement (n)), "n:float");
962                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
963                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
964                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (c); }, "c:Guid");
965                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (d); }, "d:Guid");
966                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (o); }, "o:Guid");
967                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (l); }, "l:Guid");
968                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (I); }, "I:Guid");
969                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (i); }, "i:Guid");
970                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (M); }, "M:Guid");
971                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (m); }, "m:Guid");
972                         AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (n); }, "n:Guid");
973                         Assert.AreEqual (7, (int) new XElement (a), "a:int");
974                         Assert.AreEqual (42, (int) new XElement (b), "b:int");
975                         Assert.AreEqual (13, (int) new XElement (c), "c:int");
976                         Assert.AreEqual (-101, (int) new XElement (d), "d:int");
977                         Assert.AreEqual (0, (int) new XElement (o), "o:int");
978                         Assert.AreEqual (1, (int) new XElement (l), "l:int");
979                         AssertThrows<FormatException> (() => { int z = (int) new XElement (I); }, "I:int");
980                         AssertThrows<FormatException> (() => { int z = (int) new XElement (i); }, "i:int");
981                         AssertThrows<FormatException> (() => { int z = (int) new XElement (M); }, "M:int");
982                         AssertThrows<FormatException> (() => { int z = (int) new XElement (m); }, "m:int");
983                         AssertThrows<FormatException> (() => { int z = (int) new XElement (n); }, "n:int");
984                         Assert.AreEqual (7L, (long) new XElement (a), "a:long");
985                         Assert.AreEqual (42L, (long) new XElement (b), "b:long");
986                         Assert.AreEqual (13L, (long) new XElement (c), "c:long");
987                         Assert.AreEqual (-101L, (long) new XElement (d), "d:long");
988                         Assert.AreEqual (0L, (long) new XElement (o), "o:long");
989                         Assert.AreEqual (1L, (long) new XElement (l), "l:long");
990                         AssertThrows<FormatException> (() => { long z = (long) new XElement (I); }, "I:long");
991                         AssertThrows<FormatException> (() => { long z = (long) new XElement (i); }, "i:long");
992                         AssertThrows<FormatException> (() => { long z = (long) new XElement (M); }, "M:long");
993                         AssertThrows<FormatException> (() => { long z = (long) new XElement (m); }, "m:long");
994                         AssertThrows<FormatException> (() => { long z = (long) new XElement (n); }, "n:long");
995                         Assert.AreEqual (7u, (uint) new XElement (a), "a:uint");
996                         Assert.AreEqual (42u, (uint) new XElement (b), "b:uint");
997                         Assert.AreEqual (13u, (uint) new XElement (c), "c:uint");
998                         // LAMESPEC: see XmlConvertTests.ToUInt32().
999                         //AssertThrows<FormatException> (() => { uint z = (uint) new XElement (d); }, "d:uint");
1000                         Assert.AreEqual (0u, (uint) new XElement (o), "o:uint");
1001                         Assert.AreEqual (1u, (uint) new XElement (l), "l:uint");
1002                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (I); }, "I:uint");
1003                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (i); }, "i:uint");
1004                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (M); }, "M:uint");
1005                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (m); }, "m:uint");
1006                         AssertThrows<FormatException> (() => { uint z = (uint) new XElement (n); }, "n:uint");
1007                         Assert.AreEqual (7UL, (ulong) new XElement (a), "a:ulong");
1008                         Assert.AreEqual (42UL, (ulong) new XElement (b), "b:ulong");
1009                         Assert.AreEqual (13UL, (ulong) new XElement (c), "c:ulong");
1010                         // LAMESPEC: see XmlConvertTests.ToUInt64().
1011                         //AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (d); }, "d:ulong");
1012                         Assert.AreEqual (0UL, (ulong) new XElement (o), "o:ulong");
1013                         Assert.AreEqual (1UL, (ulong) new XElement (l), "l:ulong");
1014                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (I); }, "I:ulong");
1015                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (i); }, "i:ulong");
1016                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (M); }, "M:ulong");
1017                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (m); }, "m:ulong");
1018                         AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (n); }, "n:ulong");
1019                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
1020                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
1021                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (c); }, "c:TimeSpan");
1022                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (d); }, "d:TimeSpan");
1023                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (o); }, "o:TimeSpan");
1024                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (l); }, "l:TimeSpan");
1025                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (I); }, "I:TimeSpan");
1026                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (i); }, "i:TimeSpan");
1027                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (M); }, "M:TimeSpan");
1028                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (m); }, "m:TimeSpan");
1029                         AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (n); }, "n:TimeSpan");
1030
1031                         // Perform some round-trip tests with numbers
1032                         XElement x;
1033                         const decimal @decimal = -41051609414188012238960097189m;
1034                         const double @double = 8.5506609919892972E+307d;
1035                         const float @float = -1.70151961E+37f;
1036                         const int @int = -1051251773;
1037                         const long @long = 4596767133891939716L;
1038                         const uint @uint = 4106628142u;
1039                         const ulong @ulong = 10713797297298255927UL;
1040                         x = new XElement ("x", @decimal);
1041                         Assert.IsNotNull ((decimal?) new XElement (x), "x:decimal?:null");
1042                         Assert.AreEqual (@decimal, ((decimal?) new XElement (x)).Value, "x:decimal?:value");
1043                         Assert.AreEqual (@decimal, (decimal) new XElement (x), "x:decimal");
1044                         x = new XElement ("x", @double);
1045                         Assert.IsNotNull ((double?) new XElement (x), "x:double?:null");
1046                         Assert.AreEqual (@double, ((double?) new XElement (x)).Value, "x:double?:value");
1047                         Assert.AreEqual (@double, (double) new XElement (x), "x:double");
1048                         x = new XElement ("x", @float);
1049                         Assert.IsNotNull ((float?) new XElement (x), "x:float?:null");
1050                         Assert.AreEqual (@float, ((float?) new XElement (x)).Value, "x:float?:value");
1051                         Assert.AreEqual (@float, (float) new XElement (x), "x:float");
1052                         x = new XElement ("x", @int);
1053                         Assert.IsNotNull ((int?) new XElement (x), "x:int?:null");
1054                         Assert.AreEqual (@int, ((int?) new XElement (x)).Value, "x:int?:value");
1055                         Assert.AreEqual (@int, (int) new XElement (x), "x:int");
1056                         x = new XElement ("x", @long);
1057                         Assert.IsNotNull ((long?) new XElement (x), "x:long?:null");
1058                         Assert.AreEqual (@long, ((long?) new XElement (x)).Value, "x:long?:value");
1059                         Assert.AreEqual (@long, (long) new XElement (x), "x:long");
1060                         x = new XElement ("x", @uint);
1061                         Assert.IsNotNull ((uint?) new XElement (x), "x:uint?:null");
1062                         Assert.AreEqual (@uint, ((uint?) new XElement (x)).Value, "x:uint?:value");
1063                         Assert.AreEqual (@uint, (uint) new XElement (x), "x:uint");
1064                         x = new XElement ("x", @ulong);
1065                         Assert.IsNotNull ((ulong?) new XElement (x), "x:ulong?:null");
1066                         Assert.AreEqual (@ulong, ((ulong?) new XElement (x)).Value, "x:ulong?:value");
1067                         Assert.AreEqual (@ulong, (ulong) new XElement (x), "x:ulong");
1068                         x = new XElement ("x", double.NaN);
1069                         Assert.IsNotNull ((double?) new XElement (x), "NaN:double?:null");
1070                         Assert.AreEqual (double.NaN, ((double?) new XElement (x)).Value, "NaN:double?:value");
1071                         Assert.AreEqual (double.NaN, (double) new XElement (x), "NaN:double");
1072                         x = new XElement ("x", float.NaN);
1073                         Assert.IsNotNull ((float?) new XElement (x), "NaN:float?:null");
1074                         Assert.AreEqual (float.NaN, ((float?) new XElement (x)).Value, "NaN:float?:value");
1075                         Assert.AreEqual (float.NaN, (float) new XElement (x), "NaN:float");
1076                         x = new XElement ("x", double.PositiveInfinity);
1077                         Assert.IsNotNull ((double?) new XElement (x), "+Inf:double?:null");
1078                         Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (x)).Value, "+Inf:double?:value");
1079                         Assert.AreEqual (double.PositiveInfinity, (double) new XElement (x), "+Inf:double");
1080                         x = new XElement ("x", float.PositiveInfinity);
1081                         Assert.IsNotNull ((float?) new XElement (x), "+Inf:float?:null");
1082                         Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (x)).Value, "+Inf:float?:value");
1083                         Assert.AreEqual (float.PositiveInfinity, (float) new XElement (x), "+Inf:float");
1084                         x = new XElement ("x", double.NegativeInfinity);
1085                         Assert.IsNotNull ((double?) new XElement (x), "-Inf:double?:null");
1086                         Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (x)).Value, "-Inf:double?:value");
1087                         Assert.AreEqual (double.NegativeInfinity, (double) new XElement (x), "-Inf:double");
1088                         x = new XElement ("x", float.NegativeInfinity);
1089                         Assert.IsNotNull ((float?) new XElement (x), "-Inf:float?:null");
1090                         Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (x)).Value, "-Inf:float?:value");
1091                         Assert.AreEqual (float.NegativeInfinity, (float) new XElement (x), "-Inf:float");
1092
1093                         // Perform overflow tests with numbers
1094                         AssertThrows<OverflowException> (() => { decimal z = (decimal) new XElement ("z", "91051609414188012238960097189"); }, "z:decimal");
1095                         AssertThrows<OverflowException> (() => { decimal? z = (decimal?) new XElement ("z", "91051609414188012238960097189"); }, "z:decimal?");
1096                         AssertThrows<OverflowException> (() => { double z = (double) new XElement ("z", "8.5506609919892972E+654"); }, "z:double");
1097                         AssertThrows<OverflowException> (() => { double? z = (double?) new XElement ("z", "8.5506609919892972E+654"); }, "z:double?");
1098                         AssertThrows<OverflowException> (() => { float z = (float) new XElement ("z", @double); }, "z:float");
1099                         AssertThrows<OverflowException> (() => { float? z = (float?) new XElement ("z", @double); }, "z:float?");
1100                         AssertThrows<OverflowException> (() => { int z = (int) new XElement ("z", @long); }, "z:int");
1101                         AssertThrows<OverflowException> (() => { int? z = (int?) new XElement ("z", @long); }, "z:int?");
1102                         AssertThrows<OverflowException> (() => { long z = (long) new XElement ("z", @decimal); }, "z:long");
1103                         AssertThrows<OverflowException> (() => { long? z = (long?) new XElement ("z", @decimal); }, "z:long?");
1104                         AssertThrows<OverflowException> (() => { uint z = (uint) new XElement ("z", @ulong); }, "z:uint");
1105                         AssertThrows<OverflowException> (() => { uint? z = (uint?) new XElement ("z", @ulong); }, "z:uint?");
1106                         AssertThrows<OverflowException> (() => { ulong z = (ulong) new XElement ("z", -@decimal); }, "z:ulong");
1107                         AssertThrows<OverflowException> (() => { ulong? z = (ulong?) new XElement ("z", -@decimal); }, "z:ulong?");
1108                 }
1109
1110                 [Test]
1111                 public void CastBooleans ()
1112                 {
1113                         Assert.IsNotNull ((bool?) new XElement ("fq", "false"), "#1a");
1114                         Assert.AreEqual (false, ((bool?) new XElement ("fq", "false")).Value, "#1b");
1115                         Assert.IsNotNull ((bool?) new XElement ("tq", "true"), "#2a");
1116                         Assert.AreEqual (true, ((bool?) new XElement ("tq", "true")).Value, "#2b");
1117                         Assert.IsNotNull ((bool?) new XElement ("Fq", "False"), "#3a");
1118                         Assert.AreEqual (false, ((bool?) new XElement ("Fq", "False")).Value, "#3b");
1119                         Assert.IsNotNull ((bool?) new XElement ("Tq", "True"), "#4a");
1120                         Assert.AreEqual (true, ((bool?) new XElement ("Tq", "True")).Value, "#4b");
1121                         Assert.IsNotNull ((bool?) new XElement ("Fs", "   False \t \r "), "#5a");
1122                         Assert.AreEqual (false, ((bool?) new XElement ("Fs", "   False \t \r ")).Value, "#5b");
1123                         Assert.IsNotNull ((bool?) new XElement ("Ts", " \t True  \n  "), "#6a");
1124                         Assert.AreEqual (true, ((bool?) new XElement ("Ts", " \t True  \n  ")).Value, "#6b");
1125                         Assert.AreEqual (false, (bool) new XElement ("f", "false"), "#7");
1126                         Assert.AreEqual (true, (bool) new XElement ("t", "true"), "#8");
1127                         Assert.AreEqual (false, (bool) new XElement ("F", "False"), "#9");
1128                         Assert.AreEqual (true, (bool) new XElement ("T", "True"), "#10");
1129                         Assert.AreEqual (false, (bool)new XElement ("fs", " false  "), "#11");
1130                         Assert.AreEqual (true, (bool)new XElement ("ts", "  true "), "#12");
1131                         Assert.IsNotNull ((bool?) new XElement ("Tc", new XCData (" \t True  \n  ")), "#13a");
1132                         Assert.AreEqual (true, ((bool?) new XElement ("Tc", new XCData (" \t True  \n  "))).Value, "#13b");
1133                         Assert.AreEqual (false, (bool)new XElement ("fc", new XCData (" false  ")), "#14");
1134                         Assert.IsNotNull ((bool?) new XElement ("x", true), "#15a");
1135                         Assert.IsTrue (((bool?) new XElement ("x", true)).Value, "#15b");
1136                         Assert.IsTrue ((bool) new XElement ("x", true), "#15c");
1137                         Assert.IsNotNull ((bool?) new XElement ("x", false), "#16a");
1138                         Assert.IsFalse (((bool?) new XElement ("x", false)).Value, "#16b");
1139                         Assert.IsFalse ((bool) new XElement ("x", false), "#16c");
1140                 }
1141
1142                 [Test]
1143                 public void CastGuids ()
1144                 {
1145                         Guid rb = new Guid (new byte[16] { 0x9A, 0xBF, 0xCE, 0x7E, 0x07, 0x29, 0x9C, 0x43, 0x80, 0x7D, 0x48, 0x20, 0xB9, 0x19, 0xEA, 0x57 });
1146                         Guid rd = new Guid (new byte[16] { 0x21, 0x5B, 0x57, 0x26, 0xCD, 0x14, 0x5E, 0x44, 0x8F, 0xFA, 0xE2, 0xBC, 0x24, 0x7B, 0x2E, 0xC9 });
1147                         Guid rn = new Guid (new byte[16] { 0xF9, 0x46, 0x41, 0xA8, 0xA5, 0x03, 0xF1, 0x4A, 0xAD, 0x97, 0x7B, 0xC7, 0x79, 0x57, 0x2B, 0x79 });
1148                         Guid rp = new Guid (new byte[16] { 0x51, 0x6B, 0x8A, 0x17, 0xEF, 0x11, 0xFB, 0x48, 0x83, 0xBD, 0x57, 0xB4, 0x99, 0xF9, 0xC1, 0xE6 });
1149                         Guid rz = Guid.Empty;
1150                         Guid rx = Guid.NewGuid ();
1151
1152                         XElement b = new XElement ("b", "  {7ECEBF9A-2907-439c-807D-4820B919EA57}");
1153                         XElement d = new XElement ("d", "26575b21-14cd-445e-8ffa-e2bc247b2ec9");
1154                         XElement n = new XElement ("n", "a84146f903A54af1ad977bC779572b79\r\n");
1155                         XElement p = new XElement ("p", "  (178a6b51-11ef-48fb-83bd-57b499f9c1e6)  \t ");
1156                         XElement z = new XElement ("z", " \t \n 00000000-0000-0000-0000-000000000000 ");
1157                         XElement x = new XElement ("x", rx);
1158
1159                         Assert.IsNotNull ((Guid?) new XElement (b), "#1a");
1160                         Assert.AreEqual (rb, ((Guid?) new XElement (b)).Value, "#1b");
1161                         Assert.AreEqual (rb, (Guid) new XElement (b), "#1c");
1162                         Assert.AreEqual (rb, (Guid) new XElement ("r", rb), "#1d");
1163                         Assert.IsNotNull ((Guid?) new XElement ("r", rb), "#1e");
1164                         Assert.AreEqual (rb, ((Guid?) new XElement ("r", rb)).Value, "#1f");
1165
1166                         Assert.IsNotNull ((Guid?) new XElement (d), "#2a");
1167                         Assert.AreEqual (rd, ((Guid?) new XElement (d)).Value, "#2b");
1168                         Assert.AreEqual (rd, (Guid) new XElement (d), "#2c");
1169                         Assert.AreEqual (rd, (Guid) new XElement ("r", rd), "#2d");
1170                         Assert.IsNotNull ((Guid?) new XElement ("r", rd), "#2e");
1171                         Assert.AreEqual (rd, ((Guid?) new XElement ("r", rd)).Value, "#2f");
1172
1173                         Assert.IsNotNull ((Guid?) new XElement (n), "#3a");
1174                         Assert.AreEqual (rn, ((Guid?) new XElement (n)).Value, "#3b");
1175                         Assert.AreEqual (rn, (Guid) new XElement (n), "#3c");
1176                         Assert.AreEqual (rn, (Guid) new XElement ("r", rn), "#3d");
1177                         Assert.IsNotNull ((Guid?) new XElement ("r", rn), "#3e");
1178                         Assert.AreEqual (rn, ((Guid?) new XElement ("r", rn)).Value, "#3f");
1179
1180                         Assert.IsNotNull ((Guid?) new XElement (p), "#4a");
1181                         Assert.AreEqual (rp, ((Guid?) new XElement (p)).Value, "#4b");
1182                         Assert.AreEqual (rp, (Guid) new XElement (p), "#4c");
1183                         Assert.AreEqual (rp, (Guid) new XElement ("r", rp), "#4d");
1184                         Assert.IsNotNull ((Guid?) new XElement ("r", rp), "#4e");
1185                         Assert.AreEqual (rp, ((Guid?) new XElement ("r", rp)).Value, "#4f");
1186
1187                         Assert.IsNotNull ((Guid?) new XElement (z), "#5a");
1188                         Assert.AreEqual (rz, ((Guid?) new XElement (z)).Value, "#5b");
1189                         Assert.AreEqual (rz, (Guid) new XElement (z), "#5c");
1190
1191                         Assert.IsNotNull ((Guid?) new XElement (x), "#6a");
1192                         Assert.AreEqual (rx, ((Guid?) new XElement (x)).Value, "#6b");
1193                         Assert.AreEqual (rx, (Guid) new XElement (x), "#6c");
1194                 }
1195
1196                 [Test]
1197                 public void CastDateTimes ()
1198                 {
1199                         DateTime ra = new DateTime (1987, 1, 23, 21, 45, 36, 89, DateTimeKind.Unspecified);
1200                         DateTime rb = new DateTime (2001, 2, 3, 4, 5, 6, 789, DateTimeKind.Local);
1201                         DateTime rc = new DateTime (2010, 1, 2, 0, 0, 0, 0, DateTimeKind.Utc);
1202                         DateTime rd = new DateTime (1956, 11, 2, 0, 34, 0);
1203                         DateTime re = new DateTime (635085111683456297L, DateTimeKind.Utc);
1204                         DateTime rf = re.ToLocalTime ();
1205                         DateTime rx = DateTime.Now;
1206                         DateTime rz = DateTime.UtcNow;
1207
1208                         XElement a = new XElement ("a", "1987-01-23T21:45:36.089");
1209                         XElement b = new XElement ("b", "2001-02-03T04:05:06.789" + DateTime.Now.ToString ("zzz"));
1210                         XElement c = new XElement ("c", "2010-01-02T00:00:00Z");
1211                         XElement d = new XElement ("d", "  Nov 2, 1956  12:34 AM \r\n   \t");
1212                         XElement e = new XElement ("e", "  2013-07-04T05:06:08.3456297Z   ");  // UTC, all the way
1213                         XElement f = new XElement ("f", "  2013-07-04T05:06:08.3456297+00:00   ");  // UTC initially, but should be converted automatically to local time
1214                         XElement x = new XElement ("x", rx);
1215                         XElement z = new XElement ("z", rz);
1216
1217                         Assert.IsNotNull ((DateTime?) new XElement (a), "#1a");
1218                         Assert.AreEqual (ra, ((DateTime?) new XElement (a)).Value, "#1b");
1219                         Assert.AreEqual (ra, (DateTime) new XElement (a), "#1c");
1220                         Assert.AreEqual (ra, (DateTime) new XElement ("r", ra), "#1d");
1221                         Assert.IsNotNull ((DateTime?) new XElement ("r", ra), "#1e");
1222                         Assert.AreEqual (ra, ((DateTime?) new XElement ("r", ra)).Value, "#1f");
1223
1224                         Assert.IsNotNull ((DateTime?) new XElement (b), "#2a");
1225                         Assert.AreEqual (rb, ((DateTime?) new XElement (b)).Value, "#2b");
1226                         Assert.AreEqual (rb, (DateTime) new XElement (b), "#2c");
1227                         Assert.AreEqual (rb, (DateTime) new XElement ("r", rb), "#2d");
1228                         Assert.IsNotNull ((DateTime?) new XElement ("r", rb), "#2e");
1229                         Assert.AreEqual (rb, ((DateTime?) new XElement ("r", rb)).Value, "#2f");
1230
1231                         Assert.IsNotNull ((DateTime?) new XElement (c), "#3a");
1232                         Assert.AreEqual (rc, ((DateTime?) new XElement (c)).Value, "#3b");
1233                         Assert.AreEqual (rc, (DateTime) new XElement (c), "#3c");
1234                         Assert.AreEqual (rc, (DateTime) new XElement ("r", rc), "#3d");
1235                         Assert.IsNotNull ((DateTime?) new XElement ("r", rc), "#3e");
1236                         Assert.AreEqual (rc, ((DateTime?) new XElement ("r", rc)).Value, "#3f");
1237
1238                         Assert.IsNotNull ((DateTime?) new XElement (d), "#4a");
1239                         Assert.AreEqual (rd, ((DateTime?) new XElement (d)).Value, "#4b");
1240                         Assert.AreEqual (rd, (DateTime) new XElement (d), "#4c");
1241                         Assert.AreEqual (rd, (DateTime) new XElement ("r", rd), "#4d");
1242                         Assert.IsNotNull ((DateTime?) new XElement ("r", rd), "#4e");
1243                         Assert.AreEqual (rd, ((DateTime?) new XElement ("r", rd)).Value, "#4f");
1244
1245                         Assert.IsNotNull ((DateTime?) new XElement (x), "#5a");
1246                         Assert.AreEqual (rx, ((DateTime?) new XElement (x)).Value, "#5b");
1247                         Assert.AreEqual (rx, (DateTime) new XElement (x), "#5c");
1248
1249                         Assert.IsNotNull ((DateTime?) new XElement (z), "#6a");
1250                         Assert.AreEqual (rz, ((DateTime?) new XElement (z)).Value, "#6b");
1251                         Assert.AreEqual (rz, (DateTime) new XElement (z), "#6c");
1252
1253                         Assert.IsNotNull ((DateTime?) new XElement (e), "#7a");
1254                         Assert.AreEqual (re, ((DateTime?) new XElement (e)).Value, "#7b");
1255                         Assert.AreEqual (re, (DateTime) new XElement (e), "#7c");
1256                         Assert.AreEqual (re, (DateTime) new XElement ("r", re), "#7d");
1257                         Assert.IsNotNull ((DateTime?) new XElement ("r", re), "#7e");
1258                         Assert.AreEqual (re, ((DateTime?) new XElement ("r", re)).Value, "#7f");
1259
1260                         Assert.IsNotNull ((DateTime?) new XElement (f), "#8a");
1261                         Assert.AreEqual (rf, ((DateTime?) new XElement (f)).Value, "#8b");
1262                         Assert.AreEqual (rf, (DateTime) new XElement (f), "#8c");
1263                         Assert.AreEqual (rf, (DateTime) new XElement ("r", rf), "#8d");
1264                         Assert.IsNotNull ((DateTime?) new XElement ("r", rf), "#8e");
1265                         Assert.AreEqual (rf, ((DateTime?) new XElement ("r", rf)).Value, "#8f");
1266                 }
1267
1268                 [Test]
1269                 public void CastDateTimeOffsets ()
1270                 {
1271                         DateTimeOffset ra = new DateTimeOffset (1987, 1, 23, 21, 45, 36, 89, TimeSpan.FromHours (+13.75));  // e.g., Chatham Islands (daylight-savings time)
1272                         DateTimeOffset rb = new DateTimeOffset (2001, 2, 3, 4, 5, 6, 789, DateTimeOffset.Now.Offset);  // Local time
1273                         DateTimeOffset rc = new DateTimeOffset (2010, 1, 2, 0, 0, 0, 0, TimeSpan.Zero);  // UTC
1274                         DateTimeOffset rd = new DateTimeOffset (1956, 11, 2, 12, 34, 10, TimeSpan.FromHours (-3.5));
1275                         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
1276                         DateTimeOffset rx = DateTimeOffset.Now;
1277                         DateTimeOffset rz = DateTimeOffset.UtcNow;
1278
1279                         XElement a = new XElement ("a", "1987-01-23T21:45:36.089+13:45");
1280                         XElement b = new XElement ("b", "2001-02-03T04:05:06.789" + DateTimeOffset.Now.ToString ("zzz"));
1281                         XElement c = new XElement ("c", "2010-01-02T00:00:00Z");
1282                         XElement d = new XElement ("d", "  Nov 2, 1956  12:34:10 PM   -3:30 \r\n   \t");
1283                         XElement e = new XElement ("e", " \t   \n  1999-06-10T21:27:03.5678363-01:00 ");
1284                         XElement x = new XElement ("x", rx);
1285                         XElement z = new XElement ("z", rz);
1286
1287                         Assert.IsNotNull ((DateTimeOffset?) new XElement (a), "#1a");
1288                         Assert.AreEqual (ra, ((DateTimeOffset?) new XElement (a)).Value, "#1b");
1289                         Assert.AreEqual (ra, (DateTimeOffset) new XElement (a), "#1c");
1290                         Assert.AreEqual (ra, (DateTimeOffset) new XElement ("r", ra), "#1d");
1291                         Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", ra), "#1e");
1292                         Assert.AreEqual (ra, ((DateTimeOffset?) new XElement ("r", ra)).Value, "#1f");
1293
1294                         Assert.IsNotNull ((DateTimeOffset?) new XElement (b), "#2a");
1295                         Assert.AreEqual (rb, ((DateTimeOffset?) new XElement (b)).Value, "#2b");
1296                         Assert.AreEqual (rb, (DateTimeOffset) new XElement (b), "#2c");
1297                         Assert.AreEqual (rb, (DateTimeOffset) new XElement ("r", rb), "#2d");
1298                         Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", rb), "#2e");
1299                         Assert.AreEqual (rb, ((DateTimeOffset?) new XElement ("r", rb)).Value, "#2f");
1300
1301                         Assert.IsNotNull ((DateTimeOffset?) new XElement (c), "#3a");
1302                         Assert.AreEqual (rc, ((DateTimeOffset?) new XElement (c)).Value, "#3b");
1303                         Assert.AreEqual (rc, (DateTimeOffset) new XElement (c), "#3c");
1304                         Assert.AreEqual (rc, (DateTimeOffset) new XElement ("r", rc), "#3d");
1305                         Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", rc), "#3e");
1306                         Assert.AreEqual (rc, ((DateTimeOffset?) new XElement ("r", rc)).Value, "#3f");
1307
1308                         AssertThrows<FormatException> (() => { DateTimeOffset? r = (DateTimeOffset?) new XElement (d); }, "#4a");
1309                         AssertThrows<FormatException> (() => { DateTimeOffset r = (DateTimeOffset) new XElement (d); }, "#4b");
1310                         Assert.AreEqual (rd, DateTimeOffset.Parse (d.Value), "#4c");  // Sanity check: Okay for standalone DateTimeOffset but not as XML as in above
1311
1312                         Assert.IsNotNull ((DateTimeOffset?) new XElement (x), "#5a");
1313                         Assert.AreEqual (rx, ((DateTimeOffset?) new XElement (x)).Value, "#5b");
1314                         Assert.AreEqual (rx, (DateTimeOffset) new XElement (x), "#5c");
1315
1316                         Assert.IsNotNull ((DateTimeOffset?) new XElement (z), "#6a");
1317                         Assert.AreEqual (rz, ((DateTimeOffset?) new XElement (z)).Value, "#6b");
1318                         Assert.AreEqual (rz, (DateTimeOffset) new XElement (z), "#6c");
1319
1320                         Assert.IsNotNull ((DateTimeOffset?) new XElement (e), "#7a");
1321                         Assert.AreEqual (re, ((DateTimeOffset?) new XElement (e)).Value, "#7b");
1322                         Assert.AreEqual (re, (DateTimeOffset) new XElement (e), "#7c");
1323                         Assert.AreEqual (re, (DateTimeOffset) new XElement ("r", re), "#7d");
1324                         Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", re), "#7e");
1325                         Assert.AreEqual (re, ((DateTimeOffset?) new XElement ("r", re)).Value, "#7f");
1326                 }
1327
1328                 [Test]
1329                 public void CastTimeSpans ()
1330                 {
1331                         TimeSpan ra = new TimeSpan (23, 21, 45, 36, 89);
1332                         TimeSpan rb = -new TimeSpan (3, 4, 5, 6, 789);
1333                         TimeSpan rc = new TimeSpan (2, 0, 0, 0, 0);
1334                         TimeSpan rd = new TimeSpan (0, 0, 0, 1);
1335                         TimeSpan re = new TimeSpan (1L);  // one tick, the smallest interval
1336                         TimeSpan rx = DateTimeOffset.Now.Offset;
1337                         TimeSpan rz = TimeSpan.Zero;
1338
1339                         XElement a = new XElement ("a", "P23DT21H45M36.089S");
1340                         XElement b = new XElement ("b", "-P3DT4H5M6.789S");
1341                         XElement c = new XElement ("c", "P2D");
1342                         XElement d = new XElement ("d", "PT1S");
1343                         XElement e = new XElement ("e", "     PT0.0000001S  \t \n   ");
1344                         XElement x = new XElement ("x", rx);
1345                         XElement z = new XElement ("z", rz);
1346
1347                         Assert.IsNotNull ((TimeSpan?) new XElement (a), "#1a");
1348                         Assert.AreEqual (ra, ((TimeSpan?) new XElement (a)).Value, "#1b");
1349                         Assert.AreEqual (ra, (TimeSpan) new XElement (a), "#1c");
1350                         Assert.AreEqual (ra, (TimeSpan) new XElement ("r", ra), "#1d");
1351                         Assert.IsNotNull ((TimeSpan?) new XElement ("r", ra), "#1e");
1352                         Assert.AreEqual (ra, ((TimeSpan?) new XElement ("r", ra)).Value, "#1f");
1353
1354                         Assert.IsNotNull ((TimeSpan?) new XElement (b), "#2a");
1355                         Assert.AreEqual (rb, ((TimeSpan?) new XElement (b)).Value, "#2b");
1356                         Assert.AreEqual (rb, (TimeSpan) new XElement (b), "#2c");
1357                         Assert.AreEqual (rb, (TimeSpan) new XElement ("r", rb), "#2d");
1358                         Assert.IsNotNull ((TimeSpan?) new XElement ("r", rb), "#2e");
1359                         Assert.AreEqual (rb, ((TimeSpan?) new XElement ("r", rb)).Value, "#2f");
1360
1361                         Assert.IsNotNull ((TimeSpan?) new XElement (c), "#3a");
1362                         Assert.AreEqual (rc, ((TimeSpan?) new XElement (c)).Value, "#3b");
1363                         Assert.AreEqual (rc, (TimeSpan) new XElement (c), "#3c");
1364                         Assert.AreEqual (rc, (TimeSpan) new XElement ("r", rc), "#3d");
1365                         Assert.IsNotNull ((TimeSpan?) new XElement ("r", rc), "#3e");
1366                         Assert.AreEqual (rc, ((TimeSpan?) new XElement ("r", rc)).Value, "#3f");
1367
1368                         Assert.IsNotNull ((TimeSpan?) new XElement (d), "#4a");
1369                         Assert.AreEqual (rd, ((TimeSpan?) new XElement (d)).Value, "#4b");
1370                         Assert.AreEqual (rd, (TimeSpan) new XElement (d), "#4c");
1371                         Assert.AreEqual (rd, (TimeSpan) new XElement ("r", rd), "#4d");
1372                         Assert.IsNotNull ((TimeSpan?) new XElement ("r", rd), "#4e");
1373                         Assert.AreEqual (rd, ((TimeSpan?) new XElement ("r", rd)).Value, "#4f");
1374
1375                         Assert.IsNotNull ((TimeSpan?) new XElement (x), "#5a");
1376                         Assert.AreEqual (rx, ((TimeSpan?) new XElement (x)).Value, "#5b");
1377                         Assert.AreEqual (rx, (TimeSpan) new XElement (x), "#5c");
1378
1379                         Assert.IsNotNull ((TimeSpan?) new XElement (z), "#6a");
1380                         Assert.AreEqual (rz, ((TimeSpan?) new XElement (z)).Value, "#6b");
1381                         Assert.AreEqual (rz, (TimeSpan) new XElement (z), "#6c");
1382
1383                         Assert.IsNotNull ((TimeSpan?) new XElement (e), "#7a");
1384                         Assert.AreEqual (re, ((TimeSpan?) new XElement (e)).Value, "#7b");
1385                         Assert.AreEqual (re, (TimeSpan) new XElement (e), "#7c");
1386                         Assert.AreEqual (re, (TimeSpan) new XElement ("r", re), "#7d");
1387                         Assert.IsNotNull ((TimeSpan?) new XElement ("r", re), "#7e");
1388                         Assert.AreEqual (re, ((TimeSpan?) new XElement ("r", re)).Value, "#7f");
1389                 }
1390 #pragma warning restore 219
1391
1392                 [Test]
1393                 public void Value ()
1394                 {
1395                         // based on bug #360858
1396                         XElement a = new XElement("root",
1397                                 new XElement ("foo"),
1398                                 "Linux&Windows",
1399                                 new XComment ("comment"),
1400                                 new XElement ("bar"));
1401                         Assert.AreEqual ("Linux&Windows", a.Value);
1402                 }
1403
1404                 [Test]
1405                 [ExpectedException (typeof (ArgumentException))]
1406                 public void SetValueXAttribute ()
1407                 {
1408                         new XElement ("foo").SetValue (new XAttribute ("foo", "bar"));
1409                 }
1410
1411                 [Test]
1412                 [ExpectedException (typeof (ArgumentException))]
1413                 public void SetValueXDocumnent ()
1414                 {
1415                         new XElement ("foo").SetValue (new XDocument ());
1416                 }
1417
1418                 [Test]
1419                 // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
1420                 [ExpectedException (typeof (ArgumentException))]
1421                 [Category ("NotDotNet")]
1422                 public void SetValueXDeclaration ()
1423                 {
1424                         var el = new XElement ("foo");
1425                         el.SetValue (new XDeclaration ("1.0", null, null));
1426                         Assert.AreEqual ("<?xml version=\"1.0\"?>", el.Value);
1427                 }
1428
1429                 [Test]
1430                 [ExpectedException (typeof (ArgumentNullException))]
1431                 public void SetValueNull ()
1432                 {
1433                         new XElement ("foo", "text").SetValue (null);
1434                 }
1435
1436                 [Test]
1437                 public void AddSameInstance () // bug #392063
1438                 {
1439                         XElement root = new XElement (XName.Get ("Root", ""));
1440                         XElement child = new XElement (XName.Get ("Child", ""));
1441
1442                         root.Add (child);
1443                         root.Add (child);
1444                         root.ToString ();
1445                 }
1446
1447                 [Test]
1448                 [ExpectedException (typeof (InvalidOperationException))]
1449                 public void AddSameInstance2 ()
1450                 {
1451                         XElement root = new XElement (XName.Get ("Root"));
1452                         XAttribute attr = new XAttribute (XName.Get ("a"), "v");
1453
1454                         root.Add (attr);
1455                         root.Add (attr); // duplicate attribute
1456                         root.ToString ();
1457                 }
1458
1459                 [Test]
1460                 public void AddAttributeFromDifferentTree ()
1461                 {
1462                         XElement e1 = new XElement (XName.Get ("e1"));
1463                         XElement e2 = new XElement (XName.Get ("e2"));
1464                         XAttribute attr = new XAttribute (XName.Get ("a"), "v");
1465
1466                         e1.Add (attr);
1467                         e2.Add (attr);
1468                         Assert.AreEqual ("<e1 a=\"v\" />", e1.ToString (), "#1");
1469                         Assert.AreEqual ("<e2 a=\"v\" />", e2.ToString (), "#2");
1470                 }
1471
1472                 [Test]
1473                 public void SavePreservePrefixes ()
1474                 {
1475                         var x = XDocument.Parse (@"
1476                         <xxx:a xmlns:xxx='http://www.foobar.com'>
1477     <xxx:b>blah blah blah</xxx:b>
1478 </xxx:a>");
1479                         StringWriter sw = new StringWriter ();
1480                         x.Save (sw, SaveOptions.DisableFormatting);
1481                         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 ());
1482                 }
1483
1484                 [Test]
1485                 public void LoadFromXmlTextReader ()
1486                 {
1487                         var foo = XElement.Load (new XmlTextReader (new StringReader ("<foo></foo>")));
1488                         Assert.IsNotNull (foo);
1489                 }
1490
1491                 [Test]
1492                 public void ReplaceNodes ()
1493                 {
1494                         var inputXml = "<Foo><C><Three>3</Three><Two></Two><One/></C><B><Aaa/><Yyy/><fff/></B><A Attrib=\"Hello World\"/></Foo>";
1495                         var reader = XmlReader.Create (new StringReader (inputXml), new XmlReaderSettings ());
1496                         XDocument doc = XDocument.Load (reader);
1497                         var result = doc.Root.Elements ().OrderBy (el => el.Name.ToString());
1498                         Assert.AreEqual (3, result.Count (), "#1");
1499                         doc.Root.FirstNode.Remove ();
1500                         Assert.AreEqual (2, result.Count (), "#2");
1501
1502                         XContainer container = doc.Root;
1503                         container.ReplaceNodes (result);
1504
1505                         Assert.AreEqual (2, container.Elements ().Count (), "#3");
1506                 }
1507         }
1508 }