2
0

XmlElementTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. public void RemoveDoesNotRemoveDefaultAttributes ()
  281. {
  282. string dtd = "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root foo CDATA 'def' bar CDATA #IMPLIED>]>";
  283. string xml = dtd + "<root bar='baz' />";
  284. XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  285. document.Load (xvr);
  286. // RemoveAll
  287. AssertNotNull (document.DocumentElement);
  288. AssertEquals ("attrCount #01", 2, document.DocumentElement.Attributes.Count);
  289. AssertEquals ("baz", document.DocumentElement.GetAttribute ("bar"));
  290. AssertEquals ("def", document.DocumentElement.GetAttribute ("foo"));
  291. AssertEquals (false, document.DocumentElement.GetAttributeNode ("foo").Specified);
  292. document.DocumentElement.RemoveAll ();
  293. AssertEquals ("attrCount #02", 1, document.DocumentElement.Attributes.Count);
  294. AssertEquals ("def", document.DocumentElement.GetAttribute ("foo"));
  295. AssertEquals (String.Empty, document.DocumentElement.GetAttribute ("bar"));
  296. // RemoveAllAttributes
  297. xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  298. document.Load (xvr);
  299. document.DocumentElement.RemoveAllAttributes ();
  300. AssertEquals ("attrCount #03", 1, document.DocumentElement.Attributes.Count);
  301. // RemoveAttribute(name)
  302. xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  303. document.Load (xvr);
  304. document.DocumentElement.RemoveAttribute ("foo");
  305. AssertEquals ("attrCount #04", 2, document.DocumentElement.Attributes.Count);
  306. // RemoveAttribute(name, ns)
  307. xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  308. document.Load (xvr);
  309. document.DocumentElement.RemoveAttribute ("foo", String.Empty);
  310. AssertEquals ("attrCount #05", 2, document.DocumentElement.Attributes.Count);
  311. // RemoveAttributeAt
  312. xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  313. document.Load (xvr);
  314. document.DocumentElement.RemoveAttributeAt (1);
  315. AssertEquals ("attrCount #06", 2, document.DocumentElement.Attributes.Count);
  316. // RemoveAttributeNode
  317. xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  318. document.Load (xvr);
  319. document.DocumentElement.RemoveAttributeNode (document.DocumentElement.Attributes [1]);
  320. AssertEquals ("attrCount #07", 2, document.DocumentElement.Attributes.Count);
  321. // RemoveAttributeNode(name, ns)
  322. xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  323. document.Load (xvr);
  324. document.DocumentElement.RemoveAttributeNode ("foo", String.Empty);
  325. AssertEquals ("attrCount #08", 2, document.DocumentElement.Attributes.Count);
  326. }
  327. [Test]
  328. public void SetAttributeNode ()
  329. {
  330. XmlDocument xmlDoc = new XmlDocument ();
  331. XmlElement xmlEl = xmlDoc.CreateElement ("TestElement");
  332. XmlAttribute xmlAttribute = xmlEl.SetAttributeNode ("attr1", "namespace1");
  333. XmlAttribute xmlAttribute2 = xmlEl.SetAttributeNode ("attr2", "namespace2");
  334. AssertEquals ("attribute name not properly created.", true, xmlAttribute.Name.Equals ("attr1"));
  335. AssertEquals ("attribute namespace not properly created.", true, xmlAttribute.NamespaceURI.Equals ("namespace1"));
  336. }
  337. [Test]
  338. [ExpectedException (typeof (XmlException))]
  339. public void SetAttributeNodeError ()
  340. {
  341. XmlDocument doc = new XmlDocument ();
  342. doc.LoadXml ("<root xmlns:x='urn:foo'/>");
  343. doc.DocumentElement.SetAttributeNode ("x:lang", "urn:foo");
  344. }
  345. [Test]
  346. public void SetAttributeXmlns ()
  347. {
  348. // should not affect Element node's xmlns
  349. XmlElement el = document.CreateElement ("root");
  350. el.SetAttribute ("xmlns", "urn:foo");
  351. AssertEquals (String.Empty, el.NamespaceURI);
  352. }
  353. [Test]
  354. public void InnerTextAndEvent ()
  355. {
  356. XmlDocument doc = new XmlDocument ();
  357. doc.LoadXml ("<root><child>text</child><child2><![CDATA[cdata]]></child2></root>");
  358. doc.NodeInserted += new XmlNodeChangedEventHandler (
  359. OnNodeInserted);
  360. doc.NodeRemoved += new XmlNodeChangedEventHandler (
  361. OnNodeRemoved);
  362. // If only one child of the element is Text node,
  363. // then no events are fired.
  364. doc.DocumentElement.FirstChild.InnerText = "no events fired.";
  365. AssertEquals ("NoInsertEventFired", false, Inserted);
  366. AssertEquals ("NoRemoveEventFired", false, Removed);
  367. AssertEquals ("SetInnerTextToSingleText", "no events fired.", doc.DocumentElement.FirstChild.InnerText);
  368. Inserted = false;
  369. Removed = false;
  370. // if only one child of the element is CDataSection,
  371. // then events are fired.
  372. doc.DocumentElement.LastChild.InnerText = "events are fired.";
  373. AssertEquals ("InsertedEventFired", true, Inserted);
  374. AssertEquals ("RemovedEventFired", true, Removed);
  375. AssertEquals ("SetInnerTextToCDataSection", "events are fired.", doc.DocumentElement.LastChild.InnerText);
  376. }
  377. [Test]
  378. public void InnerXmlSetter ()
  379. {
  380. XmlDocument doc = new XmlDocument ();
  381. doc.LoadXml ("<root/>");
  382. XmlElement el = doc.DocumentElement;
  383. AssertNull ("#Simple", el.FirstChild);
  384. el.InnerXml = "<foo><bar att='baz'/></foo>";
  385. XmlElement child = el.FirstChild as XmlElement;
  386. AssertNotNull ("#Simple.Child", child);
  387. AssertEquals ("#Simple.Child.Name", "foo", child.LocalName);
  388. XmlElement grandchild = child.FirstChild as XmlElement;
  389. AssertNotNull ("#Simple.GrandChild", grandchild);
  390. AssertEquals ("#Simple.GrandChild.Name", "bar", grandchild.LocalName);
  391. AssertEquals ("#Simple.GrandChild.Attr", "baz", grandchild.GetAttribute ("att"));
  392. doc.LoadXml ("<root xmlns='NS0' xmlns:ns1='NS1'><foo/><ns1:bar/><ns2:bar xmlns:ns2='NS2' /></root>");
  393. el = doc.DocumentElement.FirstChild.NextSibling as XmlElement; // ns1:bar
  394. AssertNull ("#Namespaced.Prepare", el.FirstChild);
  395. el.InnerXml = "<ns1:baz />";
  396. AssertNotNull ("#Namespaced.Child", el.FirstChild);
  397. AssertEquals ("#Namespaced.Child.Name", "baz", el.FirstChild.LocalName);
  398. AssertEquals ("#Namespaced.Child.NSURI", "NS1", el.FirstChild.NamespaceURI); // important!
  399. el.InnerXml = "<hoge />";
  400. AssertEquals ("#Namespaced.VerifyPreviousCleared", "hoge", el.FirstChild.Name);
  401. }
  402. [Test]
  403. public void InnerXmlSetter2 ()
  404. {
  405. // See bug #63574
  406. XmlDocument doc = new XmlDocument ();
  407. doc.LoadXml (@"<type>QPair&lt;QString,int&gt;::
  408. <ref refid='classQPair'>QPair</ref>
  409. &lt;
  410. <ref refid='classQString'>QString</ref>
  411. ,int&gt;
  412. </type>");
  413. XmlElement typeNode = doc.DocumentElement;
  414. typeNode.InnerText = "QPair<QString, int>";
  415. AssertEquals ("QPair<QString, int>", typeNode.InnerText);
  416. }
  417. [Test]
  418. public void IsEmpty ()
  419. {
  420. document.LoadXml ("<root><foo/><bar></bar></root>");
  421. Assertion.AssertEquals ("Empty", true, ((XmlElement) document.DocumentElement.FirstChild).IsEmpty);
  422. Assertion.AssertEquals ("Empty", false, ((XmlElement) document.DocumentElement.LastChild).IsEmpty);
  423. }
  424. [Test]
  425. public void RemoveAttribute ()
  426. {
  427. string xlinkURI = "http://www.w3.org/1999/XLink";
  428. XmlDocument doc = new XmlDocument ();
  429. doc.LoadXml ("<root a1='1' a2='2' xlink:href='urn:foo' xmlns:xlink='" + xlinkURI + "' />");
  430. XmlElement el = doc.DocumentElement;
  431. el.RemoveAttribute ("a1");
  432. AssertNull ("RemoveAttribute", el.GetAttributeNode ("a1"));
  433. el.RemoveAttribute ("xlink:href");
  434. AssertNull ("RemoveAttribute", el.GetAttributeNode ("href", xlinkURI));
  435. el.RemoveAllAttributes ();
  436. AssertNull ("RemoveAllAttributes", el.GetAttributeNode ("a2"));
  437. }
  438. [Test]
  439. public void WriteToWithDefaultNamespace ()
  440. {
  441. XmlDocument doc = new XmlDocument ();
  442. doc.LoadXml ("<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />");
  443. StringWriter sw = new StringWriter ();
  444. XmlTextWriter xtw = new XmlTextWriter (sw);
  445. doc.DocumentElement.WriteTo (xtw);
  446. AssertEquals ("<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", sw.ToString());
  447. }
  448. [Test]
  449. public void WriteToMakesNonsenseForDefaultNSChildren ()
  450. {
  451. XmlDocument d = new XmlDocument ();
  452. XmlElement x = d.CreateElement ("root");
  453. d.AppendChild (x);
  454. XmlElement a = d.CreateElement ("a");
  455. XmlElement b = d.CreateElement ("b");
  456. b.SetAttribute ("xmlns","probe");
  457. x.AppendChild (a);
  458. x.AppendChild (b);
  459. XmlElement b2 = d.CreateElement ("p2", "b2", "");
  460. b.AppendChild (b2);
  461. AssertEquals ("<root><a /><b xmlns=\"probe\"><b2 /></b></root>", d.OuterXml);
  462. }
  463. [Test]
  464. public void WriteToWithDeletedNamespacePrefix ()
  465. {
  466. XmlDocument doc = new XmlDocument ();
  467. doc.LoadXml ("<root xmlns:foo='urn:dummy'><foo foo:bar='baz' /></root>");
  468. doc.DocumentElement.RemoveAllAttributes ();
  469. Assert (doc.DocumentElement.FirstChild.OuterXml.IndexOf("xmlns:foo") > 0);
  470. }
  471. [Test]
  472. public void WriteToWithDifferentNamespaceAttributes ()
  473. {
  474. XmlDocument doc = new XmlDocument ();
  475. doc.LoadXml ("<root xmlns:foo='urn:dummy' xmlns:html='http://www.w3.org/1999/xhtml' html:style='font-size: 1em'></root>");
  476. Assert (doc.OuterXml.IndexOf ("xmlns:html=\"http://www.w3.org/1999/xhtml\"") > 0);
  477. }
  478. [Test]
  479. public void WriteToDefaultAttribute ()
  480. {
  481. // default attributes should be ignored.
  482. string dtd = "<!DOCTYPE root[<!ATTLIST root hoge CDATA 'hoge-def'><!ENTITY foo 'ent-foo'>]>";
  483. string xml = dtd + "<root>&foo;</root>";
  484. XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document,null);
  485. xvr.EntityHandling = EntityHandling.ExpandCharEntities;
  486. xvr.ValidationType = ValidationType.None;
  487. document.Load (xvr);
  488. StringWriter sw = new StringWriter ();
  489. XmlTextWriter xtw = new XmlTextWriter (sw);
  490. document.DocumentElement.WriteTo (xtw);
  491. AssertEquals ("<root>&foo;</root>", sw.ToString ());
  492. }
  493. }
  494. }