5 // Copyright 2007 Novell (http://www.novell.com)
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:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
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.
28 using System.Collections.Generic;
29 using System.Globalization;
33 using System.Threading;
35 using System.Xml.Linq;
36 using System.Xml.Serialization;
38 using NUnit.Framework;
40 namespace MonoTests.System.Xml.Linq
43 public class XElementTest
47 public void Constructor_NullParameters()
49 AssertThrows<ArgumentNullException>(() => new XElement((XName)null), "#1");
50 AssertThrows<ArgumentNullException>(() => new XElement((XElement)null), "#2");
51 AssertThrows<ArgumentNullException>(() => new XElement((XStreamingElement)null), "#3");
52 AssertThrows<ArgumentNullException>(() => new XElement((XName)null, null), "#4");
53 AssertThrows<ArgumentNullException>(() => new XElement((XName)null, null, null, null), "#5");
55 // This is acceptable though
56 new XElement(XName.Get("foo"), null);
59 [Test] // xml declaration is skipped.
60 public void LoadWithXmldecl ()
62 string xml = "<?xml version='1.0'?><root />";
63 XElement.Load (new StringReader (xml));
69 string xml = "<root><foo/></root>";
71 XElement el = XElement.Load (new StringReader (xml));
72 XElement first = el.FirstNode as XElement;
73 Assert.IsNotNull (first, "#1");
74 Assert.IsTrue (el.LastNode is XElement, "#2");
75 Assert.IsNull (el.NextNode, "#3");
76 Assert.IsNull (el.PreviousNode, "#4");
77 Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#5");
78 Assert.AreEqual (el, first.Parent, "#6");
79 Assert.AreEqual (first, el.LastNode, "#7");
81 Assert.AreEqual ("root", el.Name.ToString (), "#8");
82 Assert.AreEqual ("foo", first.Name.ToString (), "#9");
83 Assert.IsFalse (el.Attributes ().GetEnumerator ().MoveNext (), "#10");
87 [ExpectedException (typeof (InvalidOperationException))]
88 public void LoadInvalid ()
91 XmlReaderSettings s = new XmlReaderSettings ();
92 s.ConformanceLevel = ConformanceLevel.Fragment;
94 XElement.Load (XmlReader.Create (new StringReader (xml), s));
98 public void PrecedingWhitespaces ()
100 string xml = " <root/>";
101 XmlReaderSettings s = new XmlReaderSettings ();
102 s.ConformanceLevel = ConformanceLevel.Fragment;
104 XElement.Load (XmlReader.Create (new StringReader (xml), s));
108 public void PrecedingWhitespaces2 ()
110 string xml = " <root/>";
111 XmlReaderSettings s = new XmlReaderSettings ();
112 s.ConformanceLevel = ConformanceLevel.Fragment;
114 XmlReader r = XmlReader.Create (new StringReader (xml), s);
115 r.Read (); // at whitespace
122 bool changed = false;
123 bool changing = false;
124 var element = new XElement("foo");
125 element.Changing += (o, e) => {
126 Assert.IsFalse (changing, "#1");
127 Assert.IsFalse (changed, "#2");
128 Assert.AreSame (element, o, "#3");
129 Assert.AreEqual (XObjectChange.Name, e.ObjectChange, "#4");
133 element.Changed += (o, e) => {
134 Assert.IsTrue (changing, "#5");
135 Assert.IsFalse (changed, "#6");
136 Assert.AreSame (element, o, "#7");
137 Assert.AreEqual (XObjectChange.Name, e.ObjectChange, "#8");
141 element.Name = "bar";
142 Assert.AreEqual("bar", element.Name.LocalName, "#name");
143 Assert.IsTrue(changed, "changed");
149 string xml = "<root>foo</root>";
151 XElement el = XElement.Load (new StringReader (xml));
152 XText first = el.FirstNode as XText;
153 Assert.IsNotNull (first, "#1");
154 Assert.IsTrue (el.LastNode is XText, "#2");
155 Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#3");
156 Assert.AreEqual (el, first.Parent, "#4");
157 Assert.AreEqual (first, el.LastNode, "#5");
159 Assert.AreEqual ("foo", first.Value, "#6");
163 [ExpectedException (typeof (ArgumentException))]
164 public void AddDocumentTypeToElement ()
166 XElement el = new XElement (XName.Get ("foo"));
167 el.Add (new XDocumentType ("foo", null, null, null));
171 [ExpectedException (typeof (ArgumentException))]
172 [Category ("NotDotNet")]
173 [Ignore ("see inline comment")]
174 public void AddXDeclarationToElement ()
176 XElement el = new XElement (XName.Get ("foo"));
177 // LAMESPEC: in .NET, XDeclaration is not treated as
178 // invalid, and converted to a string without error.
179 el.Add (new XDeclaration ("1.0", null, null));
183 public void SetAttribute ()
185 XElement el = new XElement (XName.Get ("foo"));
186 el.SetAttributeValue (XName.Get ("a1"), "v1");
187 XAttribute a = el.FirstAttribute;
188 Assert.IsNotNull (a, "#1-1");
189 Assert.AreEqual (el, a.Parent, "#1-2");
190 Assert.IsNotNull (el.LastAttribute, "#1-3");
191 Assert.AreEqual (a, el.LastAttribute, "#1-4");
192 Assert.AreEqual ("a1", a.Name.LocalName, "#1-5");
193 Assert.AreEqual ("v1", a.Value, "#1-6");
194 Assert.IsNull (a.PreviousAttribute, "#1-7");
195 Assert.IsNull (a.NextAttribute, "#1-8");
197 el.SetAttributeValue (XName.Get ("a2"), "v2");
198 Assert.IsFalse (el.FirstAttribute == el.LastAttribute, "#2-1");
199 Assert.AreEqual ("a2", el.LastAttribute.Name.LocalName, "#2-2");
201 el.SetAttributeValue (XName.Get ("a1"), "v3");
202 XAttribute b = el.FirstAttribute;
203 Assert.IsNotNull (b, "#2-3");
204 Assert.IsNotNull (el.LastAttribute, "#2-4");
205 Assert.AreEqual ("a1", b.Name.LocalName, "#2-5");
206 Assert.AreEqual ("v3", b.Value, "#2-6");
207 Assert.AreEqual (a, b, "#2-7");
208 XAttribute c = el.LastAttribute;
209 Assert.AreEqual (a, c.PreviousAttribute, "#2-8");
212 Assert.IsNull (a.Parent, "#3-1");
213 Assert.IsNull (a.PreviousAttribute, "#3-2");
214 Assert.IsNull (a.NextAttribute, "#3-3");
215 Assert.IsNull (c.PreviousAttribute, "#3-4");
216 Assert.IsNull (c.NextAttribute, "#3-5");
218 el.RemoveAttributes ();
219 Assert.IsFalse (el.HasAttributes, "#4-1");
220 Assert.IsNull (b.Parent, "#4-2");
221 Assert.IsNull (c.Parent, "#4-3");
222 Assert.IsNull (el.FirstAttribute, "#4-4");
223 Assert.IsNull (el.LastAttribute, "#4-5");
227 public void AddAfterSelf ()
229 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
230 el.FirstNode.AddAfterSelf ("text");
231 XText t = el.FirstNode.NextNode as XText;
232 Assert.IsNotNull (t, "#1");
233 Assert.AreEqual ("text", t.Value, "#2");
234 XElement bar = t.NextNode as XElement;
235 Assert.IsNotNull (bar, "#3");
236 Assert.AreEqual ("bar", bar.Name.LocalName, "#4");
240 public void AddAfterSelfList ()
242 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
243 el.FirstNode.AddAfterSelf (new XText [] {
247 XText t = el.FirstNode.NextNode as XText;
248 Assert.IsNotNull (t, "#1");
249 Assert.AreEqual ("t1", t.Value, "#2");
250 Assert.AreEqual ("t2", ((XText) t.NextNode).Value, "#3");
251 Assert.AreEqual ("t3", ((XText) t.NextNode.NextNode).Value, "#4");
252 XElement bar = t.NextNode.NextNode.NextNode as XElement;
253 Assert.IsNotNull (bar, "#5");
254 Assert.AreEqual ("bar", bar.Name.LocalName, "#6");
258 [ExpectedException (typeof (ArgumentException))]
259 public void AddAfterSelfAttribute ()
261 var el = new XElement ("root", new XElement ("child"));
262 var el2 = el.FirstNode as XElement;
263 el2.AddAfterSelf (new XAttribute ("foo", "bar"));
267 [ExpectedException (typeof (ArgumentException))]
268 public void AddAfterSelfXDocument ()
270 var el = new XElement ("root", new XElement ("child"));
271 var el2 = el.FirstNode as XElement;
272 el2.AddAfterSelf (new XDocument ());
276 [ExpectedException (typeof (ArgumentException))]
277 [Category ("NotDotNet")]
278 [Ignore ("see code comment")]
279 // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
280 public void AddAfterSelfXDeclaration ()
282 var el = new XElement ("root", new XElement ("child"));
283 var el2 = el.FirstNode as XElement;
284 el2.AddAfterSelf (new XDeclaration ("1.0", null, null));
288 public void AddAfterSelfCollection ()
290 var el = new XElement ("root", new XElement ("child"));
291 el.FirstNode.AddAfterSelf (new List<XElement> (new XElement [] {new XElement ("foo"), new XElement ("bar")}));
292 Assert.AreEqual ("<root><child /><foo /><bar /></root>", el.ToString (SaveOptions.DisableFormatting), "#1");
293 Assert.AreEqual ("bar", (el.LastNode as XElement).Name.LocalName, "#2");
297 public void AddAfterSelfJoinsStringAfterText ()
299 var el = XElement.Parse ("<foo>text1</foo>");
300 el.LastNode.AddAfterSelf ("text2");
301 el.LastNode.AddAfterSelf (new XText ("text3"));
302 IEnumerator<XNode> e = el.Nodes ().GetEnumerator ();
303 Assert.IsTrue (e.MoveNext (), "#1");
304 Assert.AreEqual ("text1text2", e.Current.ToString (), "#2");
305 Assert.IsTrue (e.MoveNext (), "#3");
306 Assert.AreEqual ("text3", e.Current.ToString (), "#4");
307 Assert.IsFalse (e.MoveNext (), "#5");
311 public void AddBeforeSelf ()
313 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
314 el.FirstNode.AddBeforeSelf ("text");
315 XText t = el.FirstNode as XText;
316 Assert.IsNotNull (t, "#1");
317 Assert.AreEqual ("text", t.Value, "#2");
318 XElement foo = t.NextNode as XElement;
319 Assert.IsNotNull (foo, "#3");
320 Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
324 public void AddBeforeSelfList ()
326 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
327 el.FirstNode.AddBeforeSelf (new XText [] {
331 XText t = el.FirstNode as XText;
332 Assert.IsNotNull (t, "#1");
333 Assert.AreEqual ("t1", t.Value, "#2");
334 Assert.AreEqual ("t2", ((XText) t.NextNode).Value, "#3");
335 Assert.AreEqual ("t3", ((XText) t.NextNode.NextNode).Value, "#4");
336 XElement foo = t.NextNode.NextNode.NextNode as XElement;
337 Assert.IsNotNull (foo, "#5");
338 Assert.AreEqual ("foo", foo.Name.LocalName, "#6");
342 public void AddBeforeSelfList2 ()
344 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
345 el.FirstNode.AddBeforeSelf ("t1", "t2", "t3");
346 XText t = el.FirstNode as XText;
347 Assert.IsNotNull (t, "#1");
348 Assert.AreEqual ("t1t2t3", t.Value, "#2");
349 XElement foo = t.NextNode as XElement;
350 Assert.IsNotNull (foo, "#3");
351 Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
355 public void AddJoinsStringAfterText ()
357 var el = XElement.Parse ("<foo>text1</foo>");
359 el.Add (new XText ("text3"));
360 IEnumerator<XNode> e = el.Nodes ().GetEnumerator ();
361 Assert.IsTrue (e.MoveNext (), "#1");
362 Assert.AreEqual ("text1text2", e.Current.ToString (), "#2");
363 Assert.IsTrue (e.MoveNext (), "#3");
364 Assert.AreEqual ("text3", e.Current.ToString (), "#4");
365 Assert.IsFalse (e.MoveNext (), "#5");
369 [ExpectedException (typeof (InvalidOperationException))]
370 public void AddDuplicateAttribute ()
372 var el = new XElement ("foo",
373 new XAttribute ("bar", "baz"));
374 el.Add (new XAttribute ("bar", "baz"));
378 public void RemoveElement_FromChildNode_ChangeTriggers()
380 var childChanging = false;
381 var childChanged = false;
382 var rootChanging = false;
383 var rootChanged = false;
385 var subchild = new XElement("subfoo");
386 var child = new XElement("foo", subchild);
387 var root = new XElement("root", child);
389 child.Changing += (o, e) => {
390 Assert.IsFalse(childChanging, "#c1");
391 Assert.IsFalse(childChanged, "#c2");
392 Assert.IsFalse(rootChanging, "#c3");
393 Assert.IsFalse(rootChanged, "#c4");
394 Assert.AreSame(subchild, o, "#c5");
395 Assert.AreEqual(XObjectChange.Remove, e.ObjectChange, "#c6");
396 Assert.IsNotNull(subchild.Parent, "childChangingParent");
397 childChanging = true;
399 root.Changing += (o, e) => {
400 Assert.IsTrue(childChanging, "#r1");
401 Assert.IsFalse(childChanged, "#r2");
402 Assert.IsFalse(rootChanging, "#r3");
403 Assert.IsFalse(rootChanged, "#r4");
404 Assert.AreSame(subchild, o, "#r5");
405 Assert.AreEqual(XObjectChange.Remove, e.ObjectChange, "#r6");
406 Assert.IsNotNull(subchild.Parent, "rootChangingParent");
409 child.Changed += (o, e) => {
410 Assert.IsTrue(childChanging, "#c7");
411 Assert.IsFalse(childChanged, "#c8");
412 Assert.IsTrue(rootChanging, "#c9");
413 Assert.IsFalse(rootChanged, "#c10");
414 Assert.AreSame(subchild, o, "#c11");
415 Assert.AreEqual(XObjectChange.Remove, e.ObjectChange, "#c12");
416 Assert.IsNull(subchild.Parent, "childChangedParent");
419 root.Changed += (o, e) => {
420 Assert.IsTrue(childChanging, "#r7");
421 Assert.IsTrue(childChanged, "#r8");
422 Assert.IsTrue(rootChanging, "#r9");
423 Assert.IsFalse(rootChanged, "#r10");
424 Assert.AreSame(subchild, o, "#11");
425 Assert.AreEqual(XObjectChange.Remove, e.ObjectChange, "#12");
426 Assert.IsNull(subchild.Parent, "rootChangedParent");
431 Assert.IsTrue(childChanging, "#a");
432 Assert.IsTrue(childChanged, "#b");
433 Assert.IsTrue(rootChanging, "#c");
434 Assert.IsTrue(rootChanged, "#d");
438 public void RemoveElement_FromRootNode_ChangeTriggers()
440 var childChanging = false;
441 var childChanged = false;
442 var rootChanging = false;
443 var rootChanged = false;
445 var child = new XElement ("foo");
446 var root = new XElement ("root", child);
447 child.Changing += (o, e) => childChanging = true;
448 child.Changed += (o, e) => childChanged = true;
450 root.Changing += (o, e) => {
451 Assert.IsFalse(rootChanging, "#1");
452 Assert.IsFalse(rootChanged, "#2");
453 Assert.AreSame (child, o, "#3");
454 Assert.AreEqual(XObjectChange.Remove, e.ObjectChange, "#4");
457 root.Changed += (o, e) => {
458 Assert.IsFalse(rootChanged, "#5");
459 Assert.IsTrue(rootChanging, "#6");
460 Assert.AreSame(child, o, "#7");
461 Assert.AreEqual(XObjectChange.Remove, e.ObjectChange, "#8");
466 Assert.IsFalse(childChanging, "#9");
467 Assert.IsFalse(childChanged, "#10");
468 Assert.IsTrue(rootChanging, "#11");
469 Assert.IsTrue(rootChanged, "#12");
473 public void ReplaceWith ()
475 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
476 XNode fc = el.FirstNode;
477 fc.ReplaceWith ("test");
478 XText t = el.FirstNode as XText;
479 Assert.IsNotNull (t, "#1");
480 Assert.AreEqual ("test", t.Value, "#2");
484 public void ReplaceAll ()
486 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
487 el.ReplaceAll ("test");
488 XText t = el.FirstNode as XText;
489 Assert.IsNotNull (t, "#1");
490 Assert.AreEqual ("test", t.Value, "#2");
491 Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#3");
495 public void ReplaceAllList ()
497 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
501 new XText ("test3"));
502 XText t = el.FirstNode as XText;
503 Assert.IsNotNull (t, "#1");
504 Assert.AreEqual ("test1", t.Value, "#2");
505 t = el.LastNode as XText;
506 Assert.IsNotNull (t, "#3");
507 Assert.AreEqual ("test3", t.Value, "#4");
508 Assert.AreEqual (3, new List<XNode> (el.Nodes ()).Count, "#5");
512 public void ReplaceAttributes ()
514 XElement el = XElement.Parse ("<root x='y'><foo a='b'/></root>");
515 Assert.IsTrue (el.Attributes ().GetEnumerator ().MoveNext (), "#0");
516 el.ReplaceAttributes ("test");
517 Assert.IsTrue (el.FirstNode is XElement, "#1");
518 Assert.IsTrue (el.LastNode is XText, "#2");
519 Assert.IsFalse (el.Attributes ().GetEnumerator ().MoveNext (), "#3");
523 public void GetDefaultNamespace ()
525 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>");
526 XNamespace ns1 = XNamespace.Get ("urn:foo");
527 Assert.AreEqual (ns1, el.GetDefaultNamespace (), "#1");
528 XElement foo = (XElement) el.FirstNode;
529 Assert.AreEqual (ns1, foo.GetDefaultNamespace (), "#2");
530 Assert.AreEqual (ns1, ((XElement) foo.FirstNode).GetDefaultNamespace (), "#3");
531 XElement bar = (XElement) foo.NextNode;
532 Assert.AreEqual (ns1, bar.GetDefaultNamespace (), "#4");
533 Assert.AreEqual (ns1, ((XElement) bar.FirstNode).GetDefaultNamespace (), "#5");
534 XElement baz = (XElement) bar.NextNode;
535 Assert.AreEqual (XNamespace.Get (String.Empty), baz.GetDefaultNamespace (), "#6");
536 Assert.AreEqual (XNamespace.Get (String.Empty), ((XElement) baz.FirstNode).GetDefaultNamespace (), "#7");
540 public void GetPrefixNamespace ()
542 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>");
543 XNamespace ns1 = XNamespace.Get ("urn:foo");
544 XNamespace ns2 = XNamespace.Get ("urn:bar");
545 Assert.AreEqual (ns1, el.GetNamespaceOfPrefix ("x"), "#1-1");
546 Assert.AreEqual ("x", el.GetPrefixOfNamespace (ns1), "#1-2");
547 XElement foo = (XElement) el.FirstNode;
548 Assert.AreEqual (ns1, foo.GetNamespaceOfPrefix ("x"), "#2-1");
549 Assert.AreEqual ("x", foo.GetPrefixOfNamespace (ns1), "#2-2");
550 Assert.AreEqual (ns1, ((XElement) foo.FirstNode).GetNamespaceOfPrefix ("x"), "#3-1");
551 Assert.AreEqual ("x", ((XElement) foo.FirstNode).GetPrefixOfNamespace (ns1), "#3-2");
552 XElement bar = (XElement) foo.NextNode;
553 Assert.AreEqual (ns2, bar.GetNamespaceOfPrefix ("x"), "#4-1");
554 Assert.AreEqual ("x", bar.GetPrefixOfNamespace (ns2), "#4-2");
555 Assert.AreEqual (null, bar.GetPrefixOfNamespace (ns1), "#4-3");
556 Assert.AreEqual (ns2, ((XElement) bar.FirstNode).GetNamespaceOfPrefix ("x"), "#5-1");
557 Assert.AreEqual ("x", ((XElement) bar.FirstNode).GetPrefixOfNamespace (ns2), "#5-2");
558 Assert.AreEqual (null, ((XElement) bar.FirstNode).GetPrefixOfNamespace (ns1), "#5-3");
561 #pragma warning disable 219
563 public void CastNulls ()
565 const XElement a = null;
567 Assert.AreEqual (null, (bool?) a, "bool?");
568 Assert.AreEqual (null, (DateTime?) a, "DateTime?");
569 Assert.AreEqual (null, (DateTimeOffset?) a, "DateTimeOffset?");
570 Assert.AreEqual (null, (decimal?) a, "decimal?");
571 Assert.AreEqual (null, (double?) a, "double?");
572 Assert.AreEqual (null, (float?) a, "float?");
573 Assert.AreEqual (null, (Guid?) a, "Guid?");
574 Assert.AreEqual (null, (int?) a, "int?");
575 Assert.AreEqual (null, (long?) a, "long?");
576 Assert.AreEqual (null, (uint?) a, "uint?");
577 Assert.AreEqual (null, (ulong?) a, "ulong?");
578 Assert.AreEqual (null, (TimeSpan?) a, "TimeSpan?");
579 Assert.AreEqual (null, (string) a, "string");
580 AssertThrows<ArgumentNullException> (() => { bool z = (bool) a; }, "bool");
581 AssertThrows<ArgumentNullException> (() => { DateTime z = (DateTime) a; }, "DateTime");
582 AssertThrows<ArgumentNullException> (() => { DateTimeOffset z = (DateTimeOffset) a; }, "DateTimeOffset");
583 AssertThrows<ArgumentNullException> (() => { decimal z = (decimal) a; }, "decimal");
584 AssertThrows<ArgumentNullException> (() => { double z = (double) a; }, "double");
585 AssertThrows<ArgumentNullException> (() => { float z = (float) a; }, "float");
586 AssertThrows<ArgumentNullException> (() => { Guid z = (Guid) a; }, "Guid");
587 AssertThrows<ArgumentNullException> (() => { int z = (int) a; }, "int");
588 AssertThrows<ArgumentNullException> (() => { long z = (long) a; }, "long");
589 AssertThrows<ArgumentNullException> (() => { uint z = (uint) a; }, "uint");
590 AssertThrows<ArgumentNullException> (() => { ulong z = (ulong) a; }, "ulong");
591 AssertThrows<ArgumentNullException> (() => { TimeSpan z = (TimeSpan) a; }, "TimeSpan");
595 /// Provides functionality similar to Assert.Throws that is available on newer versions of NUnit.
597 private static T AssertThrows<T> (Action code, string message, params object[] args) where T : Exception
599 Exception actual = null;
602 } catch (Exception exception) {
605 Assert.That (actual, new NUnit.Framework.Constraints.ExactTypeConstraint (typeof (T)), message, args);
610 public void CastEmpties ()
612 XElement a = new XElement ("a");
614 // Verify expected "cloning" and "empty" behaviour as prerequisites
615 Assert.IsTrue (a.IsEmpty, "#1-1");
616 Assert.IsTrue (new XElement (a).IsEmpty, "#1-2");
617 Assert.AreEqual (String.Empty, a.Value, "#2-1");
618 Assert.AreEqual (String.Empty, new XElement (a).Value, "#2-2");
619 Assert.AreNotSame (a, new XElement (a), "#3-1");
620 Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2");
621 Assert.AreEqual ("<a />", a.ToString (), "#3-3");
622 Assert.AreEqual (a.ToString (), new XElement ("a", null).ToString (), "#3-4");
624 // Execute the primary assertions of this test
625 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "bool?");
626 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "DateTime?");
627 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "DateTimeOffset?");
628 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "decimal?");
629 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "double?");
630 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "float?");
631 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "Guid?");
632 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "int?");
633 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "long?");
634 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "uint?");
635 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "ulong?");
636 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "TimeSpan?");
637 Assert.AreEqual (String.Empty, (string) new XElement (a), "string");
638 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "bool");
639 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "DateTime");
640 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "DateTimeOffset");
641 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "decimal");
642 AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "double");
643 AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "float");
644 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "Guid");
645 AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "int");
646 AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "long");
647 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "uint");
648 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "ulong");
649 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "TimeSpan");
653 public void CastBlanks ()
655 XElement a = new XElement ("a", String.Empty);
656 XElement b = new XElement ("b", new XCData (string.Empty));
658 // Verify expected "cloning" and "blank" behaviour as prerequisites
659 Assert.IsFalse (a.IsEmpty, "#1-1a");
660 Assert.IsFalse (b.IsEmpty, "#1-1b");
661 Assert.IsFalse (new XElement (a).IsEmpty, "#1-2a");
662 Assert.IsFalse (new XElement (b).IsEmpty, "#1-2b");
663 Assert.AreEqual (String.Empty, a.Value, "#2-1a");
664 Assert.AreEqual (String.Empty, b.Value, "#2-1b");
665 Assert.AreEqual (String.Empty, new XElement (a).Value, "#2-2a");
666 Assert.AreEqual (String.Empty, new XElement (b).Value, "#2-2b");
667 Assert.AreNotSame (a, new XElement (a), "#3-1a");
668 Assert.AreNotSame (b, new XElement (b), "#3-1b");
669 Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2a");
670 Assert.AreEqual (b.ToString (), new XElement (b).ToString (), "#3-2b");
671 Assert.AreEqual ("<a></a>", a.ToString (), "#3-3a");
672 Assert.AreEqual ("<b><![CDATA[]]></b>", b.ToString (), "#3-3b");
673 Assert.AreEqual (a.ToString (), new XElement ("a", "").ToString (), "#3-4a");
674 Assert.AreEqual (b.ToString (), new XElement ("b", new XCData ("")).ToString (), "#3-4b");
676 // Execute the primary assertions of this test
677 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
678 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
679 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
680 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
681 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
682 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
683 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "a:decimal?");
684 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (b); }, "b:decimal?");
685 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "a:double?");
686 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (b); }, "b:double?");
687 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "a:float?");
688 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (b); }, "b:float?");
689 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
690 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
691 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "a:int?");
692 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (b); }, "b:int?");
693 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "a:long?");
694 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (b); }, "b:long?");
695 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "a:uint?");
696 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (b); }, "b:uint?");
697 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "a:ulong?");
698 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (b); }, "b:ulong?");
699 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
700 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
701 Assert.AreEqual (String.Empty, (string) new XElement (a), "a:string");
702 Assert.AreEqual (String.Empty, (string) new XElement (b), "b:string");
703 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
704 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
705 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
706 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
707 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
708 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
709 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "a:decimal");
710 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (b); }, "b:decimal");
711 AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "a:double");
712 AssertThrows<FormatException> (() => { double z = (double) new XElement (b); }, "b:double");
713 AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "a:float");
714 AssertThrows<FormatException> (() => { float z = (float) new XElement (b); }, "b:float");
715 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
716 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
717 AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "a:int");
718 AssertThrows<FormatException> (() => { int z = (int) new XElement (b); }, "b:int");
719 AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "a:long");
720 AssertThrows<FormatException> (() => { long z = (long) new XElement (b); }, "b:long");
721 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "a:uint");
722 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (b); }, "b:uint");
723 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "a:ulong");
724 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (b); }, "b:ulong");
725 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
726 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
730 public void CastSpaces ()
732 XElement a = new XElement ("a", " ");
733 XElement b = new XElement ("b", new XCData (" "));
735 // Verify expected "cloning" and "space" behaviour as prerequisites
736 Assert.IsFalse (a.IsEmpty, "#1-1a");
737 Assert.IsFalse (b.IsEmpty, "#1-1b");
738 Assert.IsFalse (new XElement (a).IsEmpty, "#1-2a");
739 Assert.IsFalse (new XElement (b).IsEmpty, "#1-2b");
740 Assert.AreEqual (" ", a.Value, "#2-1a");
741 Assert.AreEqual (" ", b.Value, "#2-1b");
742 Assert.AreEqual (" ", new XElement (a).Value, "#2-2a");
743 Assert.AreEqual (" ", new XElement (b).Value, "#2-2b");
744 Assert.AreNotSame (a, new XElement (a), "#3-1a");
745 Assert.AreNotSame (b, new XElement (b), "#3-1b");
746 Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2a");
747 Assert.AreEqual (b.ToString (), new XElement (b).ToString (), "#3-2b");
748 Assert.AreEqual ("<a> </a>", a.ToString (), "#3-3a");
749 Assert.AreEqual ("<b><![CDATA[ ]]></b>", b.ToString (), "#3-3b");
750 Assert.AreEqual (a.ToString (), new XElement ("a", ' ').ToString (), "#3-4");
752 // Execute the primary assertions of this test
753 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
754 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
755 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
756 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
757 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
758 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
759 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "a:decimal?");
760 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (b); }, "b:decimal?");
761 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "a:double?");
762 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (b); }, "b:double?");
763 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "a:float?");
764 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (b); }, "b:float?");
765 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
766 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
767 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "a:int?");
768 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (b); }, "b:int?");
769 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "a:long?");
770 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (b); }, "b:long?");
771 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "a:uint?");
772 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (b); }, "b:uint?");
773 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "a:ulong?");
774 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (b); }, "b:ulong?");
775 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
776 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
777 Assert.AreEqual (" ", (string) new XElement (a), "a:string");
778 Assert.AreEqual (" ", (string) new XElement (b), "b:string");
779 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
780 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
781 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
782 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
783 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
784 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
785 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "a:decimal");
786 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (b); }, "b:decimal");
787 AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "a:double");
788 AssertThrows<FormatException> (() => { double z = (double) new XElement (b); }, "b:double");
789 AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "a:float");
790 AssertThrows<FormatException> (() => { float z = (float) new XElement (b); }, "b:float");
791 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
792 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
793 AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "a:int");
794 AssertThrows<FormatException> (() => { int z = (int) new XElement (b); }, "b:int");
795 AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "a:long");
796 AssertThrows<FormatException> (() => { long z = (long) new XElement (b); }, "b:long");
797 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "a:uint");
798 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (b); }, "b:uint");
799 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "a:ulong");
800 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (b); }, "b:ulong");
801 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
802 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
806 public void CastNumbers ()
808 XElement a = new XElement ("a", "7");
809 XElement b = new XElement ("b", new XCData (" 42 "));
810 XElement c = new XElement ("c", " \r\n 13 \t ".Replace ("\r\n", Environment.NewLine));
811 XElement d = new XElement ("d", -101);
812 XElement o = new XElement ("o", "0");
813 XElement l = new XElement ("l", "1");
814 XElement I = new XElement ("I", "INF");
815 XElement i = new XElement ("i", " Infinity ");
816 XElement M = new XElement ("M", " -INF ");
817 XElement m = new XElement ("m", "-Infinity");
818 XElement n = new XElement ("n", "\t NaN ");
820 // Verify expected "cloning" and basic conversion behaviour as prerequisites
821 Assert.IsFalse (a.IsEmpty, "#1-1");
822 Assert.IsFalse (new XElement (b).IsEmpty, "#1-2");
823 Assert.AreEqual (" \r\n 13 \t ".Replace ("\r\n", Environment.NewLine), c.Value, "#2-1");
824 Assert.AreEqual ("-101", new XElement (d).Value, "#2-2");
825 Assert.AreNotSame (o, new XElement (o), "#3-1");
826 Assert.AreEqual (l.ToString (), new XElement (l).ToString (), "#3-2");
827 Assert.AreEqual ("<a>7</a>", a.ToString (), "#3-3a");
828 Assert.AreEqual ("<b><![CDATA[ 42 ]]></b>", b.ToString (), "#3-3b");
829 Assert.AreEqual ("<c> \r\n 13 \t </c>".Replace ("\r\n", Environment.NewLine), c.ToString (), "#3-3c");
830 Assert.AreEqual ("<d>-101</d>", d.ToString (), "#3-3d");
831 Assert.AreEqual ("<o>0</o>", new XElement ("o", 0.0).ToString (), "#3-3o");
832 Assert.AreEqual ("<l>1</l>", new XElement ("l", 1.0f).ToString (), "#3-3l");
833 Assert.AreEqual ("<n>NaN</n>", new XElement ("n", double.NaN).ToString (), "#3-3n");
834 Assert.AreEqual (a.ToString (), new XElement ("a", '7').ToString (), "#3-4a");
835 Assert.AreEqual (d.ToString (), new XElement ("d", "-101").ToString (), "#3-4d");
836 Assert.AreEqual (o.ToString (), new XElement ("o", 0L).ToString (), "#3-4o");
837 Assert.AreEqual (l.ToString (), new XElement ("l", 1m).ToString (), "#3-4l");
839 // Execute the primary assertions of this test
840 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
841 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
842 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (c); }, "c:bool?");
843 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (d); }, "d:bool?");
844 Assert.IsNotNull ((bool?) new XElement (o), "o:bool?:null");
845 Assert.AreEqual (false, ((bool?) new XElement (o)).Value, "o:bool?:value");
846 Assert.IsNotNull ((bool?) new XElement (l), "l:bool?:null");
847 Assert.AreEqual (true, ((bool?) new XElement (l)).Value, "l:bool?:value");
848 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (I); }, "I:bool?");
849 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (i); }, "i:bool?");
850 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (M); }, "M:bool?");
851 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (m); }, "m:bool?");
852 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (n); }, "n:bool?");
853 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
854 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
855 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (c); }, "c:DateTime?");
856 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (d); }, "d:DateTime?");
857 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (o); }, "o:DateTime?");
858 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (l); }, "l:DateTime?");
859 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (I); }, "I:DateTime?");
860 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (i); }, "i:DateTime?");
861 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (M); }, "M:DateTime?");
862 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (m); }, "m:DateTime?");
863 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (n); }, "n:DateTime?");
864 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
865 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
866 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (c); }, "c:DateTimeOffset?");
867 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (d); }, "d:DateTimeOffset?");
868 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (o); }, "o:DateTimeOffset?");
869 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (l); }, "l:DateTimeOffset?");
870 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (I); }, "I:DateTimeOffset?");
871 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (i); }, "i:DateTimeOffset?");
872 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (M); }, "M:DateTimeOffset?");
873 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (m); }, "m:DateTimeOffset?");
874 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (n); }, "n:DateTimeOffset?");
875 Assert.IsNotNull ((decimal?) new XElement (a), "a:decimal?:null");
876 Assert.AreEqual (7m, ((decimal?) new XElement (a)).Value, "a:decimal?:value");
877 Assert.IsNotNull ((decimal?) new XElement (b), "b:decimal?:null");
878 Assert.AreEqual (42m, ((decimal?) new XElement (b)).Value, "b:decimal?:value");
879 Assert.IsNotNull ((decimal?) new XElement (c), "c:decimal?:null");
880 Assert.AreEqual (13m, ((decimal?) new XElement (c)).Value, "c:decimal?:value");
881 Assert.IsNotNull ((decimal?) new XElement (d), "d:decimal?:null");
882 Assert.AreEqual (-101m, ((decimal?) new XElement (d)).Value, "d:decimal?:value");
883 Assert.IsNotNull ((decimal?) new XElement (o), "o:decimal?:null");
884 Assert.AreEqual (0m, ((decimal?) new XElement (o)).Value, "o:decimal?:value");
885 Assert.IsNotNull ((decimal?) new XElement (l), "l:decimal?:null");
886 Assert.AreEqual (1m, ((decimal?) new XElement (l)).Value, "l:decimal?:value");
887 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (I); }, "I:decimal?");
888 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (i); }, "i:decimal?");
889 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (M); }, "M:decimal?");
890 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (m); }, "m:decimal?");
891 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (n); }, "n:decimal?");
892 Assert.IsNotNull ((double?) new XElement (a), "a:double?:null");
893 Assert.AreEqual (7d, ((double?) new XElement (a)).Value, "a:double?:value");
894 Assert.IsNotNull ((double?) new XElement (b), "b:double?:null");
895 Assert.AreEqual (42d, ((double?) new XElement (b)).Value, "b:double?:value");
896 Assert.IsNotNull ((double?) new XElement (c), "c:double?:null");
897 Assert.AreEqual (13d, ((double?) new XElement (c)).Value, "c:double?:value");
898 Assert.IsNotNull ((double?) new XElement (d), "d:double?:null");
899 Assert.AreEqual (-101d, ((double?) new XElement (d)).Value, "d:double?:value");
900 Assert.IsNotNull ((double?) new XElement (o), "o:double?:null");
901 Assert.AreEqual (0d, ((double?) new XElement (o)).Value, "o:double?:value");
902 Assert.IsNotNull ((double?) new XElement (l), "l:double?:null");
903 Assert.AreEqual (1d, ((double?) new XElement (l)).Value, "l:double?:value");
904 Assert.IsNotNull ((double?) new XElement (I), "I:double?:null");
905 Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (I)).Value, "I:double?:value");
906 Assert.IsNotNull ((double?) new XElement (i), "i:double?:null");
907 Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (i)).Value, "i:double?:value");
908 Assert.IsNotNull ((double?) new XElement (M), "M:double?:null");
909 Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (M)).Value, "M:double?:value");
910 Assert.IsNotNull ((double?) new XElement (m), "m:double?:null");
911 Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (m)).Value, "m:double?:value");
912 Assert.IsNotNull ((double?) new XElement (n), "n:double?:null");
913 Assert.AreEqual (double.NaN, ((double?) new XElement (n)).Value, "n:double?:value");
914 Assert.IsNotNull ((float?) new XElement (a), "a:float?:null");
915 Assert.AreEqual (7f, ((float?) new XElement (a)).Value, "a:float?:value");
916 Assert.IsNotNull ((float?) new XElement (b), "b:float?:null");
917 Assert.AreEqual (42f, ((float?) new XElement (b)).Value, "b:float?:value");
918 Assert.IsNotNull ((float?) new XElement (c), "c:float?:null");
919 Assert.AreEqual (13f, ((float?) new XElement (c)).Value, "c:float?:value");
920 Assert.IsNotNull ((float?) new XElement (d), "d:float?:null");
921 Assert.AreEqual (-101f, ((float?) new XElement (d)).Value, "d:float?:value");
922 Assert.IsNotNull ((float?) new XElement (o), "o:float?:null");
923 Assert.AreEqual (0f, ((float?) new XElement (o)).Value, "o:float?:value");
924 Assert.IsNotNull ((float?) new XElement (l), "l:float?:null");
925 Assert.AreEqual (1f, ((float?) new XElement (l)).Value, "l:float?:value");
926 Assert.IsNotNull ((float?) new XElement (I), "I:float?:null");
927 Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (I)).Value, "I:float?:value");
928 Assert.IsNotNull ((float?) new XElement (i), "i:float?:null");
929 Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (i)).Value, "i:float?:value");
930 Assert.IsNotNull ((float?) new XElement (M), "M:float?:null");
931 Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (M)).Value, "M:float?:value");
932 Assert.IsNotNull ((float?) new XElement (m), "m:float?:null");
933 Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (m)).Value, "m:float?:value");
934 Assert.IsNotNull ((float?) new XElement (n), "n:float?:null");
935 Assert.AreEqual (float.NaN, ((float?) new XElement (n)).Value, "n:float?:value");
936 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
937 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
938 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (c); }, "c:Guid?");
939 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (d); }, "d:Guid?");
940 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (o); }, "o:Guid?");
941 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (l); }, "l:Guid?");
942 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (I); }, "I:Guid?");
943 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (i); }, "i:Guid?");
944 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (M); }, "M:Guid?");
945 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (m); }, "m:Guid?");
946 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (n); }, "n:Guid?");
947 Assert.IsNotNull ((int?) new XElement (a), "a:int?:null");
948 Assert.AreEqual (7, ((int?) new XElement (a)).Value, "a:int?:value");
949 Assert.IsNotNull ((int?) new XElement (b), "b:int?:null");
950 Assert.AreEqual (42, ((int?) new XElement (b)).Value, "b:int?:value");
951 Assert.IsNotNull ((int?) new XElement (c), "c:int?:null");
952 Assert.AreEqual (13, ((int?) new XElement (c)).Value, "c:int?:value");
953 Assert.IsNotNull ((int?) new XElement (d), "d:int?:null");
954 Assert.AreEqual (-101, ((int?) new XElement (d)).Value, "d:int?:value");
955 Assert.IsNotNull ((int?) new XElement (o), "o:int?:null");
956 Assert.AreEqual (0, ((int?) new XElement (o)).Value, "o:int?:value");
957 Assert.IsNotNull ((int?) new XElement (l), "l:int?:null");
958 Assert.AreEqual (1, ((int?) new XElement (l)).Value, "l:int?:value");
959 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (I); }, "I:int?");
960 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (i); }, "i:int?");
961 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (M); }, "M:int?");
962 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (m); }, "m:int?");
963 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (n); }, "n:int?");
964 Assert.IsNotNull ((long?) new XElement (a), "a:long?:null");
965 Assert.AreEqual (7L, ((long?) new XElement (a)).Value, "a:long?:value");
966 Assert.IsNotNull ((long?) new XElement (b), "b:long?:null");
967 Assert.AreEqual (42L, ((long?) new XElement (b)).Value, "b:long?:value");
968 Assert.IsNotNull ((long?) new XElement (c), "c:long?:null");
969 Assert.AreEqual (13L, ((long?) new XElement (c)).Value, "c:long?:value");
970 Assert.IsNotNull ((long?) new XElement (d), "d:long?:null");
971 Assert.AreEqual (-101L, ((long?) new XElement (d)).Value, "d:long?:value");
972 Assert.IsNotNull ((long?) new XElement (o), "o:long?:null");
973 Assert.AreEqual (0L, ((long?) new XElement (o)).Value, "o:long?:value");
974 Assert.IsNotNull ((long?) new XElement (l), "l:long?:null");
975 Assert.AreEqual (1L, ((long?) new XElement (l)).Value, "l:long?:value");
976 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (I); }, "I:long?");
977 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (i); }, "i:long?");
978 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (M); }, "M:long?");
979 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (m); }, "m:long?");
980 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (n); }, "n:long?");
981 Assert.IsNotNull ((uint?) new XElement (a), "a:uint?:null");
982 Assert.AreEqual (7u, ((uint?) new XElement (a)).Value, "a:uint?:value");
983 Assert.IsNotNull ((uint?) new XElement (b), "b:uint?:null");
984 Assert.AreEqual (42u, ((uint?) new XElement (b)).Value, "b:uint?:value");
985 Assert.IsNotNull ((uint?) new XElement (c), "c:uint?:null");
986 Assert.AreEqual (13u, ((uint?) new XElement (c)).Value, "c:uint?:value");
987 // LAMESPEC: see XmlConvertTests.ToUInt32().
988 //AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (d); }, "d:uint?");
989 Assert.IsNotNull ((uint?) new XElement (o), "o:uint?:null");
990 Assert.AreEqual (0u, ((uint?) new XElement (o)).Value, "o:uint?:value");
991 Assert.IsNotNull ((uint?) new XElement (l), "l:uint?:null");
992 Assert.AreEqual (1u, ((uint?) new XElement (l)).Value, "l:uint?:value");
993 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (I); }, "I:uint?");
994 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (i); }, "i:uint?");
995 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (M); }, "M:uint?");
996 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (m); }, "m:uint?");
997 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (n); }, "n:uint?");
998 Assert.IsNotNull ((ulong?) new XElement (a), "a:ulong?:null");
999 Assert.AreEqual (7UL, ((ulong?) new XElement (a)).Value, "a:ulong?:value");
1000 Assert.IsNotNull ((ulong?) new XElement (b), "b:ulong?:null");
1001 Assert.AreEqual (42UL, ((ulong?) new XElement (b)).Value, "b:ulong?:value");
1002 Assert.IsNotNull ((ulong?) new XElement (c), "c:ulong?:null");
1003 Assert.AreEqual (13UL, ((ulong?) new XElement (c)).Value, "c:ulong?:value");
1004 // LAMESPEC: see XmlConvertTests.ToUInt64().
1005 //AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (d); }, "d:ulong?");
1006 Assert.IsNotNull ((ulong?) new XElement (o), "o:ulong?:null");
1007 Assert.AreEqual (0UL, ((ulong?) new XElement (o)).Value, "o:ulong?:value");
1008 Assert.IsNotNull ((ulong?) new XElement (l), "l:ulong?:null");
1009 Assert.AreEqual (1UL, ((ulong?) new XElement (l)).Value, "l:ulong?:value");
1010 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (I); }, "I:ulong?");
1011 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (i); }, "i:ulong?");
1012 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (M); }, "M:ulong?");
1013 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (m); }, "m:ulong?");
1014 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (n); }, "n:ulong?");
1015 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
1016 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
1017 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (c); }, "c:TimeSpan?");
1018 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (d); }, "d:TimeSpan?");
1019 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (o); }, "o:TimeSpan?");
1020 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (l); }, "l:TimeSpan?");
1021 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (I); }, "I:TimeSpan?");
1022 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (i); }, "i:TimeSpan?");
1023 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (M); }, "M:TimeSpan?");
1024 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (m); }, "m:TimeSpan?");
1025 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (n); }, "n:TimeSpan?");
1026 Assert.AreEqual ("7", (string) new XElement (a), "a:string");
1027 Assert.AreEqual (" 42 ", (string) new XElement (b), "b:string");
1028 Assert.AreEqual (" \r\n 13 \t ".Replace ("\r\n", Environment.NewLine), (string) new XElement (c), "c:string");
1029 Assert.AreEqual ("-101", (string) new XElement (d), "d:string");
1030 Assert.AreEqual ("0", (string) new XElement (o), "o:string");
1031 Assert.AreEqual ("1", (string) new XElement (l), "l:string");
1032 Assert.AreEqual ("INF", (string) new XElement (I), "I:string");
1033 Assert.AreEqual (" Infinity ", (string) new XElement (i), "i:string");
1034 Assert.AreEqual (" -INF ", (string) new XElement (M), "M:string");
1035 Assert.AreEqual ("-Infinity", (string) new XElement (m), "m:string");
1036 Assert.AreEqual ("\t NaN ", (string) new XElement (n), "n:string");
1037 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
1038 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
1039 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (c); }, "c:bool");
1040 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (d); }, "d:bool");
1041 Assert.AreEqual (false, (bool) new XElement (o), "o:bool");
1042 Assert.AreEqual (true, (bool) new XElement (l), "l:bool");
1043 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (I); }, "I:bool");
1044 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (i); }, "i:bool");
1045 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (M); }, "M:bool");
1046 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (m); }, "m:bool");
1047 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (n); }, "n:bool");
1048 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
1049 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
1050 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (c); }, "c:DateTime");
1051 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (d); }, "d:DateTime");
1052 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (o); }, "o:DateTime");
1053 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (l); }, "l:DateTime");
1054 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (I); }, "I:DateTime");
1055 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (i); }, "i:DateTime");
1056 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (M); }, "M:DateTime");
1057 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (m); }, "m:DateTime");
1058 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (n); }, "n:DateTime");
1059 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
1060 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
1061 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (c); }, "c:DateTimeOffset");
1062 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (d); }, "d:DateTimeOffset");
1063 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (o); }, "o:DateTimeOffset");
1064 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (l); }, "l:DateTimeOffset");
1065 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (I); }, "I:DateTimeOffset");
1066 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (i); }, "i:DateTimeOffset");
1067 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (M); }, "M:DateTimeOffset");
1068 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (m); }, "m:DateTimeOffset");
1069 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (n); }, "n:DateTimeOffset");
1070 Assert.AreEqual (7m, (decimal) new XElement (a), "a:decimal");
1071 Assert.AreEqual (42m, (decimal) new XElement (b), "b:decimal");
1072 Assert.AreEqual (13m, (decimal) new XElement (c), "c:decimal");
1073 Assert.AreEqual (-101m, (decimal) new XElement (d), "d:decimal");
1074 Assert.AreEqual (0m, (decimal) new XElement (o), "o:decimal");
1075 Assert.AreEqual (1m, (decimal) new XElement (l), "l:decimal");
1076 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (I); }, "I:decimal");
1077 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (i); }, "i:decimal");
1078 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (M); }, "M:decimal");
1079 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (m); }, "m:decimal");
1080 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (n); }, "n:decimal");
1081 Assert.AreEqual (7d, (double) new XElement (a), "a:double");
1082 Assert.AreEqual (42d, (double) new XElement (b), "b:double");
1083 Assert.AreEqual (13d, (double) new XElement (c), "c:double");
1084 Assert.AreEqual (-101d, (double) new XElement (d), "d:double");
1085 Assert.AreEqual (0d, (double) new XElement (o), "o:double");
1086 Assert.AreEqual (1d, (double) new XElement (l), "l:double");
1087 Assert.AreEqual (double.PositiveInfinity, (double) new XElement (I), "I:double");
1088 Assert.AreEqual (double.PositiveInfinity, (double) new XElement (i), "i:double");
1089 Assert.AreEqual (double.NegativeInfinity, (double) new XElement (M), "M:double");
1090 Assert.AreEqual (double.NegativeInfinity, (double) new XElement (m), "m:double");
1091 Assert.AreEqual (double.NaN, ((double) new XElement (n)), "n:double");
1092 Assert.AreEqual (7f, (float) new XElement (a), "a:float");
1093 Assert.AreEqual (42f, (float) new XElement (b), "b:float");
1094 Assert.AreEqual (13f, (float) new XElement (c), "c:float");
1095 Assert.AreEqual (-101f, (float) new XElement (d), "d:float");
1096 Assert.AreEqual (0f, (float) new XElement (o), "o:float");
1097 Assert.AreEqual (1f, (float) new XElement (l), "l:float");
1098 Assert.AreEqual (float.PositiveInfinity, (float) new XElement (I), "I:float");
1099 Assert.AreEqual (float.PositiveInfinity, (float) new XElement (i), "i:float");
1100 Assert.AreEqual (float.NegativeInfinity, (float) new XElement (M), "M:float");
1101 Assert.AreEqual (float.NegativeInfinity, (float) new XElement (m), "m:float");
1102 Assert.AreEqual (float.NaN, ((float) new XElement (n)), "n:float");
1103 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
1104 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
1105 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (c); }, "c:Guid");
1106 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (d); }, "d:Guid");
1107 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (o); }, "o:Guid");
1108 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (l); }, "l:Guid");
1109 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (I); }, "I:Guid");
1110 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (i); }, "i:Guid");
1111 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (M); }, "M:Guid");
1112 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (m); }, "m:Guid");
1113 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (n); }, "n:Guid");
1114 Assert.AreEqual (7, (int) new XElement (a), "a:int");
1115 Assert.AreEqual (42, (int) new XElement (b), "b:int");
1116 Assert.AreEqual (13, (int) new XElement (c), "c:int");
1117 Assert.AreEqual (-101, (int) new XElement (d), "d:int");
1118 Assert.AreEqual (0, (int) new XElement (o), "o:int");
1119 Assert.AreEqual (1, (int) new XElement (l), "l:int");
1120 AssertThrows<FormatException> (() => { int z = (int) new XElement (I); }, "I:int");
1121 AssertThrows<FormatException> (() => { int z = (int) new XElement (i); }, "i:int");
1122 AssertThrows<FormatException> (() => { int z = (int) new XElement (M); }, "M:int");
1123 AssertThrows<FormatException> (() => { int z = (int) new XElement (m); }, "m:int");
1124 AssertThrows<FormatException> (() => { int z = (int) new XElement (n); }, "n:int");
1125 Assert.AreEqual (7L, (long) new XElement (a), "a:long");
1126 Assert.AreEqual (42L, (long) new XElement (b), "b:long");
1127 Assert.AreEqual (13L, (long) new XElement (c), "c:long");
1128 Assert.AreEqual (-101L, (long) new XElement (d), "d:long");
1129 Assert.AreEqual (0L, (long) new XElement (o), "o:long");
1130 Assert.AreEqual (1L, (long) new XElement (l), "l:long");
1131 AssertThrows<FormatException> (() => { long z = (long) new XElement (I); }, "I:long");
1132 AssertThrows<FormatException> (() => { long z = (long) new XElement (i); }, "i:long");
1133 AssertThrows<FormatException> (() => { long z = (long) new XElement (M); }, "M:long");
1134 AssertThrows<FormatException> (() => { long z = (long) new XElement (m); }, "m:long");
1135 AssertThrows<FormatException> (() => { long z = (long) new XElement (n); }, "n:long");
1136 Assert.AreEqual (7u, (uint) new XElement (a), "a:uint");
1137 Assert.AreEqual (42u, (uint) new XElement (b), "b:uint");
1138 Assert.AreEqual (13u, (uint) new XElement (c), "c:uint");
1139 // LAMESPEC: see XmlConvertTests.ToUInt32().
1140 //AssertThrows<FormatException> (() => { uint z = (uint) new XElement (d); }, "d:uint");
1141 Assert.AreEqual (0u, (uint) new XElement (o), "o:uint");
1142 Assert.AreEqual (1u, (uint) new XElement (l), "l:uint");
1143 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (I); }, "I:uint");
1144 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (i); }, "i:uint");
1145 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (M); }, "M:uint");
1146 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (m); }, "m:uint");
1147 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (n); }, "n:uint");
1148 Assert.AreEqual (7UL, (ulong) new XElement (a), "a:ulong");
1149 Assert.AreEqual (42UL, (ulong) new XElement (b), "b:ulong");
1150 Assert.AreEqual (13UL, (ulong) new XElement (c), "c:ulong");
1151 // LAMESPEC: see XmlConvertTests.ToUInt64().
1152 //AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (d); }, "d:ulong");
1153 Assert.AreEqual (0UL, (ulong) new XElement (o), "o:ulong");
1154 Assert.AreEqual (1UL, (ulong) new XElement (l), "l:ulong");
1155 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (I); }, "I:ulong");
1156 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (i); }, "i:ulong");
1157 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (M); }, "M:ulong");
1158 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (m); }, "m:ulong");
1159 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (n); }, "n:ulong");
1160 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
1161 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
1162 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (c); }, "c:TimeSpan");
1163 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (d); }, "d:TimeSpan");
1164 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (o); }, "o:TimeSpan");
1165 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (l); }, "l:TimeSpan");
1166 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (I); }, "I:TimeSpan");
1167 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (i); }, "i:TimeSpan");
1168 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (M); }, "M:TimeSpan");
1169 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (m); }, "m:TimeSpan");
1170 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (n); }, "n:TimeSpan");
1172 // Perform some round-trip tests with numbers
1174 const decimal @decimal = -41051609414188012238960097189m;
1175 const double @double = 8.5506609919892972E+307d;
1176 const float @float = -1.70151961E+37f;
1177 const int @int = -1051251773;
1178 const long @long = 4596767133891939716L;
1179 const uint @uint = 4106628142u;
1180 const ulong @ulong = 10713797297298255927UL;
1181 x = new XElement ("x", @decimal);
1182 Assert.IsNotNull ((decimal?) new XElement (x), "x:decimal?:null");
1183 Assert.AreEqual (@decimal, ((decimal?) new XElement (x)).Value, "x:decimal?:value");
1184 Assert.AreEqual (@decimal, (decimal) new XElement (x), "x:decimal");
1185 x = new XElement ("x", @double);
1186 Assert.IsNotNull ((double?) new XElement (x), "x:double?:null");
1187 Assert.AreEqual (@double, ((double?) new XElement (x)).Value, "x:double?:value");
1188 Assert.AreEqual (@double, (double) new XElement (x), "x:double");
1189 x = new XElement ("x", @float);
1190 Assert.IsNotNull ((float?) new XElement (x), "x:float?:null");
1191 Assert.AreEqual (@float, ((float?) new XElement (x)).Value, "x:float?:value");
1192 Assert.AreEqual (@float, (float) new XElement (x), "x:float");
1193 x = new XElement ("x", @int);
1194 Assert.IsNotNull ((int?) new XElement (x), "x:int?:null");
1195 Assert.AreEqual (@int, ((int?) new XElement (x)).Value, "x:int?:value");
1196 Assert.AreEqual (@int, (int) new XElement (x), "x:int");
1197 x = new XElement ("x", @long);
1198 Assert.IsNotNull ((long?) new XElement (x), "x:long?:null");
1199 Assert.AreEqual (@long, ((long?) new XElement (x)).Value, "x:long?:value");
1200 Assert.AreEqual (@long, (long) new XElement (x), "x:long");
1201 x = new XElement ("x", @uint);
1202 Assert.IsNotNull ((uint?) new XElement (x), "x:uint?:null");
1203 Assert.AreEqual (@uint, ((uint?) new XElement (x)).Value, "x:uint?:value");
1204 Assert.AreEqual (@uint, (uint) new XElement (x), "x:uint");
1205 x = new XElement ("x", @ulong);
1206 Assert.IsNotNull ((ulong?) new XElement (x), "x:ulong?:null");
1207 Assert.AreEqual (@ulong, ((ulong?) new XElement (x)).Value, "x:ulong?:value");
1208 Assert.AreEqual (@ulong, (ulong) new XElement (x), "x:ulong");
1209 x = new XElement ("x", double.NaN);
1210 Assert.IsNotNull ((double?) new XElement (x), "NaN:double?:null");
1211 Assert.AreEqual (double.NaN, ((double?) new XElement (x)).Value, "NaN:double?:value");
1212 Assert.AreEqual (double.NaN, (double) new XElement (x), "NaN:double");
1213 x = new XElement ("x", float.NaN);
1214 Assert.IsNotNull ((float?) new XElement (x), "NaN:float?:null");
1215 Assert.AreEqual (float.NaN, ((float?) new XElement (x)).Value, "NaN:float?:value");
1216 Assert.AreEqual (float.NaN, (float) new XElement (x), "NaN:float");
1217 x = new XElement ("x", double.PositiveInfinity);
1218 Assert.IsNotNull ((double?) new XElement (x), "+Inf:double?:null");
1219 Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (x)).Value, "+Inf:double?:value");
1220 Assert.AreEqual (double.PositiveInfinity, (double) new XElement (x), "+Inf:double");
1221 x = new XElement ("x", float.PositiveInfinity);
1222 Assert.IsNotNull ((float?) new XElement (x), "+Inf:float?:null");
1223 Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (x)).Value, "+Inf:float?:value");
1224 Assert.AreEqual (float.PositiveInfinity, (float) new XElement (x), "+Inf:float");
1225 x = new XElement ("x", double.NegativeInfinity);
1226 Assert.IsNotNull ((double?) new XElement (x), "-Inf:double?:null");
1227 Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (x)).Value, "-Inf:double?:value");
1228 Assert.AreEqual (double.NegativeInfinity, (double) new XElement (x), "-Inf:double");
1229 x = new XElement ("x", float.NegativeInfinity);
1230 Assert.IsNotNull ((float?) new XElement (x), "-Inf:float?:null");
1231 Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (x)).Value, "-Inf:float?:value");
1232 Assert.AreEqual (float.NegativeInfinity, (float) new XElement (x), "-Inf:float");
1234 // Perform overflow tests with numbers
1235 AssertThrows<OverflowException> (() => { decimal z = (decimal) new XElement ("z", "91051609414188012238960097189"); }, "z:decimal");
1236 AssertThrows<OverflowException> (() => { decimal? z = (decimal?) new XElement ("z", "91051609414188012238960097189"); }, "z:decimal?");
1237 AssertThrows<OverflowException> (() => { double z = (double) new XElement ("z", "8.5506609919892972E+654"); }, "z:double");
1238 AssertThrows<OverflowException> (() => { double? z = (double?) new XElement ("z", "8.5506609919892972E+654"); }, "z:double?");
1239 AssertThrows<OverflowException> (() => { float z = (float) new XElement ("z", @double); }, "z:float");
1240 AssertThrows<OverflowException> (() => { float? z = (float?) new XElement ("z", @double); }, "z:float?");
1241 AssertThrows<OverflowException> (() => { int z = (int) new XElement ("z", @long); }, "z:int");
1242 AssertThrows<OverflowException> (() => { int? z = (int?) new XElement ("z", @long); }, "z:int?");
1243 AssertThrows<OverflowException> (() => { long z = (long) new XElement ("z", @decimal); }, "z:long");
1244 AssertThrows<OverflowException> (() => { long? z = (long?) new XElement ("z", @decimal); }, "z:long?");
1245 AssertThrows<OverflowException> (() => { uint z = (uint) new XElement ("z", @ulong); }, "z:uint");
1246 AssertThrows<OverflowException> (() => { uint? z = (uint?) new XElement ("z", @ulong); }, "z:uint?");
1247 AssertThrows<OverflowException> (() => { ulong z = (ulong) new XElement ("z", -@decimal); }, "z:ulong");
1248 AssertThrows<OverflowException> (() => { ulong? z = (ulong?) new XElement ("z", -@decimal); }, "z:ulong?");
1252 public void CastExtremes ()
1254 // Test extremes/constants where round-trips should work in specific ways
1255 Assert.AreEqual (decimal.MaxValue, (decimal) new XElement ("k", decimal.MaxValue), "MaxValue:decimal");
1256 Assert.AreEqual (decimal.MinValue, (decimal) new XElement ("k", decimal.MinValue), "MinValue:decimal");
1257 Assert.AreEqual (decimal.MinusOne, (decimal) new XElement ("k", decimal.MinusOne), "MinusOne:decimal");
1258 Assert.AreEqual (decimal.One, (decimal) new XElement ("k", decimal.One), "One:decimal");
1259 Assert.AreEqual (decimal.Zero, (decimal) new XElement ("k", decimal.Zero), "Zero:decimal");
1260 Assert.AreEqual (double.MaxValue, (double) new XElement ("k", double.MaxValue), "MaxValue:double");
1261 Assert.AreEqual (double.MinValue, (double) new XElement ("k", double.MinValue), "MinValue:double");
1262 Assert.AreEqual (double.Epsilon, (double) new XElement ("k", double.Epsilon), "Epsilon:double");
1263 Assert.AreEqual (double.NaN, (double) new XElement ("k", double.NaN), "NaN:double");
1264 Assert.AreEqual (double.NegativeInfinity, (double) new XElement ("k", double.NegativeInfinity), "-Inf:double");
1265 Assert.AreEqual (double.PositiveInfinity, (double) new XElement ("k", double.PositiveInfinity), "+Inf:double");
1266 Assert.AreEqual (float.MaxValue, (float) new XElement ("k", float.MaxValue), "MaxValue:float");
1267 Assert.AreEqual (float.MinValue, (float) new XElement ("k", float.MinValue), "MinValue:float");
1268 Assert.AreEqual (float.Epsilon, (float) new XElement ("k", float.Epsilon), "Epsilon:float");
1269 Assert.AreEqual (float.NaN, (float) new XElement ("k", float.NaN), "NaN:float");
1270 Assert.AreEqual (float.NegativeInfinity, (float) new XElement ("k", float.NegativeInfinity), "-Inf:float");
1271 Assert.AreEqual (float.PositiveInfinity, (float) new XElement ("k", float.PositiveInfinity), "+Inf:float");
1272 Assert.AreEqual (int.MaxValue, (int) new XElement ("k", int.MaxValue), "MaxValue:int");
1273 Assert.AreEqual (int.MinValue, (int) new XElement ("k", int.MinValue), "MinValue:int");
1274 Assert.AreEqual (long.MaxValue, (long) new XElement ("k", long.MaxValue), "MaxValue:long");
1275 Assert.AreEqual (long.MinValue, (long) new XElement ("k", long.MinValue), "MinValue:long");
1276 Assert.AreEqual (uint.MaxValue, (uint) new XElement ("k", uint.MaxValue), "MaxValue:uint");
1277 Assert.AreEqual (uint.MinValue, (uint) new XElement ("k", uint.MinValue), "MinValue:uint");
1278 Assert.AreEqual (ulong.MaxValue, (ulong) new XElement ("k", ulong.MaxValue), "MaxValue:ulong");
1279 Assert.AreEqual (ulong.MinValue, (ulong) new XElement ("k", ulong.MinValue), "MinValue:ulong");
1280 Assert.AreEqual (decimal.MaxValue, (decimal?) new XElement ("k", decimal.MaxValue), "MaxValue:decimal?");
1281 Assert.AreEqual (decimal.MinValue, (decimal?) new XElement ("k", decimal.MinValue), "MinValue:decimal?");
1282 Assert.AreEqual (decimal.MinusOne, (decimal?) new XElement ("k", decimal.MinusOne), "MinusOne:decimal?");
1283 Assert.AreEqual (decimal.One, (decimal?) new XElement ("k", decimal.One), "One:decimal?");
1284 Assert.AreEqual (decimal.Zero, (decimal?) new XElement ("k", decimal.Zero), "Zero:decimal?");
1285 Assert.AreEqual (double.MaxValue, (double?) new XElement ("k", double.MaxValue), "MaxValue:double?");
1286 Assert.AreEqual (double.MinValue, (double?) new XElement ("k", double.MinValue), "MinValue:double?");
1287 Assert.AreEqual (double.Epsilon, (double?) new XElement ("k", double.Epsilon), "Epsilon:double?");
1288 Assert.AreEqual (double.NaN, (double?) new XElement ("k", double.NaN), "NaN:double?");
1289 Assert.AreEqual (double.NegativeInfinity, (double?) new XElement ("k", double.NegativeInfinity), "-Inf:double?");
1290 Assert.AreEqual (double.PositiveInfinity, (double?) new XElement ("k", double.PositiveInfinity), "+Inf:double?");
1291 Assert.AreEqual (float.MaxValue, (float?) new XElement ("k", float.MaxValue), "MaxValue:float?");
1292 Assert.AreEqual (float.MinValue, (float?) new XElement ("k", float.MinValue), "MinValue:float?");
1293 Assert.AreEqual (float.Epsilon, (float?) new XElement ("k", float.Epsilon), "Epsilon:float?");
1294 Assert.AreEqual (float.NaN, (float?) new XElement ("k", float.NaN), "NaN:float?");
1295 Assert.AreEqual (float.NegativeInfinity, (float?) new XElement ("k", float.NegativeInfinity), "-Inf:float?");
1296 Assert.AreEqual (float.PositiveInfinity, (float?) new XElement ("k", float.PositiveInfinity), "+Inf:float?");
1297 Assert.AreEqual (int.MaxValue, (int?) new XElement ("k", int.MaxValue), "MaxValue:int?");
1298 Assert.AreEqual (int.MinValue, (int?) new XElement ("k", int.MinValue), "MinValue:int?");
1299 Assert.AreEqual (long.MaxValue, (long?) new XElement ("k", long.MaxValue), "MaxValue:long?");
1300 Assert.AreEqual (long.MinValue, (long?) new XElement ("k", long.MinValue), "MinValue:long?");
1301 Assert.AreEqual (uint.MaxValue, (uint?) new XElement ("k", uint.MaxValue), "MaxValue:uint?");
1302 Assert.AreEqual (uint.MinValue, (uint?) new XElement ("k", uint.MinValue), "MinValue:uint?");
1303 Assert.AreEqual (ulong.MaxValue, (ulong?) new XElement ("k", ulong.MaxValue), "MaxValue:ulong?");
1304 Assert.AreEqual (ulong.MinValue, (ulong?) new XElement ("k", ulong.MinValue), "MinValue:ulong?");
1305 Assert.AreEqual (DateTime.MaxValue, (DateTime) new XElement ("k", DateTime.MaxValue), "MaxValue:DateTime");
1306 Assert.AreEqual (DateTime.MinValue, (DateTime) new XElement ("k", DateTime.MinValue), "MinValue:DateTime");
1307 Assert.AreEqual (DateTime.MaxValue, (DateTime?) new XElement ("k", DateTime.MaxValue), "MaxValue:DateTime?");
1308 Assert.AreEqual (DateTime.MinValue, (DateTime?) new XElement ("k", DateTime.MinValue), "MinValue:DateTime?");
1309 Assert.AreEqual (DateTimeOffset.MaxValue, (DateTimeOffset) new XElement ("k", DateTimeOffset.MaxValue), "MaxValue:DateTimeOffset");
1310 Assert.AreEqual (DateTimeOffset.MinValue, (DateTimeOffset) new XElement ("k", DateTimeOffset.MinValue), "MinValue:DateTimeOffset");
1311 Assert.AreEqual (DateTimeOffset.MaxValue, (DateTimeOffset?) new XElement ("k", DateTimeOffset.MaxValue), "MaxValue:DateTimeOffset?");
1312 Assert.AreEqual (DateTimeOffset.MinValue, (DateTimeOffset?) new XElement ("k", DateTimeOffset.MinValue), "MinValue:DateTimeOffset?");
1313 Assert.AreEqual (TimeSpan.MaxValue, (TimeSpan) new XElement ("k", TimeSpan.MaxValue), "MaxValue:TimeSpan");
1314 Assert.AreEqual (TimeSpan.MinValue, (TimeSpan) new XElement ("k", TimeSpan.MinValue), "MinValue:TimeSpan");
1315 Assert.AreEqual (TimeSpan.MaxValue, (TimeSpan?) new XElement ("k", TimeSpan.MaxValue), "MaxValue:TimeSpan?");
1316 Assert.AreEqual (TimeSpan.MinValue, (TimeSpan?) new XElement ("k", TimeSpan.MinValue), "MinValue:TimeSpan?");
1320 public void CastBooleans ()
1322 Assert.IsNotNull ((bool?) new XElement ("fq", "false"), "#1a");
1323 Assert.AreEqual (false, ((bool?) new XElement ("fq", "false")).Value, "#1b");
1324 Assert.IsNotNull ((bool?) new XElement ("tq", "true"), "#2a");
1325 Assert.AreEqual (true, ((bool?) new XElement ("tq", "true")).Value, "#2b");
1326 Assert.IsNotNull ((bool?) new XElement ("Fq", "False"), "#3a");
1327 Assert.AreEqual (false, ((bool?) new XElement ("Fq", "False")).Value, "#3b");
1328 Assert.IsNotNull ((bool?) new XElement ("Tq", "True"), "#4a");
1329 Assert.AreEqual (true, ((bool?) new XElement ("Tq", "True")).Value, "#4b");
1330 Assert.IsNotNull ((bool?) new XElement ("Fs", " False \t \r "), "#5a");
1331 Assert.AreEqual (false, ((bool?) new XElement ("Fs", " False \t \r ")).Value, "#5b");
1332 Assert.IsNotNull ((bool?) new XElement ("Ts", " \t True \n "), "#6a");
1333 Assert.AreEqual (true, ((bool?) new XElement ("Ts", " \t True \n ")).Value, "#6b");
1334 Assert.AreEqual (false, (bool) new XElement ("f", "false"), "#7");
1335 Assert.AreEqual (true, (bool) new XElement ("t", "true"), "#8");
1336 Assert.AreEqual (false, (bool) new XElement ("F", "False"), "#9");
1337 Assert.AreEqual (true, (bool) new XElement ("T", "True"), "#10");
1338 Assert.AreEqual (false, (bool)new XElement ("fs", " false "), "#11");
1339 Assert.AreEqual (true, (bool)new XElement ("ts", " true "), "#12");
1340 Assert.IsNotNull ((bool?) new XElement ("Tc", new XCData (" \t True \n ")), "#13a");
1341 Assert.AreEqual (true, ((bool?) new XElement ("Tc", new XCData (" \t True \n "))).Value, "#13b");
1342 Assert.AreEqual (false, (bool)new XElement ("fc", new XCData (" false ")), "#14");
1343 Assert.IsNotNull ((bool?) new XElement ("x", true), "#15a");
1344 Assert.IsTrue (((bool?) new XElement ("x", true)).Value, "#15b");
1345 Assert.IsTrue ((bool) new XElement ("x", true), "#15c");
1346 Assert.IsNotNull ((bool?) new XElement ("x", false), "#16a");
1347 Assert.IsFalse (((bool?) new XElement ("x", false)).Value, "#16b");
1348 Assert.IsFalse ((bool) new XElement ("x", false), "#16c");
1349 Assert.IsTrue ((bool) new XElement ("x", bool.TrueString), "#17a");
1350 Assert.IsFalse ((bool) new XElement ("x", bool.FalseString), "#17b");
1351 Assert.IsTrue ((bool) new XElement ("x", new XCData (bool.TrueString)), "#18a");
1352 Assert.IsFalse ((bool) new XElement ("x", new XCData (bool.FalseString)), "#18b");
1356 public void CastGuids ()
1358 Guid rb = new Guid (new byte[16] { 0x9A, 0xBF, 0xCE, 0x7E, 0x07, 0x29, 0x9C, 0x43, 0x80, 0x7D, 0x48, 0x20, 0xB9, 0x19, 0xEA, 0x57 });
1359 Guid rd = new Guid (new byte[16] { 0x21, 0x5B, 0x57, 0x26, 0xCD, 0x14, 0x5E, 0x44, 0x8F, 0xFA, 0xE2, 0xBC, 0x24, 0x7B, 0x2E, 0xC9 });
1360 Guid rn = new Guid (new byte[16] { 0xF9, 0x46, 0x41, 0xA8, 0xA5, 0x03, 0xF1, 0x4A, 0xAD, 0x97, 0x7B, 0xC7, 0x79, 0x57, 0x2B, 0x79 });
1361 Guid rp = new Guid (new byte[16] { 0x51, 0x6B, 0x8A, 0x17, 0xEF, 0x11, 0xFB, 0x48, 0x83, 0xBD, 0x57, 0xB4, 0x99, 0xF9, 0xC1, 0xE6 });
1362 Guid rz = Guid.Empty;
1363 Guid rx = Guid.NewGuid ();
1365 XElement b = new XElement ("b", " {7ECEBF9A-2907-439c-807D-4820B919EA57}");
1366 XElement d = new XElement ("d", "26575b21-14cd-445e-8ffa-e2bc247b2ec9");
1367 XElement n = new XElement ("n", "a84146f903A54af1ad977bC779572b79\r\n");
1368 XElement p = new XElement ("p", " (178a6b51-11ef-48fb-83bd-57b499f9c1e6) \t ");
1369 XElement z = new XElement ("z", " \t \n 00000000-0000-0000-0000-000000000000 ");
1370 XElement x = new XElement ("x", rx);
1372 Assert.IsNotNull ((Guid?) new XElement (b), "#1a");
1373 Assert.AreEqual (rb, ((Guid?) new XElement (b)).Value, "#1b");
1374 Assert.AreEqual (rb, (Guid) new XElement (b), "#1c");
1375 Assert.AreEqual (rb, (Guid) new XElement ("r", rb), "#1d");
1376 Assert.IsNotNull ((Guid?) new XElement ("r", rb), "#1e");
1377 Assert.AreEqual (rb, ((Guid?) new XElement ("r", rb)).Value, "#1f");
1379 Assert.IsNotNull ((Guid?) new XElement (d), "#2a");
1380 Assert.AreEqual (rd, ((Guid?) new XElement (d)).Value, "#2b");
1381 Assert.AreEqual (rd, (Guid) new XElement (d), "#2c");
1382 Assert.AreEqual (rd, (Guid) new XElement ("r", rd), "#2d");
1383 Assert.IsNotNull ((Guid?) new XElement ("r", rd), "#2e");
1384 Assert.AreEqual (rd, ((Guid?) new XElement ("r", rd)).Value, "#2f");
1386 Assert.IsNotNull ((Guid?) new XElement (n), "#3a");
1387 Assert.AreEqual (rn, ((Guid?) new XElement (n)).Value, "#3b");
1388 Assert.AreEqual (rn, (Guid) new XElement (n), "#3c");
1389 Assert.AreEqual (rn, (Guid) new XElement ("r", rn), "#3d");
1390 Assert.IsNotNull ((Guid?) new XElement ("r", rn), "#3e");
1391 Assert.AreEqual (rn, ((Guid?) new XElement ("r", rn)).Value, "#3f");
1393 Assert.IsNotNull ((Guid?) new XElement (p), "#4a");
1394 Assert.AreEqual (rp, ((Guid?) new XElement (p)).Value, "#4b");
1395 Assert.AreEqual (rp, (Guid) new XElement (p), "#4c");
1396 Assert.AreEqual (rp, (Guid) new XElement ("r", rp), "#4d");
1397 Assert.IsNotNull ((Guid?) new XElement ("r", rp), "#4e");
1398 Assert.AreEqual (rp, ((Guid?) new XElement ("r", rp)).Value, "#4f");
1400 Assert.IsNotNull ((Guid?) new XElement (z), "#5a");
1401 Assert.AreEqual (rz, ((Guid?) new XElement (z)).Value, "#5b");
1402 Assert.AreEqual (rz, (Guid) new XElement (z), "#5c");
1404 Assert.IsNotNull ((Guid?) new XElement (x), "#6a");
1405 Assert.AreEqual (rx, ((Guid?) new XElement (x)).Value, "#6b");
1406 Assert.AreEqual (rx, (Guid) new XElement (x), "#6c");
1410 public void CastDateTimes ()
1412 DateTime ra = new DateTime (1987, 1, 23, 21, 45, 36, 89, DateTimeKind.Unspecified);
1413 DateTime rb = new DateTime (2001, 2, 3, 4, 5, 6, 789, DateTimeKind.Local);
1414 DateTime rc = new DateTime (2010, 1, 2, 0, 0, 0, 0, DateTimeKind.Utc);
1415 DateTime rd = new DateTime (1956, 11, 2, 0, 34, 0);
1416 DateTime re = new DateTime (635085111683456297L, DateTimeKind.Utc);
1417 DateTime rf = re.ToLocalTime ();
1418 DateTime rx = DateTime.Now;
1419 DateTime rz = DateTime.UtcNow;
1421 XElement a = new XElement ("a", "1987-01-23T21:45:36.089");
1422 XElement b = new XElement ("b", "2001-02-03T04:05:06.789" + rb.ToString ("zzz"));
1423 XElement c = new XElement ("c", "2010-01-02T00:00:00Z");
1424 XElement d = new XElement ("d", " Nov 2, 1956 12:34 AM \r\n \t");
1425 XElement e = new XElement ("e", " 2013-07-04T05:06:08.3456297Z "); // UTC, all the way
1426 XElement f = new XElement ("f", " 2013-07-04T05:06:08.3456297+00:00 "); // UTC initially, but should be converted automatically to local time
1427 XElement x = new XElement ("x", rx);
1428 XElement z = new XElement ("z", rz);
1430 Assert.IsNotNull ((DateTime?) new XElement (a), "#1a");
1431 Assert.AreEqual (ra, ((DateTime?) new XElement (a)).Value, "#1b");
1432 Assert.AreEqual (ra, (DateTime) new XElement (a), "#1c");
1433 Assert.AreEqual (ra, (DateTime) new XElement ("r", ra), "#1d");
1434 Assert.IsNotNull ((DateTime?) new XElement ("r", ra), "#1e");
1435 Assert.AreEqual (ra, ((DateTime?) new XElement ("r", ra)).Value, "#1f");
1437 Assert.IsNotNull ((DateTime?) new XElement (b), "#2a");
1438 Assert.AreEqual (rb, ((DateTime?) new XElement (b)).Value, "#2b");
1439 Assert.AreEqual (rb, (DateTime) new XElement (b), "#2c");
1440 Assert.AreEqual (rb, (DateTime) new XElement ("r", rb), "#2d");
1441 Assert.IsNotNull ((DateTime?) new XElement ("r", rb), "#2e");
1442 Assert.AreEqual (rb, ((DateTime?) new XElement ("r", rb)).Value, "#2f");
1444 Assert.IsNotNull ((DateTime?) new XElement (c), "#3a");
1445 Assert.AreEqual (rc, ((DateTime?) new XElement (c)).Value, "#3b");
1446 Assert.AreEqual (rc, (DateTime) new XElement (c), "#3c");
1447 Assert.AreEqual (rc, (DateTime) new XElement ("r", rc), "#3d");
1448 Assert.IsNotNull ((DateTime?) new XElement ("r", rc), "#3e");
1449 Assert.AreEqual (rc, ((DateTime?) new XElement ("r", rc)).Value, "#3f");
1451 Assert.IsNotNull ((DateTime?) new XElement (d), "#4a");
1452 Assert.AreEqual (rd, ((DateTime?) new XElement (d)).Value, "#4b");
1453 Assert.AreEqual (rd, (DateTime) new XElement (d), "#4c");
1454 Assert.AreEqual (rd, (DateTime) new XElement ("r", rd), "#4d");
1455 Assert.IsNotNull ((DateTime?) new XElement ("r", rd), "#4e");
1456 Assert.AreEqual (rd, ((DateTime?) new XElement ("r", rd)).Value, "#4f");
1458 Assert.IsNotNull ((DateTime?) new XElement (x), "#5a");
1459 Assert.AreEqual (rx, ((DateTime?) new XElement (x)).Value, "#5b");
1460 Assert.AreEqual (rx, (DateTime) new XElement (x), "#5c");
1462 Assert.IsNotNull ((DateTime?) new XElement (z), "#6a");
1463 Assert.AreEqual (rz, ((DateTime?) new XElement (z)).Value, "#6b");
1464 Assert.AreEqual (rz, (DateTime) new XElement (z), "#6c");
1466 Assert.IsNotNull ((DateTime?) new XElement (e), "#7a");
1467 Assert.AreEqual (re, ((DateTime?) new XElement (e)).Value, "#7b");
1468 Assert.AreEqual (re, (DateTime) new XElement (e), "#7c");
1469 Assert.AreEqual (re, (DateTime) new XElement ("r", re), "#7d");
1470 Assert.IsNotNull ((DateTime?) new XElement ("r", re), "#7e");
1471 Assert.AreEqual (re, ((DateTime?) new XElement ("r", re)).Value, "#7f");
1473 Assert.IsNotNull ((DateTime?) new XElement (f), "#8a");
1474 Assert.AreEqual (rf, ((DateTime?) new XElement (f)).Value, "#8b");
1475 Assert.AreEqual (rf, (DateTime) new XElement (f), "#8c");
1476 Assert.AreEqual (rf, (DateTime) new XElement ("r", rf), "#8d");
1477 Assert.IsNotNull ((DateTime?) new XElement ("r", rf), "#8e");
1478 Assert.AreEqual (rf, ((DateTime?) new XElement ("r", rf)).Value, "#8f");
1482 public void CastDateTimeOffsets ()
1484 DateTimeOffset ra = new DateTimeOffset (1987, 1, 23, 21, 45, 36, 89, TimeSpan.FromHours (+13.75)); // e.g., Chatham Islands (daylight-savings time)
1485 DateTimeOffset rb = new DateTimeOffset (2001, 2, 3, 4, 5, 6, 789, DateTimeOffset.Now.Offset); // Local time
1486 DateTimeOffset rc = new DateTimeOffset (2010, 1, 2, 0, 0, 0, 0, TimeSpan.Zero); // UTC
1487 DateTimeOffset rd = new DateTimeOffset (1956, 11, 2, 12, 34, 10, TimeSpan.FromHours (-3.5));
1488 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
1489 DateTimeOffset rx = DateTimeOffset.Now;
1490 DateTimeOffset rz = DateTimeOffset.UtcNow;
1492 XElement a = new XElement ("a", "1987-01-23T21:45:36.089+13:45");
1493 XElement b = new XElement ("b", "2001-02-03T04:05:06.789" + DateTimeOffset.Now.ToString ("zzz"));
1494 XElement c = new XElement ("c", "2010-01-02T00:00:00Z");
1495 XElement d = new XElement ("d", " Nov 2, 1956 12:34:10 PM -3:30 \r\n \t");
1496 XElement e = new XElement ("e", " \t \n 1999-06-10T21:27:03.5678363-01:00 ");
1497 XElement x = new XElement ("x", rx);
1498 XElement z = new XElement ("z", rz);
1500 Assert.IsNotNull ((DateTimeOffset?) new XElement (a), "#1a");
1501 Assert.AreEqual (ra, ((DateTimeOffset?) new XElement (a)).Value, "#1b");
1502 Assert.AreEqual (ra, (DateTimeOffset) new XElement (a), "#1c");
1503 Assert.AreEqual (ra, (DateTimeOffset) new XElement ("r", ra), "#1d");
1504 Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", ra), "#1e");
1505 Assert.AreEqual (ra, ((DateTimeOffset?) new XElement ("r", ra)).Value, "#1f");
1507 Assert.IsNotNull ((DateTimeOffset?) new XElement (b), "#2a");
1508 Assert.AreEqual (rb, ((DateTimeOffset?) new XElement (b)).Value, "#2b");
1509 Assert.AreEqual (rb, (DateTimeOffset) new XElement (b), "#2c");
1510 Assert.AreEqual (rb, (DateTimeOffset) new XElement ("r", rb), "#2d");
1511 Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", rb), "#2e");
1512 Assert.AreEqual (rb, ((DateTimeOffset?) new XElement ("r", rb)).Value, "#2f");
1514 Assert.IsNotNull ((DateTimeOffset?) new XElement (c), "#3a");
1515 Assert.AreEqual (rc, ((DateTimeOffset?) new XElement (c)).Value, "#3b");
1516 Assert.AreEqual (rc, (DateTimeOffset) new XElement (c), "#3c");
1517 Assert.AreEqual (rc, (DateTimeOffset) new XElement ("r", rc), "#3d");
1518 Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", rc), "#3e");
1519 Assert.AreEqual (rc, ((DateTimeOffset?) new XElement ("r", rc)).Value, "#3f");
1521 AssertThrows<FormatException> (() => { DateTimeOffset? r = (DateTimeOffset?) new XElement (d); }, "#4a");
1522 AssertThrows<FormatException> (() => { DateTimeOffset r = (DateTimeOffset) new XElement (d); }, "#4b");
1523 Assert.AreEqual (rd, DateTimeOffset.Parse (d.Value), "#4c"); // Sanity check: Okay for standalone DateTimeOffset but not as XML as in above
1525 Assert.IsNotNull ((DateTimeOffset?) new XElement (x), "#5a");
1526 Assert.AreEqual (rx, ((DateTimeOffset?) new XElement (x)).Value, "#5b");
1527 Assert.AreEqual (rx, (DateTimeOffset) new XElement (x), "#5c");
1529 Assert.IsNotNull ((DateTimeOffset?) new XElement (z), "#6a");
1530 Assert.AreEqual (rz, ((DateTimeOffset?) new XElement (z)).Value, "#6b");
1531 Assert.AreEqual (rz, (DateTimeOffset) new XElement (z), "#6c");
1533 Assert.IsNotNull ((DateTimeOffset?) new XElement (e), "#7a");
1534 Assert.AreEqual (re, ((DateTimeOffset?) new XElement (e)).Value, "#7b");
1535 Assert.AreEqual (re, (DateTimeOffset) new XElement (e), "#7c");
1536 Assert.AreEqual (re, (DateTimeOffset) new XElement ("r", re), "#7d");
1537 Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", re), "#7e");
1538 Assert.AreEqual (re, ((DateTimeOffset?) new XElement ("r", re)).Value, "#7f");
1542 public void CastTimeSpans ()
1544 TimeSpan ra = new TimeSpan (23, 21, 45, 36, 89);
1545 TimeSpan rb = -new TimeSpan (3, 4, 5, 6, 789);
1546 TimeSpan rc = new TimeSpan (2, 0, 0, 0, 0);
1547 TimeSpan rd = new TimeSpan (0, 0, 0, 1);
1548 TimeSpan re = new TimeSpan (1L); // one tick, the smallest interval
1549 TimeSpan rx = DateTimeOffset.Now.Offset;
1550 TimeSpan rz = TimeSpan.Zero;
1552 XElement a = new XElement ("a", "P23DT21H45M36.089S");
1553 XElement b = new XElement ("b", "-P3DT4H5M6.789S");
1554 XElement c = new XElement ("c", "P2D");
1555 XElement d = new XElement ("d", "PT1S");
1556 XElement e = new XElement ("e", " PT0.0000001S \t \n ");
1557 XElement x = new XElement ("x", rx);
1558 XElement z = new XElement ("z", rz);
1560 Assert.IsNotNull ((TimeSpan?) new XElement (a), "#1a");
1561 Assert.AreEqual (ra, ((TimeSpan?) new XElement (a)).Value, "#1b");
1562 Assert.AreEqual (ra, (TimeSpan) new XElement (a), "#1c");
1563 Assert.AreEqual (ra, (TimeSpan) new XElement ("r", ra), "#1d");
1564 Assert.IsNotNull ((TimeSpan?) new XElement ("r", ra), "#1e");
1565 Assert.AreEqual (ra, ((TimeSpan?) new XElement ("r", ra)).Value, "#1f");
1567 Assert.IsNotNull ((TimeSpan?) new XElement (b), "#2a");
1568 Assert.AreEqual (rb, ((TimeSpan?) new XElement (b)).Value, "#2b");
1569 Assert.AreEqual (rb, (TimeSpan) new XElement (b), "#2c");
1570 Assert.AreEqual (rb, (TimeSpan) new XElement ("r", rb), "#2d");
1571 Assert.IsNotNull ((TimeSpan?) new XElement ("r", rb), "#2e");
1572 Assert.AreEqual (rb, ((TimeSpan?) new XElement ("r", rb)).Value, "#2f");
1574 Assert.IsNotNull ((TimeSpan?) new XElement (c), "#3a");
1575 Assert.AreEqual (rc, ((TimeSpan?) new XElement (c)).Value, "#3b");
1576 Assert.AreEqual (rc, (TimeSpan) new XElement (c), "#3c");
1577 Assert.AreEqual (rc, (TimeSpan) new XElement ("r", rc), "#3d");
1578 Assert.IsNotNull ((TimeSpan?) new XElement ("r", rc), "#3e");
1579 Assert.AreEqual (rc, ((TimeSpan?) new XElement ("r", rc)).Value, "#3f");
1581 Assert.IsNotNull ((TimeSpan?) new XElement (d), "#4a");
1582 Assert.AreEqual (rd, ((TimeSpan?) new XElement (d)).Value, "#4b");
1583 Assert.AreEqual (rd, (TimeSpan) new XElement (d), "#4c");
1584 Assert.AreEqual (rd, (TimeSpan) new XElement ("r", rd), "#4d");
1585 Assert.IsNotNull ((TimeSpan?) new XElement ("r", rd), "#4e");
1586 Assert.AreEqual (rd, ((TimeSpan?) new XElement ("r", rd)).Value, "#4f");
1588 Assert.IsNotNull ((TimeSpan?) new XElement (x), "#5a");
1589 Assert.AreEqual (rx, ((TimeSpan?) new XElement (x)).Value, "#5b");
1590 Assert.AreEqual (rx, (TimeSpan) new XElement (x), "#5c");
1592 Assert.IsNotNull ((TimeSpan?) new XElement (z), "#6a");
1593 Assert.AreEqual (rz, ((TimeSpan?) new XElement (z)).Value, "#6b");
1594 Assert.AreEqual (rz, (TimeSpan) new XElement (z), "#6c");
1596 Assert.IsNotNull ((TimeSpan?) new XElement (e), "#7a");
1597 Assert.AreEqual (re, ((TimeSpan?) new XElement (e)).Value, "#7b");
1598 Assert.AreEqual (re, (TimeSpan) new XElement (e), "#7c");
1599 Assert.AreEqual (re, (TimeSpan) new XElement ("r", re), "#7d");
1600 Assert.IsNotNull ((TimeSpan?) new XElement ("r", re), "#7e");
1601 Assert.AreEqual (re, ((TimeSpan?) new XElement ("r", re)).Value, "#7f");
1603 #pragma warning restore 219
1606 public void Value ()
1608 // based on bug #360858
1609 XElement a = new XElement("root",
1610 new XElement ("foo"),
1612 new XComment ("comment"),
1613 new XElement ("bar"));
1614 Assert.AreEqual ("Linux&Windows", a.Value);
1618 [ExpectedException (typeof (ArgumentException))]
1619 public void SetValueXAttribute ()
1621 new XElement ("foo").SetValue (new XAttribute ("foo", "bar"));
1625 [ExpectedException (typeof (ArgumentException))]
1626 public void SetValueXDocumnent ()
1628 new XElement ("foo").SetValue (new XDocument ());
1632 public void SetValue_ChangeTriggers()
1634 bool changed = false;
1635 bool changing = false;
1637 var element = new XElement("foo");
1638 element.Changing += (o, e) => {
1639 Assert.IsFalse(changing, "#1");
1640 Assert.IsFalse(changed, "#2");
1641 Assert.IsTrue (o is XText, "#3");
1642 Assert.AreEqual("bar", ((XText)o).Value, "#4");
1643 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#5");
1646 element.Changed += (o, e) => {
1647 Assert.IsTrue(changing, "#5");
1648 Assert.IsFalse(changed, "#6");
1649 Assert.IsTrue (o is XText, "#7");
1650 Assert.AreEqual("bar", ((XText)o).Value, "#8");
1651 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#9");
1655 element.SetValue("bar");
1656 Assert.IsTrue(changed, "#changed");
1660 // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
1661 [ExpectedException (typeof (ArgumentException))]
1662 [Category ("NotDotNet")]
1663 [Ignore ("see code comment")]
1664 public void SetValueXDeclaration ()
1666 var el = new XElement ("foo");
1667 el.SetValue (new XDeclaration ("1.0", null, null));
1668 Assert.AreEqual ("<?xml version=\"1.0\"?>", el.Value);
1672 [ExpectedException (typeof (ArgumentNullException))]
1673 public void SetValueNull ()
1675 new XElement ("foo", "text").SetValue (null);
1679 public void AddSameInstance () // bug #392063
1681 XElement root = new XElement (XName.Get ("Root", ""));
1682 XElement child = new XElement (XName.Get ("Child", ""));
1686 Assert.AreEqual(2, root.Elements().Count(), "#1");
1688 Assert.AreEqual(1, root.Elements().Count(), "#2");
1689 AssertThrows<InvalidOperationException>(() => child.Remove(), "#3");
1693 [ExpectedException (typeof (InvalidOperationException))]
1694 public void AddSameInstance2 ()
1696 XElement root = new XElement (XName.Get ("Root"));
1697 XAttribute attr = new XAttribute (XName.Get ("a"), "v");
1700 root.Add (attr); // duplicate attribute
1701 Assert.AreEqual(2, root.Attributes().Count(), "#1");
1705 public void AddAttributeFromDifferentTree ()
1707 XElement e1 = new XElement (XName.Get ("e1"));
1708 XElement e2 = new XElement (XName.Get ("e2"));
1709 XAttribute attr = new XAttribute (XName.Get ("a"), "v");
1713 Assert.AreEqual ("<e1 a=\"v\" />", e1.ToString (), "#1");
1714 Assert.AreEqual ("<e2 a=\"v\" />", e2.ToString (), "#2");
1718 public void SavePreservePrefixes ()
1720 var x = XDocument.Parse (@"
1721 <xxx:a xmlns:xxx='http://www.foobar.com'>
1722 <xxx:b>blah blah blah</xxx:b>
1724 StringWriter sw = new StringWriter ();
1725 x.Save (sw, SaveOptions.DisableFormatting);
1726 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 ());
1730 public void LoadFromXmlTextReader ()
1732 var foo = XElement.Load (new XmlTextReader (new StringReader ("<foo></foo>")));
1733 Assert.IsNotNull (foo);
1737 public void ReplaceNodes ()
1739 var inputXml = "<Foo><C><Three>3</Three><Two></Two><One/></C><B><Aaa/><Yyy/><fff/></B><A Attrib=\"Hello World\"/></Foo>";
1740 var reader = XmlReader.Create (new StringReader (inputXml), new XmlReaderSettings ());
1741 XDocument doc = XDocument.Load (reader);
1742 var result = doc.Root.Elements ().OrderBy (el => el.Name.ToString());
1743 Assert.AreEqual (3, result.Count (), "#1");
1744 doc.Root.FirstNode.Remove ();
1745 Assert.AreEqual (2, result.Count (), "#2");
1747 XContainer container = doc.Root;
1748 container.ReplaceNodes (result);
1750 Assert.AreEqual (2, container.Elements ().Count (), "#3");
1754 public void ReplaceCreatesSnapshotBeforeRemoval ()
1757 XElement data1 = new XElement ("A");
1758 XElement data3 = new XElement ("C");
1759 XElement data4 = new XElement ("D");
1760 XElement root = new XElement ("rt",
1761 new XElement ("z", new XElement ("Name", data1), new XElement ("Desc", data4)), data3);
1762 var elements = root.Elements ().Elements ();
1763 root.ReplaceNodes (elements);
1764 root.Add (elements);
1775 Assert.AreEqual (xml.NormalizeNewline (), root.ToString ().NormalizeNewline (), "#1");
1779 public void AddBefore_ChildNode_ChangeTriggers()
1783 var child = new XElement("child");
1784 var root = new XElement("root", child);
1785 root.Changed += (o, e) => changed++;
1786 root.Changing += (o, e) => changing++;
1788 child.AddAfterSelf(new XElement("a"));
1789 Assert.AreEqual(1, changed, "#1");
1790 Assert.AreEqual(1, changing, "#2");
1792 child.AddBeforeSelf(new XElement("b"));
1793 Assert.AreEqual(2, changed, "#3");
1794 Assert.AreEqual(2, changing, "#4");
1796 child.AddFirst(new XElement("c"));
1797 Assert.AreEqual(3, changed, "#5");
1798 Assert.AreEqual(3, changing, "#6");
1802 public void AddAttribute_ToRootNode_ChangeTriggers()
1806 var node = new XElement("foo");
1807 node.Changed += (o, e) => changed ++;
1808 node.Changing += (o, e) => changing++;
1810 node.Add(new XAttribute("foo", "bar"));
1811 Assert.AreEqual(1, changing, "#1");
1812 Assert.AreEqual(1, changed, "#2");
1814 node.Add(new XAttribute("foo2", "bar2"));
1815 Assert.AreEqual(2, changing, "#3");
1816 Assert.AreEqual(2, changed, "#4");
1820 public void SetAttributeValue_ToRootNode_ChangeTriggers()
1824 var node = new XElement("foo");
1825 node.Changed += (o, e) => changed++;
1826 node.Changing += (o, e) => changing++;
1828 node.SetAttributeValue("foo", "bar");
1829 Assert.AreEqual(1, changing, "#1");
1830 Assert.AreEqual(1, changed, "#2");
1832 node.SetAttributeValue("foo2", "bar2");
1833 Assert.AreEqual(2, changing, "#3");
1834 Assert.AreEqual(2, changed, "#4");
1836 node.SetAttributeValue("foo2", null);
1837 Assert.AreEqual(3, changing, "#7");
1838 Assert.AreEqual(3, changed, "#8");
1840 node.SetAttributeValue("foo52", null);
1841 Assert.AreEqual(3, changing, "#9");
1842 Assert.AreEqual(3, changed, "#10");
1846 public void RemoveAttributes_FromRootNode_ChangeTriggers()
1850 var node = new XElement("foo", new XAttribute("foo", "bar"), new XAttribute("foo2", "bar2"), new XElement ("Barry"));
1851 node.Changed += (o, e) => changed++;
1852 node.Changing += (o, e) => changing++;
1854 node.RemoveAttributes();
1855 Assert.AreEqual(2, changing, "#1");
1856 Assert.AreEqual(2, changed, "#2");
1860 public void RemoveNodes_FromRootNode_ChangeTriggers()
1864 var node = new XElement("foo", new XAttribute("foo", "bar"), new XAttribute("foo2", "bar2"), new XElement("Barry"));
1865 node.Changed += (o, e) => changed++;
1866 node.Changing += (o, e) => changing++;
1869 Assert.AreEqual(1, changing, "#1");
1870 Assert.AreEqual(1, changed, "#2");
1874 public void RemoveAll_FromRootNode_ChangeTriggers()
1878 var node = new XElement("foo", new XAttribute("foo", "bar"), new XAttribute("foo2", "bar2"), new XElement("Barry"));
1879 node.Changed += (o, e) => changed++;
1880 node.Changing += (o, e) => changing++;
1883 Assert.AreEqual(3, changing, "#1");
1884 Assert.AreEqual(3, changed, "#2");
1888 public void AddElement_ToRootNode_ChangeTriggers()
1890 var childChanging = false;
1891 var childChanged = false;
1892 var rootChanging = false;
1893 var rootChanged = false;
1895 var child = new XElement("foo");
1896 var root = new XElement("root");
1897 child.Changing += (o, e) => childChanging = true;
1898 child.Changed += (o, e) => childChanged = true;
1900 root.Changing += (o, e) => {
1901 Assert.IsFalse(rootChanging, "#1");
1902 Assert.IsFalse(rootChanged, "#2");
1903 Assert.AreSame(child, o, "#3");
1904 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#4");
1905 rootChanging = true;
1907 root.Changed += (o, e) => {
1908 Assert.IsFalse(rootChanged, "#5");
1909 Assert.IsTrue(rootChanging, "#6");
1910 Assert.AreSame(child, o, "#7");
1911 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#8");
1916 Assert.IsFalse(childChanging, "#9");
1917 Assert.IsFalse(childChanged, "#10");
1918 Assert.IsTrue(rootChanging, "#11");
1919 Assert.IsTrue(rootChanged, "#12");
1923 public void AddElement_ToChildNode_ChangeTriggers()
1925 var childChanging = false;
1926 var childChanged = false;
1927 var rootChanging = false;
1928 var rootChanged = false;
1930 var subchild = new XElement("subfoo");
1931 var child = new XElement("foo");
1932 var root = new XElement("root", child);
1934 child.Changing += (o, e) =>
1936 Assert.IsFalse(childChanging, "#c1");
1937 Assert.IsFalse(childChanged, "#c2");
1938 Assert.IsFalse(rootChanging, "#c3");
1939 Assert.IsFalse(rootChanged, "#c4");
1940 Assert.AreSame(subchild, o, "#c5");
1941 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#c6");
1942 Assert.IsNull(subchild.Parent, "childChangingParent");
1943 childChanging = true;
1945 root.Changing += (o, e) =>
1947 Assert.IsTrue(childChanging, "#r1");
1948 Assert.IsFalse(childChanged, "#r2");
1949 Assert.IsFalse(rootChanging, "#r3");
1950 Assert.IsFalse(rootChanged, "#r4");
1951 Assert.AreSame(subchild, o, "#r5");
1952 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#r6");
1953 Assert.IsNull(subchild.Parent, "rootChangingParent");
1954 rootChanging = true;
1956 child.Changed += (o, e) =>
1958 Assert.IsTrue(childChanging, "#c7");
1959 Assert.IsFalse(childChanged, "#c8");
1960 Assert.IsTrue(rootChanging, "#c9");
1961 Assert.IsFalse(rootChanged, "#c10");
1962 Assert.AreSame(subchild, o, "#c11");
1963 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#c12");
1964 Assert.IsNotNull(subchild.Parent, "childChangedParent");
1965 childChanged = true;
1967 root.Changed += (o, e) =>
1969 Assert.IsTrue(childChanging, "#r7");
1970 Assert.IsTrue(childChanged, "#r8");
1971 Assert.IsTrue(rootChanging, "#r9");
1972 Assert.IsFalse(rootChanged, "#r10");
1973 Assert.AreSame(subchild, o, "#11");
1974 Assert.AreEqual(XObjectChange.Add, e.ObjectChange, "#12");
1975 Assert.IsNotNull(subchild.Parent, "rootChangedParent");
1979 child.Add (subchild);
1980 Assert.IsTrue(childChanging, "#a");
1981 Assert.IsTrue(childChanged, "#b");
1982 Assert.IsTrue(rootChanging, "#c");
1983 Assert.IsTrue(rootChanged, "#d");
1987 public void SetElementValue () // #699242
1989 var element = XElement.Parse ("<foo><bar>bar</bar><baz>baz</baz></foo>");
1990 element.SetElementValue ("bar", "babar");
1991 element.SetElementValue ("baz", "babaz");
1992 element.SetElementValue ("gaz", "gazonk");
1994 Assert.AreEqual ("<foo><bar>babar</bar><baz>babaz</baz><gaz>gazonk</gaz></foo>", element.ToString (SaveOptions.DisableFormatting));
1996 element.SetElementValue ("gaz", null);
1997 Assert.AreEqual ("<foo><bar>babar</bar><baz>babaz</baz></foo>", element.ToString (SaveOptions.DisableFormatting));
2001 public void Bug3137 ()
2003 CultureInfo current = Thread.CurrentThread.CurrentCulture;
2005 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
2006 var element1 = new XElement ("Property1", new XAttribute ("type", "number"), 1.2343445);
2007 Assert.AreEqual ("<Property1 type=\"number\">1.2343445</Property1>", element1.ToString (), "en-US");
2009 Thread.CurrentThread.CurrentCulture = new CultureInfo ("de-DE");
2010 // this was already working because the element was created with en-US
2011 Assert.AreEqual ("<Property1 type=\"number\">1.2343445</Property1>", element1.ToString (), "de-DE/1");
2012 // however creating a new, identical, element under de-DE return*ed* a different string
2013 var element2 = new XElement ("Property1", new XAttribute ("type", "number"), 1.2343445);
2014 Assert.AreEqual ("<Property1 type=\"number\">1.2343445</Property1>", element2.ToString (), "de-DE/2");
2017 Thread.CurrentThread.CurrentCulture = current;
2022 public void DecimalFormatting () // bug #3634
2025 var bak = Thread.CurrentThread.CurrentCulture;
2026 Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfoByIetfLanguageTag("nl-NL");
2028 var element = new XElement ("Demo", data);
2029 Assert.AreEqual ("<Demo>5.5</Demo>", element.ToString (), "#1");
2031 Thread.CurrentThread.CurrentCulture = bak;
2036 public void UseGetPrefixOfNamespaceForToString ()
2040 xsi1:type='xsi:SubscriptionEvent'
2041 xmlns:xsi='http://relevo.se/xsi'
2042 xmlns:xsi1='http://www.w3.org/2001/XMLSchema-instance'>
2043 <xsi:eventData xsi1:type='xsi:CallSubscriptionEvent'/>
2045 var e = XElement.Parse (xml);
2046 string expected = @"<xsi:eventData xsi1:type='xsi:CallSubscriptionEvent' xmlns:xsi1='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsi='http://relevo.se/xsi' />".Replace ('\'', '"');
2047 Assert.AreEqual (expected, e.Nodes ().First ().ToString (), "#1");
2051 public void DoUseEmptyNamespacePrefixWhenApplicable ()
2053 XNamespace ns = "http://jabber.org/protocol/geoloc";
2054 XElement newElement = new XElement(ns + "geoloc");
2055 Assert.AreEqual ("<geoloc xmlns=\"http://jabber.org/protocol/geoloc\" />", newElement.ToString (), "#1");
2058 [Test] // bug #10194
2059 public void SetElementValueNullOnNonExistingElement ()
2061 var xd = XDocument.Parse ("<foo />");
2062 xd.Root.SetElementValue (XName.Get ("bar"), null);
2065 [Test] // bug #11298
2066 public void ReplaceAttributesIteratesContentsFirstThenRemove ()
2068 var xmlString = "<Class Id='1' Name='' Cluster='' xmlns='urn:x' />";
2069 var e = XDocument.Parse (xmlString).Root;
2070 var attrs = e.Attributes ()
2071 .Where (a => !a.IsNamespaceDeclaration)
2072 .Select (a => a.Name.Namespace != XNamespace.None ?
2073 new XAttribute (XName.Get(a.Name.LocalName), a.Value) : a);
2074 e.ReplaceAttributes (attrs);
2075 Assert.IsNotNull (e.Attribute ("Id"), "#1");
2076 Assert.IsNotNull (e.Attribute ("Name"), "#2");
2077 Assert.IsNotNull (e.Attribute ("Cluster"), "#3");
2081 public class SerializableClass
2084 public XElement Content;
2090 public void DeserializeXElement ()
2092 var xmlString = "<Root><Data /></Root>";
2094 var serializer = new XmlSerializer (typeof (SerializableClass));
2095 var res = serializer.Deserialize (new StringReader (xmlString));
2097 Assert.IsNotNull (res, "#1");
2098 Assert.AreEqual (typeof (SerializableClass), res.GetType (), "#2");
2099 var xe = (SerializableClass)res;
2100 Assert.AreEqual (xe.Content.ToString (), "<Data />", "#3");
2104 [Test] // Bug #20151
2105 public void XElementFromArrayWithNullValuesAsObject ()
2107 string[] content = {null, "content1", null, "content2"};
2108 var el = new XElement ("test", (object)content);
2109 Assert.AreEqual ("<test>content1content2</test>", el.ToString ());
2112 [Test] // bug #14856
2113 public void PossibleDuplicateNamespaces ()
2116 "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
2117 "<TestElement xmlns=\"http://www.test.com/TestNamespace\" />";
2119 using (var stream = new MemoryStream (Encoding.UTF8.GetBytes (testXML)))
2121 var root = XElement.Load (stream);
2122 using (var savedStream = new MemoryStream ())
2124 var options = SaveOptions.None;
2126 // Comment out this line to make it not crash.
2127 options |= SaveOptions.OmitDuplicateNamespaces;
2129 root.Save (savedStream, options);
2130 savedStream.Flush ();
2131 savedStream.Position = 0;
2133 var settings = new XmlReaderSettings { IgnoreComments = true, IgnoreWhitespace = true };
2134 using (var xmlReader = XmlReader.Create (savedStream, settings))
2136 while (xmlReader.Read ())
2145 public void ParseVsReadXml ()
2147 var p = XElement.Parse ("<root xmlns='urn:foo'><foo><xxx /></foo><x:bar xmlns:x='urn:bar'><yyy /></x:bar><baz xmlns=''><zzz /></baz></root>");
2148 var r = XElement.Parse ("<foo />");
2149 XmlReader xr = XmlReader.Create (new StringReader ("<root xmlns='urn:foo'><foo><xxx /></foo><x:bar xmlns:x='urn:bar'><yyy /></x:bar><baz xmlns=''><zzz /></baz></root>"), new XmlReaderSettings ());
2150 ((IXmlSerializable)r).ReadXml (xr);
2152 Assert.IsTrue (XNode.DeepEquals (p, r), "The XElements were not equal.\nParse() expected:\n{0}\n\nBut ReadXml() was:\n{1}\n", p, r);