XmlElementTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. //
  2. // XmlElementTests
  3. //
  4. // Authors:
  5. // Jason Diamond ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. //
  8. // (C) 2002 Jason Diamond http://injektilo.org/
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using System;
  12. using System.Xml;
  13. using System.IO;
  14. using System.Text;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Xml
  17. {
  18. [TestFixture]
  19. public class XmlElementTests : Assertion
  20. {
  21. private XmlDocument document;
  22. [SetUp]
  23. public void GetReady ()
  24. {
  25. document = new XmlDocument ();
  26. }
  27. private void AssertElement (XmlElement element, string prefix,
  28. string localName, string namespaceURI,
  29. int attributesCount)
  30. {
  31. AssertEquals (prefix != String.Empty ? prefix + ":" + localName : localName, element.Name);
  32. AssertEquals (prefix, element.Prefix);
  33. AssertEquals (localName, element.LocalName);
  34. AssertEquals (namespaceURI, element.NamespaceURI);
  35. //AssertEquals (attributesCount, element.Attributes.Count);
  36. }
  37. // for NodeInserted Event
  38. private bool Inserted = false;
  39. private void OnNodeInserted (object o, XmlNodeChangedEventArgs e)
  40. {
  41. Inserted = true;
  42. }
  43. // for NodeChanged Event
  44. private bool Changed = false;
  45. private void OnNodeChanged (object o, XmlNodeChangedEventArgs e)
  46. {
  47. Changed = true;
  48. }
  49. // for NodeRemoved Event
  50. private bool Removed = false;
  51. private void OnNodeRemoved (object o, XmlNodeChangedEventArgs e)
  52. {
  53. Removed = true;
  54. }
  55. [Test]
  56. public void CloneNode ()
  57. {
  58. XmlElement element = document.CreateElement ("foo");
  59. XmlElement child = document.CreateElement ("bar");
  60. XmlElement grandson = document.CreateElement ("baz");
  61. element.SetAttribute ("attr1", "val1");
  62. element.SetAttribute ("attr2", "val2");
  63. element.AppendChild (child);
  64. child.SetAttribute ("attr3", "val3");
  65. child.AppendChild (grandson);
  66. document.AppendChild (element);
  67. XmlNode deep = element.CloneNode (true);
  68. // AssertEquals ("These should be the same", deep.OuterXml, element.OuterXml);
  69. AssertNull ("This is not null", deep.ParentNode);
  70. Assert ("Copies, not pointers", !Object.ReferenceEquals (element,deep));
  71. XmlNode shallow = element.CloneNode (false);
  72. AssertNull ("This is not null", shallow.ParentNode);
  73. Assert ("Copies, not pointers", !Object.ReferenceEquals (element,shallow));
  74. AssertEquals ("Shallow clones shalt have no children!", false, shallow.HasChildNodes);
  75. }
  76. [Test]
  77. public void ConstructionAndDefaultAttributes ()
  78. {
  79. string dtd = "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root foo CDATA 'def'>]>";
  80. string xml = dtd + "<root />";
  81. // XmlValidatingReader xvr = new XmlValidatingReader (new XmlTextReader (xml, XmlNodeType.Document, null));
  82. XmlDocument doc = new XmlDocument ();
  83. doc.LoadXml (xml);
  84. Console.WriteLine (doc.DocumentElement.Attributes.Count);
  85. Console.WriteLine (doc.CreateElement ("root").Attributes.Count);
  86. Console.WriteLine (doc.CreateElement ("root2").Attributes.Count);
  87. }
  88. [Test]
  89. public void CreateElement1 ()
  90. {
  91. XmlElement element = document.CreateElement ("name");
  92. AssertElement (element, String.Empty, "name", String.Empty, 0);
  93. }
  94. [Test]
  95. public void CreateElement1WithPrefix ()
  96. {
  97. XmlElement element = document.CreateElement ("prefix:localName");
  98. AssertElement (element, "prefix", "localName", String.Empty, 0);
  99. }
  100. [Test]
  101. public void CreateElement2 ()
  102. {
  103. XmlElement element = document.CreateElement ("qualifiedName", "namespaceURI");
  104. AssertElement (element, String.Empty, "qualifiedName",
  105. "namespaceURI", 0);
  106. }
  107. [Test]
  108. public void CreateElement2WithPrefix ()
  109. {
  110. XmlElement element = document.CreateElement ("prefix:localName", "namespaceURI");
  111. AssertElement (element, "prefix", "localName", "namespaceURI", 0);
  112. }
  113. [Test]
  114. public void CreateElement3 ()
  115. {
  116. XmlElement element = document.CreateElement ("prefix", "localName", "namespaceURI");
  117. AssertElement (element, "prefix", "localName", "namespaceURI", 0);
  118. }
  119. [Test]
  120. public void CreateElement3WithNullNamespace ()
  121. {
  122. // bug #26855, NamespaceURI should NEVER be null.
  123. XmlElement element = document.CreateElement (null, "localName", null);
  124. AssertElement (element, String.Empty, "localName", String.Empty, 0);
  125. }
  126. [Test]
  127. public void InnerAndOuterXml ()
  128. {
  129. XmlElement element;
  130. XmlText text;
  131. XmlComment comment;
  132. element = document.CreateElement ("foo");
  133. AssertEquals (String.Empty, element.InnerXml);
  134. AssertEquals ("<foo />", element.OuterXml);
  135. text = document.CreateTextNode ("bar");
  136. element.AppendChild (text);
  137. AssertEquals ("bar", element.InnerXml);
  138. AssertEquals ("<foo>bar</foo>", element.OuterXml);
  139. element.SetAttribute ("baz", "quux");
  140. AssertEquals ("bar", element.InnerXml);
  141. AssertEquals ("<foo baz=\"quux\">bar</foo>", element.OuterXml);
  142. comment = document.CreateComment ("squonk");
  143. element.AppendChild (comment);
  144. AssertEquals ("bar<!--squonk-->", element.InnerXml);
  145. AssertEquals ("<foo baz=\"quux\">bar<!--squonk--></foo>", element.OuterXml);
  146. element.RemoveAll();
  147. element.AppendChild(document.CreateElement("hoge"));
  148. AssertEquals ("<hoge />", element.InnerXml);
  149. }
  150. [Test]
  151. public void SetGetAttribute ()
  152. {
  153. XmlElement element = document.CreateElement ("foo");
  154. element.SetAttribute ("attr1", "val1");
  155. element.SetAttribute ("attr2", "val2");
  156. AssertEquals ("val1", element.GetAttribute ("attr1"));
  157. AssertEquals ("val2", element.GetAttribute ("attr2"));
  158. }
  159. [Test]
  160. public void GetElementsByTagNameNoNameSpace ()
  161. {
  162. string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
  163. <price>34.95</price></book><book><title>Bear and the Dragon</title>
  164. <author>Tom Clancy</author><price>6.95</price></book><book>
  165. <title>Bourne Identity</title><author>Robert Ludlum</author>
  166. <price>9.95</price></book><Fluffer><Nutter><book>
  167. <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
  168. <price>9.95</price></book></Nutter></Fluffer></library>";
  169. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
  170. document = new XmlDocument ();
  171. document.Load (memoryStream);
  172. XmlNodeList libraryList = document.GetElementsByTagName ("library");
  173. XmlNode xmlNode = libraryList.Item (0);
  174. XmlElement xmlElement = xmlNode as XmlElement;
  175. XmlNodeList bookList = xmlElement.GetElementsByTagName ("book");
  176. AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
  177. }
  178. [Test]
  179. public void GetElementsByTagNameUsingNameSpace ()
  180. {
  181. StringBuilder xml = new StringBuilder ();
  182. xml.Append ("<?xml version=\"1.0\" ?><library xmlns:North=\"http://www.foo.com\" ");
  183. xml.Append ("xmlns:South=\"http://www.goo.com\"><North:book type=\"non-fiction\"> ");
  184. xml.Append ("<North:title type=\"intro\">XML Fun</North:title> " );
  185. xml.Append ("<North:author>John Doe</North:author> " );
  186. xml.Append ("<North:price>34.95</North:price></North:book> " );
  187. xml.Append ("<South:book type=\"fiction\"> " );
  188. xml.Append ("<South:title>Bear and the Dragon</South:title> " );
  189. xml.Append ("<South:author>Tom Clancy</South:author> " );
  190. xml.Append ("<South:price>6.95</South:price></South:book> " );
  191. xml.Append ("<South:book type=\"fiction\"><South:title>Bourne Identity</South:title> " );
  192. xml.Append ("<South:author>Robert Ludlum</South:author> " );
  193. xml.Append ("<South:price>9.95</South:price></South:book></library>");
  194. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  195. document = new XmlDocument ();
  196. document.Load (memoryStream);
  197. XmlNodeList libraryList = document.GetElementsByTagName ("library");
  198. XmlNode xmlNode = libraryList.Item (0);
  199. XmlElement xmlElement = xmlNode as XmlElement;
  200. XmlNodeList bookList = xmlElement.GetElementsByTagName ("book", "http://www.foo.com");
  201. AssertEquals ("GetElementsByTagName (string, uri) returned incorrect count.", 1, bookList.Count);
  202. }
  203. [Test]
  204. public void GetElementsByTagNameNs2 ()
  205. {
  206. document.LoadXml (@"<root>
  207. <x:a xmlns:x='urn:foo' id='a'>
  208. <y:a xmlns:y='urn:foo' id='b'/>
  209. <x:a id='c' />
  210. <z id='d' />
  211. text node
  212. <?a processing instruction ?>
  213. <x:w id='e'/>
  214. </x:a>
  215. </root>");
  216. // id='b' has different prefix. Should not caught by (name),
  217. // while should caught by (name, ns).
  218. XmlNodeList nl = document.DocumentElement.GetElementsByTagName ("x:a");
  219. AssertEquals (2, nl.Count);
  220. AssertEquals ("a", nl [0].Attributes ["id"].Value);
  221. AssertEquals ("c", nl [1].Attributes ["id"].Value);
  222. nl = document.DocumentElement.GetElementsByTagName ("a", "urn:foo");
  223. AssertEquals (3, nl.Count);
  224. AssertEquals ("a", nl [0].Attributes ["id"].Value);
  225. AssertEquals ("b", nl [1].Attributes ["id"].Value);
  226. AssertEquals ("c", nl [2].Attributes ["id"].Value);
  227. // name wildcard
  228. nl = document.DocumentElement.GetElementsByTagName ("*");
  229. AssertEquals (5, nl.Count);
  230. AssertEquals ("a", nl [0].Attributes ["id"].Value);
  231. AssertEquals ("b", nl [1].Attributes ["id"].Value);
  232. AssertEquals ("c", nl [2].Attributes ["id"].Value);
  233. AssertEquals ("d", nl [3].Attributes ["id"].Value);
  234. AssertEquals ("e", nl [4].Attributes ["id"].Value);
  235. // wildcard - local and ns
  236. nl = document.DocumentElement.GetElementsByTagName ("*", "*");
  237. AssertEquals (5, nl.Count);
  238. AssertEquals ("a", nl [0].Attributes ["id"].Value);
  239. AssertEquals ("b", nl [1].Attributes ["id"].Value);
  240. AssertEquals ("c", nl [2].Attributes ["id"].Value);
  241. AssertEquals ("d", nl [3].Attributes ["id"].Value);
  242. AssertEquals ("e", nl [4].Attributes ["id"].Value);
  243. // namespace wildcard - namespace
  244. nl = document.DocumentElement.GetElementsByTagName ("*", "urn:foo");
  245. AssertEquals (4, nl.Count);
  246. AssertEquals ("a", nl [0].Attributes ["id"].Value);
  247. AssertEquals ("b", nl [1].Attributes ["id"].Value);
  248. AssertEquals ("c", nl [2].Attributes ["id"].Value);
  249. AssertEquals ("e", nl [3].Attributes ["id"].Value);
  250. // namespace wildcard - local only. I dare say, such usage is not XML-ish!
  251. nl = document.DocumentElement.GetElementsByTagName ("a", "*");
  252. AssertEquals (3, nl.Count);
  253. AssertEquals ("a", nl [0].Attributes ["id"].Value);
  254. AssertEquals ("b", nl [1].Attributes ["id"].Value);
  255. AssertEquals ("c", nl [2].Attributes ["id"].Value);
  256. }
  257. [Test]
  258. public void OuterXmlWithNamespace ()
  259. {
  260. XmlElement element = document.CreateElement ("foo", "bar", "#foo");
  261. AssertEquals ("<foo:bar xmlns:foo=\"#foo\" />", element.OuterXml);
  262. }
  263. [Test]
  264. public void RemoveAllAttributes ()
  265. {
  266. StringBuilder xml = new StringBuilder ();
  267. xml.Append ("<?xml version=\"1.0\" ?><library><book type=\"non-fiction\" price=\"34.95\"> ");
  268. xml.Append ("<title type=\"intro\">XML Fun</title> " );
  269. xml.Append ("<author>John Doe</author></book></library>");
  270. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  271. document = new XmlDocument ();
  272. document.Load (memoryStream);
  273. XmlNodeList bookList = document.GetElementsByTagName ("book");
  274. XmlNode xmlNode = bookList.Item (0);
  275. XmlElement xmlElement = xmlNode as XmlElement;
  276. xmlElement.RemoveAllAttributes ();
  277. AssertEquals ("attributes not properly removed.", false, xmlElement.HasAttribute ("type"));
  278. }
  279. [Test]
  280. #if NET_2_0
  281. [Ignore ("This test is very implementation dependent and thus .NET 2.0 does not pass. That's why I said http://primates.ximian.com/~atsushi/blog/archives/000416.html and http://svn.myrealbox.com/viewcvs/trunk/mono/web/xml-classes?rev=23598")]
  282. #endif
  283. public void RemoveDoesNotRemoveDefaultAttributes ()
  284. {
  285. string dtd = "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root foo CDATA 'def' bar CDATA #IMPLIED>]>";
  286. string xml = dtd + "<root bar='baz' />";
  287. XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  288. document.Load (xvr);
  289. // RemoveAll
  290. AssertNotNull (document.DocumentElement);
  291. AssertEquals ("attrCount #01", 2, document.DocumentElement.Attributes.Count);
  292. AssertEquals ("baz", document.DocumentElement.GetAttribute ("bar"));
  293. AssertEquals ("def", document.DocumentElement.GetAttribute ("foo"));
  294. AssertEquals (false, document.DocumentElement.GetAttributeNode ("foo").Specified);
  295. document.DocumentElement.RemoveAll ();
  296. AssertEquals ("attrCount #02", 1, document.DocumentElement.Attributes.Count);
  297. AssertEquals ("def", document.DocumentElement.GetAttribute ("foo"));
  298. AssertEquals (String.Empty, document.DocumentElement.GetAttribute ("bar"));
  299. // RemoveAllAttributes
  300. xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  301. document.Load (xvr);
  302. document.DocumentElement.RemoveAllAttributes ();
  303. AssertEquals ("attrCount #03", 1, document.DocumentElement.Attributes.Count);
  304. // RemoveAttribute(name)
  305. xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  306. document.Load (xvr);
  307. document.DocumentElement.RemoveAttribute ("foo");
  308. AssertEquals ("attrCount #04", 2, document.DocumentElement.Attributes.Count);
  309. // RemoveAttribute(name, ns)
  310. xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  311. document.Load (xvr);
  312. document.DocumentElement.RemoveAttribute ("foo", String.Empty);
  313. AssertEquals ("attrCount #05", 2, document.DocumentElement.Attributes.Count);
  314. // RemoveAttributeAt
  315. xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  316. document.Load (xvr);
  317. document.DocumentElement.RemoveAttributeAt (1);
  318. AssertEquals ("attrCount #06", 2, document.DocumentElement.Attributes.Count);
  319. // RemoveAttributeNode
  320. xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  321. document.Load (xvr);
  322. document.DocumentElement.RemoveAttributeNode (document.DocumentElement.Attributes [1]);
  323. AssertEquals ("attrCount #07", 2, document.DocumentElement.Attributes.Count);
  324. // RemoveAttributeNode(name, ns)
  325. xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  326. document.Load (xvr);
  327. document.DocumentElement.RemoveAttributeNode ("foo", String.Empty);
  328. AssertEquals ("attrCount #08", 2, document.DocumentElement.Attributes.Count);
  329. }
  330. [Test]
  331. public void SetAttributeNode ()
  332. {
  333. XmlDocument xmlDoc = new XmlDocument ();
  334. XmlElement xmlEl = xmlDoc.CreateElement ("TestElement");
  335. XmlAttribute xmlAttribute = xmlEl.SetAttributeNode ("attr1", "namespace1");
  336. XmlAttribute xmlAttribute2 = xmlEl.SetAttributeNode ("attr2", "namespace2");
  337. AssertEquals ("attribute name not properly created.", true, xmlAttribute.Name.Equals ("attr1"));
  338. AssertEquals ("attribute namespace not properly created.", true, xmlAttribute.NamespaceURI.Equals ("namespace1"));
  339. }
  340. [Test]
  341. [ExpectedException (typeof (XmlException))]
  342. public void SetAttributeNodeError ()
  343. {
  344. XmlDocument doc = new XmlDocument ();
  345. doc.LoadXml ("<root xmlns:x='urn:foo'/>");
  346. doc.DocumentElement.SetAttributeNode ("x:lang", "urn:foo");
  347. }
  348. [Test]
  349. public void SetAttributeXmlns ()
  350. {
  351. // should not affect Element node's xmlns
  352. XmlElement el = document.CreateElement ("root");
  353. el.SetAttribute ("xmlns", "urn:foo");
  354. AssertEquals (String.Empty, el.NamespaceURI);
  355. }
  356. [Test]
  357. public void InnerTextAndEvent ()
  358. {
  359. XmlDocument doc = new XmlDocument ();
  360. doc.LoadXml ("<root><child>text</child><child2><![CDATA[cdata]]></child2></root>");
  361. doc.NodeInserted += new XmlNodeChangedEventHandler (
  362. OnNodeInserted);
  363. doc.NodeRemoved += new XmlNodeChangedEventHandler (
  364. OnNodeRemoved);
  365. // If only one child of the element is Text node,
  366. // then no events are fired.
  367. doc.DocumentElement.FirstChild.InnerText = "no events fired.";
  368. AssertEquals ("NoInsertEventFired", false, Inserted);
  369. AssertEquals ("NoRemoveEventFired", false, Removed);
  370. AssertEquals ("SetInnerTextToSingleText", "no events fired.", doc.DocumentElement.FirstChild.InnerText);
  371. Inserted = false;
  372. Removed = false;
  373. // if only one child of the element is CDataSection,
  374. // then events are fired.
  375. doc.DocumentElement.LastChild.InnerText = "events are fired.";
  376. AssertEquals ("InsertedEventFired", true, Inserted);
  377. AssertEquals ("RemovedEventFired", true, Removed);
  378. AssertEquals ("SetInnerTextToCDataSection", "events are fired.", doc.DocumentElement.LastChild.InnerText);
  379. }
  380. [Test]
  381. public void InnerXmlSetter ()
  382. {
  383. XmlDocument doc = new XmlDocument ();
  384. doc.LoadXml ("<root/>");
  385. XmlElement el = doc.DocumentElement;
  386. AssertNull ("#Simple", el.FirstChild);
  387. el.InnerXml = "<foo><bar att='baz'/></foo>";
  388. XmlElement child = el.FirstChild as XmlElement;
  389. AssertNotNull ("#Simple.Child", child);
  390. AssertEquals ("#Simple.Child.Name", "foo", child.LocalName);
  391. XmlElement grandchild = child.FirstChild as XmlElement;
  392. AssertNotNull ("#Simple.GrandChild", grandchild);
  393. AssertEquals ("#Simple.GrandChild.Name", "bar", grandchild.LocalName);
  394. AssertEquals ("#Simple.GrandChild.Attr", "baz", grandchild.GetAttribute ("att"));
  395. doc.LoadXml ("<root xmlns='NS0' xmlns:ns1='NS1'><foo/><ns1:bar/><ns2:bar xmlns:ns2='NS2' /></root>");
  396. el = doc.DocumentElement.FirstChild.NextSibling as XmlElement; // ns1:bar
  397. AssertNull ("#Namespaced.Prepare", el.FirstChild);
  398. el.InnerXml = "<ns1:baz />";
  399. AssertNotNull ("#Namespaced.Child", el.FirstChild);
  400. AssertEquals ("#Namespaced.Child.Name", "baz", el.FirstChild.LocalName);
  401. AssertEquals ("#Namespaced.Child.NSURI", "NS1", el.FirstChild.NamespaceURI); // important!
  402. el.InnerXml = "<hoge />";
  403. AssertEquals ("#Namespaced.VerifyPreviousCleared", "hoge", el.FirstChild.Name);
  404. }
  405. [Test]
  406. public void InnerXmlSetter2 ()
  407. {
  408. // See bug #63574
  409. XmlDocument doc = new XmlDocument ();
  410. doc.LoadXml (@"<type>QPair&lt;QString,int&gt;::
  411. <ref refid='classQPair'>QPair</ref>
  412. &lt;
  413. <ref refid='classQString'>QString</ref>
  414. ,int&gt;
  415. </type>");
  416. XmlElement typeNode = doc.DocumentElement;
  417. typeNode.InnerText = "QPair<QString, int>";
  418. AssertEquals ("QPair<QString, int>", typeNode.InnerText);
  419. }
  420. [Test]
  421. public void IsEmpty ()
  422. {
  423. document.LoadXml ("<root><foo/><bar></bar></root>");
  424. Assertion.AssertEquals ("Empty", true, ((XmlElement) document.DocumentElement.FirstChild).IsEmpty);
  425. Assertion.AssertEquals ("Empty", false, ((XmlElement) document.DocumentElement.LastChild).IsEmpty);
  426. }
  427. [Test]
  428. public void RemoveAttribute ()
  429. {
  430. string xlinkURI = "http://www.w3.org/1999/XLink";
  431. XmlDocument doc = new XmlDocument ();
  432. doc.LoadXml ("<root a1='1' a2='2' xlink:href='urn:foo' xmlns:xlink='" + xlinkURI + "' />");
  433. XmlElement el = doc.DocumentElement;
  434. el.RemoveAttribute ("a1");
  435. AssertNull ("RemoveAttribute", el.GetAttributeNode ("a1"));
  436. el.RemoveAttribute ("xlink:href");
  437. AssertNull ("RemoveAttribute", el.GetAttributeNode ("href", xlinkURI));
  438. el.RemoveAllAttributes ();
  439. AssertNull ("RemoveAllAttributes", el.GetAttributeNode ("a2"));
  440. }
  441. [Test]
  442. public void WriteToWithDefaultNamespace ()
  443. {
  444. XmlDocument doc = new XmlDocument ();
  445. doc.LoadXml ("<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />");
  446. StringWriter sw = new StringWriter ();
  447. XmlTextWriter xtw = new XmlTextWriter (sw);
  448. doc.DocumentElement.WriteTo (xtw);
  449. AssertEquals ("<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", sw.ToString());
  450. }
  451. [Test]
  452. public void WriteToMakesNonsenseForDefaultNSChildren ()
  453. {
  454. XmlDocument d = new XmlDocument ();
  455. XmlElement x = d.CreateElement ("root");
  456. d.AppendChild (x);
  457. XmlElement a = d.CreateElement ("a");
  458. XmlElement b = d.CreateElement ("b");
  459. b.SetAttribute ("xmlns","probe");
  460. x.AppendChild (a);
  461. x.AppendChild (b);
  462. XmlElement b2 = d.CreateElement ("p2", "b2", "");
  463. b.AppendChild (b2);
  464. AssertEquals ("<root><a /><b xmlns=\"probe\"><b2 /></b></root>", d.OuterXml);
  465. }
  466. [Test]
  467. public void WriteToWithDeletedNamespacePrefix ()
  468. {
  469. XmlDocument doc = new XmlDocument ();
  470. doc.LoadXml ("<root xmlns:foo='urn:dummy'><foo foo:bar='baz' /></root>");
  471. doc.DocumentElement.RemoveAllAttributes ();
  472. Assert (doc.DocumentElement.FirstChild.OuterXml.IndexOf("xmlns:foo") > 0);
  473. }
  474. [Test]
  475. public void WriteToWithDifferentNamespaceAttributes ()
  476. {
  477. XmlDocument doc = new XmlDocument ();
  478. doc.LoadXml ("<root xmlns:foo='urn:dummy' xmlns:html='http://www.w3.org/1999/xhtml' html:style='font-size: 1em'></root>");
  479. Assert (doc.OuterXml.IndexOf ("xmlns:html=\"http://www.w3.org/1999/xhtml\"") > 0);
  480. }
  481. [Test]
  482. public void WriteToDefaultAttribute ()
  483. {
  484. // default attributes should be ignored.
  485. string dtd = "<!DOCTYPE root[<!ATTLIST root hoge CDATA 'hoge-def'><!ENTITY foo 'ent-foo'>]>";
  486. string xml = dtd + "<root>&foo;</root>";
  487. XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document,null);
  488. xvr.EntityHandling = EntityHandling.ExpandCharEntities;
  489. xvr.ValidationType = ValidationType.None;
  490. document.Load (xvr);
  491. StringWriter sw = new StringWriter ();
  492. XmlTextWriter xtw = new XmlTextWriter (sw);
  493. document.DocumentElement.WriteTo (xtw);
  494. AssertEquals ("<root>&foo;</root>", sw.ToString ());
  495. }
  496. [Test]
  497. #if ONLY_1_1
  498. [ExpectedException (typeof (ArgumentNullException))]
  499. #endif
  500. public void SetNullPrefix ()
  501. {
  502. XmlDocument doc = new XmlDocument ();
  503. doc.LoadXml ("<root/>");
  504. doc.DocumentElement.Prefix = null;
  505. #if NET_2_0
  506. AssertEquals ("#1", string.Empty, doc.DocumentElement.Prefix);
  507. AssertClearPrefix ((string) null);
  508. #endif
  509. }
  510. [Test]
  511. public void SetEmptyStringPrefix ()
  512. {
  513. XmlDocument doc = new XmlDocument ();
  514. doc.LoadXml ("<root />");
  515. doc.DocumentElement.Prefix = String.Empty;
  516. AssertEquals ("#1", string.Empty, doc.DocumentElement.Prefix);
  517. AssertClearPrefix (string.Empty);
  518. }
  519. private void AssertClearPrefix (string newPrefix)
  520. {
  521. XmlDocument doc = new XmlDocument ();
  522. doc.LoadXml ("<x:root xmlns:x=\"http://somenamespace.com\" />");
  523. AssertEquals ("#Clear1", "<x:root xmlns:x=\"http://somenamespace.com\" />",
  524. doc.OuterXml);
  525. AssertEquals ("#Clear2", "<x:root xmlns:x=\"http://somenamespace.com\" />",
  526. doc.DocumentElement.OuterXml);
  527. AssertEquals ("#Clear3", "x", doc.DocumentElement.Prefix);
  528. doc.DocumentElement.Prefix = newPrefix;
  529. AssertEquals ("#Clear4", "<root xmlns:x=\"http://somenamespace.com\" xmlns=\"http://somenamespace.com\" />",
  530. doc.OuterXml);
  531. AssertEquals ("#Clear5", "<root xmlns:x=\"http://somenamespace.com\" xmlns=\"http://somenamespace.com\" />",
  532. doc.DocumentElement.OuterXml);
  533. AssertEquals ("#Clear6", string.Empty, doc.DocumentElement.Prefix);
  534. }
  535. [Test]
  536. public void NullPrefix ()
  537. {
  538. new MyXmlElement ("foo", "urn:foo", new XmlDocument ());
  539. }
  540. class MyXmlElement : XmlElement
  541. {
  542. public MyXmlElement (string localName, string ns, XmlDocument doc)
  543. : base (null, localName, ns, doc)
  544. {
  545. }
  546. }
  547. }
  548. }