XmlElementTests.cs 20 KB

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