XmlNodeTests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. //
  2. // System.Xml.XmlNodeTests
  3. //
  4. // Authors:
  5. // Kral Ferch <[email protected]>
  6. // Martin Willemoes Hansen
  7. //
  8. // (C) 2002 Kral Ferch
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using System;
  12. using System.IO;
  13. using System.Text;
  14. using System.Xml;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Xml
  17. {
  18. [TestFixture]
  19. public class XmlNodeTests
  20. {
  21. XmlDocument document;
  22. XmlElement element;
  23. XmlElement element2;
  24. bool inserted;
  25. bool inserting;
  26. bool changed;
  27. bool changing;
  28. bool removed;
  29. bool removing;
  30. [SetUp]
  31. public void GetReady ()
  32. {
  33. document = new XmlDocument ();
  34. document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
  35. document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInserting);
  36. document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
  37. document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemoving);
  38. element = document.CreateElement ("foo");
  39. element2 = document.CreateElement ("bar");
  40. }
  41. private void EventNodeInserted(Object sender, XmlNodeChangedEventArgs e)
  42. {
  43. inserted = true;
  44. }
  45. private void EventNodeInserting (Object sender, XmlNodeChangedEventArgs e)
  46. {
  47. inserting = true;
  48. }
  49. private void EventNodeChanged(Object sender, XmlNodeChangedEventArgs e)
  50. {
  51. changed = true;
  52. }
  53. private void EventNodeChanging (Object sender, XmlNodeChangedEventArgs e)
  54. {
  55. changing = true;
  56. }
  57. private void EventNodeRemoved(Object sender, XmlNodeChangedEventArgs e)
  58. {
  59. removed = true;
  60. }
  61. private void EventNodeRemoving (Object sender, XmlNodeChangedEventArgs e)
  62. {
  63. removing = true;
  64. }
  65. [Test]
  66. public void AppendChild ()
  67. {
  68. XmlComment comment;
  69. inserted = false;
  70. inserting = false;
  71. element.AppendChild (element2);
  72. Assert.IsTrue (inserted);
  73. Assert.IsTrue (inserting);
  74. // Can only append to elements, documents, and attributes
  75. try
  76. {
  77. comment = document.CreateComment ("baz");
  78. comment.AppendChild (element2);
  79. Assert.Fail ("Expected an InvalidOperationException to be thrown.");
  80. }
  81. catch (InvalidOperationException) {}
  82. // Can't append a node from one document into another document.
  83. XmlDocument document2 = new XmlDocument();
  84. Assert.AreEqual (1, element.ChildNodes.Count);
  85. try
  86. {
  87. element2 = document2.CreateElement ("qux");
  88. element.AppendChild (element2);
  89. Assert.Fail ("Expected an ArgumentException to be thrown.");
  90. }
  91. catch (ArgumentException) {}
  92. Assert.AreEqual (1, element.ChildNodes.Count);
  93. // Can't append to a readonly node.
  94. /* TODO put this in when I figure out how to create a read-only node.
  95. try
  96. {
  97. XmlElement element3 = (XmlElement)element.CloneNode (false);
  98. Assert.IsTrue (!element.IsReadOnly);
  99. Assert.IsTrue (element3.IsReadOnly);
  100. element2 = document.CreateElement ("quux");
  101. element3.AppendChild (element2);
  102. Assert.Fail ("Expected an ArgumentException to be thrown.");
  103. }
  104. catch (ArgumentException) {}
  105. */
  106. }
  107. [Test]
  108. public void GetNamespaceOfPrefix ()
  109. {
  110. document.LoadXml ("<root xmlns='urn:default' attr='value' "
  111. + "xml:lang='en' xmlns:foo='urn:foo' foo:att='fooatt'>text node</root>");
  112. XmlNode n = document.DocumentElement;
  113. Assert.AreEqual ("urn:default", n.GetNamespaceOfPrefix (String.Empty), "#1");
  114. Assert.AreEqual ("urn:foo", n.GetNamespaceOfPrefix ("foo"), "#2");
  115. Assert.AreEqual (String.Empty, n.GetNamespaceOfPrefix ("bar"), "#3");
  116. #if NET_2_0
  117. Assert.AreEqual ("http://www.w3.org/XML/1998/namespace", n.GetNamespaceOfPrefix ("xml"), "#4");
  118. Assert.AreEqual ("http://www.w3.org/2000/xmlns/", n.GetNamespaceOfPrefix ("xmlns"), "#5");
  119. #else
  120. Assert.AreEqual (String.Empty, n.GetNamespaceOfPrefix ("xml"), "#4");
  121. Assert.AreEqual (String.Empty, n.GetNamespaceOfPrefix ("xmlns"), "#5");
  122. #endif
  123. n = document.DocumentElement.FirstChild;
  124. Assert.AreEqual ("urn:default", n.GetNamespaceOfPrefix (String.Empty), "#6");
  125. Assert.AreEqual ("urn:foo", n.GetNamespaceOfPrefix ("foo"), "#7");
  126. Assert.AreEqual (String.Empty, n.GetNamespaceOfPrefix ("bar"), "#8");
  127. #if NET_2_0
  128. Assert.AreEqual ("http://www.w3.org/XML/1998/namespace", n.GetNamespaceOfPrefix ("xml"), "#9");
  129. Assert.AreEqual ("http://www.w3.org/2000/xmlns/", n.GetNamespaceOfPrefix ("xmlns"), "#10");
  130. #else
  131. Assert.AreEqual (String.Empty, n.GetNamespaceOfPrefix ("xml"), "#9");
  132. Assert.AreEqual (String.Empty, n.GetNamespaceOfPrefix ("xmlns"), "#10");
  133. #endif
  134. }
  135. [Test]
  136. [ExpectedException (typeof (ArgumentNullException))]
  137. public void GetNamespaceOfPrefixNullArg ()
  138. {
  139. new XmlDocument ().GetNamespaceOfPrefix (null);
  140. }
  141. [Test]
  142. public void InsertBefore()
  143. {
  144. document = new XmlDocument();
  145. document.LoadXml("<root><sub /></root>");
  146. XmlElement docelem = document.DocumentElement;
  147. docelem.InsertBefore(document.CreateElement("good_child"), docelem.FirstChild);
  148. Assert.AreEqual ("good_child", docelem.FirstChild.Name, "InsertBefore.Normal");
  149. // These are required for .NET 1.0 but not for .NET 1.1.
  150. try {
  151. document.InsertBefore (document.CreateElement ("BAD_MAN"), docelem);
  152. Assert.Fail ("#InsertBefore.BadPositionButNoError.1");
  153. }
  154. catch (Exception) {}
  155. }
  156. [Test]
  157. public void InsertAfter()
  158. {
  159. document = new XmlDocument();
  160. document.LoadXml("<root><sub1 /><sub2 /></root>");
  161. XmlElement docelem = document.DocumentElement;
  162. XmlElement newelem = document.CreateElement("good_child");
  163. docelem.InsertAfter(newelem, docelem.FirstChild);
  164. Assert.AreEqual (3, docelem.ChildNodes.Count, "InsertAfter.Normal");
  165. Assert.AreEqual ("sub1", docelem.FirstChild.Name, "InsertAfter.First");
  166. Assert.AreEqual ("sub2", docelem.LastChild.Name, "InsertAfter.Last");
  167. Assert.AreEqual ("good_child", docelem.FirstChild.NextSibling.Name, "InsertAfter.Prev");
  168. Assert.AreEqual ("good_child", docelem.LastChild.PreviousSibling.Name, "InsertAfter.Next");
  169. // this doesn't throw any exception *only on .NET 1.1*
  170. // .NET 1.0 throws an exception.
  171. try {
  172. document.InsertAfter(document.CreateElement("BAD_MAN"), docelem);
  173. #if USE_VERSION_1_1
  174. Assert.AreEqual ("<root><sub1 /><good_child /><sub2 /></root><BAD_MAN />", document.InnerXml, "InsertAfter with bad location");
  175. } catch (XmlException ex) {
  176. throw ex;
  177. }
  178. #else
  179. } catch (Exception) {}
  180. #endif
  181. }
  182. [Test]
  183. public void Normalize ()
  184. {
  185. XmlDocument doc = new XmlDocument ();
  186. doc.LoadXml ("<root>This is the <b>hardest</b> one.</root>");
  187. doc.NodeInserted += new XmlNodeChangedEventHandler (EventNodeInserted);
  188. doc.NodeChanged += new XmlNodeChangedEventHandler (EventNodeChanged);
  189. doc.NodeRemoved += new XmlNodeChangedEventHandler (EventNodeRemoved);
  190. Assert.AreEqual (3, doc.DocumentElement.ChildNodes.Count);
  191. doc.DocumentElement.Normalize ();
  192. Assert.AreEqual (3, doc.DocumentElement.ChildNodes.Count);
  193. Assert.IsTrue (changed);
  194. inserted = changed = removed = false;
  195. doc.DocumentElement.AppendChild (doc.CreateTextNode ("Addendum."));
  196. Assert.AreEqual (4, doc.DocumentElement.ChildNodes.Count);
  197. inserted = changed = removed = false;
  198. doc.DocumentElement.Normalize ();
  199. Assert.AreEqual (3, doc.DocumentElement.ChildNodes.Count);
  200. Assert.IsTrue (changed);
  201. Assert.IsTrue (removed);
  202. inserted = changed = removed = false;
  203. doc.DocumentElement.SetAttribute ("attr", "");
  204. XmlAttribute attr = doc.DocumentElement.Attributes [0] as XmlAttribute;
  205. Assert.AreEqual (1, attr.ChildNodes.Count);
  206. inserted = changed = removed = false;
  207. attr.Normalize ();
  208. // Such behavior violates DOM Level 2 Node#normalize(),
  209. // but MS DOM is designed as such.
  210. Assert.AreEqual (1, attr.ChildNodes.Count);
  211. }
  212. [Test]
  213. public void Normalize2 ()
  214. {
  215. XmlDocument doc = new XmlDocument ();
  216. doc.PreserveWhitespace = true;
  217. doc.LoadXml ("<root> </root>");
  218. XmlElement root = doc.DocumentElement;
  219. root.AppendChild (doc.CreateTextNode ("foo"));
  220. root.AppendChild (doc.CreateTextNode ("bar"));
  221. root.AppendChild (doc.CreateWhitespace (" "));
  222. root.AppendChild (doc.CreateTextNode ("baz"));
  223. doc.NodeInserted += new XmlNodeChangedEventHandler (OnChange);
  224. doc.NodeChanged += new XmlNodeChangedEventHandler (OnChange);
  225. doc.NodeRemoved += new XmlNodeChangedEventHandler (OnChange);
  226. Assert.AreEqual (5, root.ChildNodes.Count, "Before Normalize()");
  227. root.Normalize ();
  228. Assert.AreEqual ("<root> foobar baz</root>", root.OuterXml);
  229. Assert.AreEqual (1, root.ChildNodes.Count, "After Normalize()");
  230. }
  231. int normalize2Count;
  232. private void OnChange (object o, XmlNodeChangedEventArgs e)
  233. {
  234. switch (normalize2Count) {
  235. case 0:
  236. Assert.AreEqual (XmlNodeChangedAction.Remove, e.Action, "Action0");
  237. Assert.AreEqual (" ", e.Node.Value, "Value0");
  238. break;
  239. case 1:
  240. Assert.AreEqual (XmlNodeChangedAction.Remove, e.Action, "Action1");
  241. Assert.AreEqual ("bar", e.Node.Value, "Value1");
  242. break;
  243. case 2:
  244. Assert.AreEqual (XmlNodeChangedAction.Remove, e.Action, "Action2");
  245. Assert.AreEqual (" ", e.Node.Value, "Value2");
  246. break;
  247. case 3:
  248. Assert.AreEqual (XmlNodeChangedAction.Remove, e.Action, "Action3");
  249. Assert.AreEqual ("baz", e.Node.Value, "Value3");
  250. break;
  251. case 4:
  252. Assert.AreEqual (XmlNodeChangedAction.Change, e.Action, "Action4");
  253. Assert.AreEqual (" foobar baz", e.Node.Value, "Value4");
  254. break;
  255. default:
  256. Assert.Fail (String.Format ("Unexpected event. Action = {0}, node type = {1}, node name = {2}, node value = {3}", e.Action, e.Node.NodeType, e.Node.Name, e.Node.Value));
  257. break;
  258. }
  259. normalize2Count++;
  260. }
  261. [Test]
  262. public void PrependChild()
  263. {
  264. document = new XmlDocument();
  265. document.LoadXml("<root><sub1 /><sub2 /></root>");
  266. XmlElement docelem = document.DocumentElement;
  267. docelem.PrependChild(document.CreateElement("prepender"));
  268. Assert.AreEqual ("prepender", docelem.FirstChild.Name, "PrependChild");
  269. }
  270. public void saveTestRemoveAll ()
  271. {
  272. // TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
  273. element.AppendChild(element2);
  274. removed = false;
  275. removing = false;
  276. element.RemoveAll ();
  277. Assert.IsTrue (removed);
  278. Assert.IsTrue (removing);
  279. }
  280. [Test]
  281. public void RemoveChild ()
  282. {
  283. element.AppendChild(element2);
  284. removed = false;
  285. removing = false;
  286. element.RemoveChild (element2);
  287. Assert.IsTrue (removed);
  288. Assert.IsTrue (removing);
  289. }
  290. [Test]
  291. public void RemoveLastChild ()
  292. {
  293. element.InnerXml = "<foo/><bar/><baz/>";
  294. element.RemoveChild (element.LastChild);
  295. Assert.IsNotNull (element.FirstChild);
  296. }
  297. [Test]
  298. public void GetPrefixOfNamespace ()
  299. {
  300. document.LoadXml ("<root><c1 xmlns='urn:foo'><c2 xmlns:foo='urn:foo' xmlns='urn:bar'><c3 xmlns=''/></c2></c1></root>");
  301. Assert.AreEqual (String.Empty, document.DocumentElement.GetPrefixOfNamespace ("urn:foo"), "root");
  302. Assert.AreEqual (String.Empty, document.DocumentElement.GetPrefixOfNamespace ("urn:foo"), "c1");
  303. Assert.AreEqual (String.Empty, document.DocumentElement.FirstChild.GetPrefixOfNamespace ("urn:foo"), "c2");
  304. Assert.AreEqual ("foo", document.DocumentElement.FirstChild.FirstChild.GetPrefixOfNamespace ("urn:foo"), "c3");
  305. // disconnected nodes.
  306. XmlNode n = document.CreateElement ("foo");
  307. Assert.AreEqual (String.Empty, n.GetPrefixOfNamespace ("foo"));
  308. n = document.CreateTextNode ("text"); // does not have Attributes
  309. Assert.AreEqual (String.Empty, n.GetPrefixOfNamespace ("foo"));
  310. n = document.CreateXmlDeclaration ("1.0", null, null); // does not have Attributes
  311. Assert.AreEqual (String.Empty, n.GetPrefixOfNamespace ("foo"));
  312. }
  313. [Test]
  314. public void GetPrefixOfNamespace2 ()
  315. {
  316. XmlDocument doc = new XmlDocument ();
  317. doc.AppendChild (doc.CreateElement ("foo"));
  318. doc.DocumentElement.SetAttributeNode (
  319. doc.CreateAttribute ("xmlns", "u", "http://www.w3.org/2000/xmlns/"));
  320. doc.DocumentElement.Attributes [0].Value = "urn:foo";
  321. XmlElement el = doc.CreateElement ("bar");
  322. doc.DocumentElement.AppendChild (el);
  323. Assert.AreEqual ("u", el.GetPrefixOfNamespace ("urn:foo"));
  324. }
  325. [Test]
  326. public void ReplaceChild ()
  327. {
  328. document.LoadXml ("<root/>");
  329. document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
  330. document.NodeChanged += new XmlNodeChangedEventHandler (this.EventNodeChanged);
  331. document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
  332. inserted = changed = removed = false;
  333. XmlElement el = document.CreateElement("root2");
  334. document.ReplaceChild (el, document.DocumentElement);
  335. Assert.AreEqual ("root2", document.DocumentElement.Name);
  336. Assert.AreEqual (1, document.ChildNodes.Count);
  337. Assert.IsTrue (inserted && removed && !changed);
  338. }
  339. [Test]
  340. public void InnerText ()
  341. {
  342. document.LoadXml ("<root>This is <b>mixed</b> content. Also includes <![CDATA[CDATA section]]>.<!-- Should be ignored --></root>");
  343. string total = "This is mixed content. Also includes CDATA section.";
  344. XmlNode elemB = document.DocumentElement.ChildNodes [1];
  345. Assert.AreEqual ("mixed", elemB.FirstChild.InnerText); // text node
  346. Assert.AreEqual ("mixed", elemB.InnerText); // element b
  347. Assert.AreEqual (total, document.DocumentElement.InnerText); // element root
  348. Assert.AreEqual (total, document.InnerText); // whole document
  349. }
  350. [Test]
  351. public void InnerXmlWithXmlns ()
  352. {
  353. XmlDocument document = new XmlDocument ();
  354. XmlElement xel = document.CreateElement ("KeyValue", "http://www.w3.org/2000/09/xmldsig#");
  355. xel.SetAttribute ("xmlns", "http://www.w3.org/2000/09/xmldsig#");
  356. xel.InnerXml = "<DSAKeyValue>blablabla</DSAKeyValue>";
  357. string expected = "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue>blablabla</DSAKeyValue></KeyValue>";
  358. Assert.AreEqual (expected, xel.OuterXml);
  359. }
  360. [Test]
  361. public void SelectNodes ()
  362. {
  363. // This test is done in this class since it tests only XmlDocumentNavigator.
  364. string xpath = "//@*|//namespace::*";
  365. XmlDocument doc = new XmlDocument ();
  366. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  367. XmlNodeList nl = doc.SelectNodes (xpath);
  368. Assert.AreEqual (6, nl.Count);
  369. // BTW, as for namespace nodes, Node does not exist
  370. // in the tree, so the return value should be
  371. // implementation dependent.
  372. Assert.AreEqual (XmlNodeType.Attribute, nl [0].NodeType, "#1");
  373. Assert.AreEqual (XmlNodeType.Attribute, nl [1].NodeType, "#2");
  374. Assert.AreEqual (XmlNodeType.Attribute, nl [2].NodeType, "#3");
  375. Assert.AreEqual (XmlNodeType.Attribute, nl [3].NodeType, "#4");
  376. Assert.AreEqual (XmlNodeType.Attribute, nl [4].NodeType, "#5");
  377. Assert.AreEqual (XmlNodeType.Attribute, nl [5].NodeType, "#6");
  378. Assert.AreEqual ("xmlns", nl [0].LocalName);
  379. Assert.AreEqual ("xml", nl [1].LocalName);
  380. Assert.AreEqual ("xmlns", nl [2].LocalName);
  381. Assert.AreEqual ("xml", nl [3].LocalName);
  382. Assert.AreEqual ("xmlns", nl [4].LocalName);
  383. Assert.AreEqual ("xml", nl [5].LocalName);
  384. }
  385. [Test]
  386. [Ignore ("MS.NET has a bug; it does not return nodes in document order.")]
  387. public void SelectNodes2 ()
  388. {
  389. // This test is done in this class since it tests only XmlDocumentNavigator.
  390. string xpath = "//*|//@*|//namespace::*";
  391. XmlDocument doc = new XmlDocument ();
  392. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  393. XmlNodeList nl = doc.SelectNodes (xpath);
  394. Assert.AreEqual (9, nl.Count);
  395. Assert.AreEqual (XmlNodeType.Element, nl [0].NodeType);
  396. Assert.AreEqual (XmlNodeType.Attribute, nl [1].NodeType);
  397. Assert.AreEqual (XmlNodeType.Attribute, nl [2].NodeType);
  398. Assert.AreEqual (XmlNodeType.Element, nl [3].NodeType);
  399. Assert.AreEqual (XmlNodeType.Attribute, nl [4].NodeType);
  400. Assert.AreEqual (XmlNodeType.Attribute, nl [5].NodeType);
  401. Assert.AreEqual (XmlNodeType.Element, nl [6].NodeType);
  402. Assert.AreEqual (XmlNodeType.Attribute, nl [7].NodeType);
  403. Assert.AreEqual (XmlNodeType.Attribute, nl [8].NodeType);
  404. Assert.AreEqual ("element", nl [0].LocalName);
  405. Assert.AreEqual ("xmlns", nl [1].LocalName);
  406. Assert.AreEqual ("xml", nl [2].LocalName);
  407. Assert.AreEqual ("foo", nl [3].LocalName);
  408. Assert.AreEqual ("xmlns", nl [4].LocalName);
  409. Assert.AreEqual ("xml", nl [5].LocalName);
  410. Assert.AreEqual ("bar", nl [6].LocalName);
  411. Assert.AreEqual ("xmlns", nl [7].LocalName);
  412. Assert.AreEqual ("xml", nl [8].LocalName);
  413. }
  414. [Test]
  415. public void BaseURI ()
  416. {
  417. // See bug #64120.
  418. XmlDocument doc = new XmlDocument ();
  419. doc.Load ("Test/XmlFiles/simple.xml");
  420. XmlElement el = doc.CreateElement ("foo");
  421. Assert.AreEqual (String.Empty, el.BaseURI);
  422. doc.DocumentElement.AppendChild (el);
  423. Assert.IsTrue (String.Empty != el.BaseURI);
  424. XmlAttribute attr = doc.CreateAttribute ("attr");
  425. Assert.AreEqual (String.Empty, attr.BaseURI);
  426. }
  427. [Test]
  428. public void CloneReadonlyNode ()
  429. {
  430. // Clone() should return such node that is not readonly
  431. string dtd = "<!DOCTYPE root ["
  432. + "<!ELEMENT root (#PCDATA|foo)*>"
  433. + "<!ELEMENT foo EMPTY>"
  434. + "<!ENTITY ent1 '<foo /><![CDATA[cdata]]>'>]>";
  435. string xml = dtd + "<root>&ent1;</root>";
  436. XmlDocument doc = new XmlDocument ();
  437. doc.LoadXml (xml);
  438. XmlNode n = doc.DocumentElement.FirstChild.FirstChild;
  439. Assert.IsTrue (n.IsReadOnly, "#1");
  440. Assert.IsTrue (!n.CloneNode (true).IsReadOnly, "#2");
  441. }
  442. [Test] // bug #80233
  443. public void InnerTextComment ()
  444. {
  445. XmlDocument doc = new XmlDocument ();
  446. doc.LoadXml ("<a><!--xx--></a>");
  447. Assert.AreEqual (String.Empty, doc.InnerText);
  448. }
  449. [Test] // part of bug #80331
  450. public void AppendReferenceChildAsNewChild ()
  451. {
  452. XmlDocument d = new XmlDocument ();
  453. XmlElement r = d.CreateElement ("Docs");
  454. d.AppendChild (r);
  455. XmlElement s = Create (d, "param", "pattern");
  456. s.AppendChild (Create (d, "para", "insert text here"));
  457. r.AppendChild (s);
  458. r.AppendChild (Create (d, "param", "pattern"));
  459. r.AppendChild (Create (d, "param", "pattern"));
  460. r.InsertBefore (s, r.FirstChild);
  461. }
  462. XmlElement Create (XmlDocument d, string name, string param)
  463. {
  464. XmlElement e = d.CreateElement (name);
  465. e.SetAttribute ("name", param);
  466. return e;
  467. }
  468. [Test] // bug #80331
  469. public void PrependChild2 ()
  470. {
  471. XmlDocument d = new XmlDocument ();
  472. XmlElement r = d.CreateElement ("Docs");
  473. d.AppendChild (r);
  474. XmlElement s = Create (d, "param", "pattern");
  475. s.AppendChild (Create (d, "para", "insert text here"));
  476. r.AppendChild (s);
  477. r.AppendChild (Create (d, "param", "pattern"));
  478. r.AppendChild (Create (d, "param", "pattern"));
  479. r.PrependChild (s);
  480. }
  481. }
  482. }