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