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;
32 using System.Xml.Linq;
34 using NUnit.Framework;
36 namespace MonoTests.System.Xml.Linq
39 public class XDocumentTest
44 string xml = "<?xml version='1.0'?><root />";
46 XDocument doc = XDocument.Load (new StringReader (xml));
47 Assert.IsTrue (doc.FirstNode is XElement, "#1");
48 Assert.IsTrue (doc.LastNode is XElement, "#2");
49 Assert.IsNull (doc.NextNode, "#3");
50 Assert.IsNull (doc.PreviousNode, "#4");
51 Assert.AreEqual (1, new List<XNode> (doc.Nodes ()).Count, "#5");
52 Assert.IsNull (doc.FirstNode.Parent, "#6");
53 Assert.AreEqual (doc.FirstNode, doc.LastNode, "#7");
54 Assert.AreEqual (XmlNodeType.Document, doc.NodeType, "#8");
55 Assert.AreEqual (doc.FirstNode, doc.Root, "#7");
61 // https://bugzilla.novell.com/show_bug.cgi?id=496285
62 byte [] bytes = Encoding.UTF8.GetBytes ("<root/>");
63 var reader = new XmlTextReader (new MemoryStream (bytes));
64 var doc = XDocument.Load (reader);
68 [ExpectedException (typeof (ArgumentException))]
69 public void LoadInvalid ()
72 XmlReaderSettings s = new XmlReaderSettings ();
73 s.ConformanceLevel = ConformanceLevel.Fragment;
75 XDocument.Load (XmlReader.Create (new StringReader (xml), s));
79 [ExpectedException (typeof (InvalidOperationException))]
80 public void LoadWhitespaces ()
83 XmlReaderSettings s = new XmlReaderSettings ();
84 s.ConformanceLevel = ConformanceLevel.Fragment;
86 XDocument.Load (XmlReader.Create (new StringReader (xml), s));
90 [ExpectedException (typeof (ArgumentException))]
91 public void AddTextToDocument ()
93 XDocument doc = new XDocument ();
98 [Category ("NotDotNet")]
99 //[ExpectedException (typeof (ArgumentException))]
100 public void AddXDeclarationToDocument ()
102 XDocument doc = new XDocument ();
103 // LAMESPEC: XDeclaration is treated as a general object
104 // and hence converted to a string -> error
105 doc.Add (new XDeclaration ("1.0", null, null));
109 [ExpectedException (typeof (ArgumentException))]
110 public void AddXAttributeToDocument ()
112 var doc = new XDocument ();
113 doc.Add (new XAttribute ("foo", " "));
117 public void AddXmlDeclarationEvenForDecllessDoc ()
119 var doc = new XDocument (
120 new XElement ("resources",
121 new XElement ("string",
122 new XAttribute ("name", "whatever"),
124 var sw = new StringWriter ();
125 using (var writer = new XmlTextWriter (sw))
126 doc.WriteTo (writer);
127 Assert.IsTrue (sw.ToString ().StartsWith ("<?xml"), "#1");