5 // Jason Diamond (jason@injektilo.org)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
8 // (C) 2002 Jason Diamond http://injektilo.org/
9 // (C) 2003 Martin Willemoes Hansen
17 using NUnit
.Framework
;
19 namespace MonoTests
.System
.Xml
22 public class XmlElementTests
24 private XmlDocument document
;
27 public void GetReady ()
29 document
= new XmlDocument ();
32 private void AssertElement (XmlElement element
, string prefix
,
33 string localName
, string namespaceURI
,
36 Assert
.AreEqual (prefix
!= String
.Empty
? prefix
+ ":" + localName
: localName
, element
.Name
);
37 Assert
.AreEqual (prefix
, element
.Prefix
);
38 Assert
.AreEqual (localName
, element
.LocalName
);
39 Assert
.AreEqual (namespaceURI
, element
.NamespaceURI
);
40 //Assert.AreEqual (attributesCount, element.Attributes.Count);
43 // for NodeInserted Event
44 private bool Inserted
= false;
45 private void OnNodeInserted (object o
, XmlNodeChangedEventArgs e
)
50 // for NodeChanged Event
51 private bool Changed
= false;
52 private void OnNodeChanged (object o
, XmlNodeChangedEventArgs e
)
57 // for NodeRemoved Event
58 private bool Removed
= false;
59 private void OnNodeRemoved (object o
, XmlNodeChangedEventArgs e
)
65 public void CloneNode ()
67 XmlElement element
= document
.CreateElement ("foo");
68 XmlElement child
= document
.CreateElement ("bar");
69 XmlElement grandson
= document
.CreateElement ("baz");
71 element
.SetAttribute ("attr1", "val1");
72 element
.SetAttribute ("attr2", "val2");
73 element
.AppendChild (child
);
74 child
.SetAttribute ("attr3", "val3");
75 child
.AppendChild (grandson
);
77 document
.AppendChild (element
);
78 XmlNode deep
= element
.CloneNode (true);
79 // Assert.AreEqual (deep.OuterXml, element.OuterXml, "These should be the same");
80 Assert
.IsNull (deep
.ParentNode
, "This is not null");
81 Assert
.IsTrue (!Object
.ReferenceEquals (element
, deep
), "Copies, not pointers");
83 XmlNode shallow
= element
.CloneNode (false);
84 Assert
.IsNull (shallow
.ParentNode
, "This is not null");
85 Assert
.IsTrue (!Object
.ReferenceEquals (element
, shallow
), "Copies, not pointers");
86 Assert
.AreEqual (false, shallow
.HasChildNodes
, "Shallow clones shalt have no children!");
90 public void ConstructionAndDefaultAttributes ()
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 ();
97 Console
.WriteLine (doc
.DocumentElement
.Attributes
.Count
);
98 Console
.WriteLine (doc
.CreateElement ("root").Attributes
.Count
);
99 Console
.WriteLine (doc
.CreateElement ("root2").Attributes
.Count
);
103 public void CreateElement1 ()
105 XmlElement element
= document
.CreateElement ("name");
106 AssertElement (element
, String
.Empty
, "name", String
.Empty
, 0);
110 public void CreateElement1WithPrefix ()
112 XmlElement element
= document
.CreateElement ("prefix:localName");
113 AssertElement (element
, "prefix", "localName", String
.Empty
, 0);
117 public void CreateElement2 ()
119 XmlElement element
= document
.CreateElement ("qualifiedName", "namespaceURI");
120 AssertElement (element
, String
.Empty
, "qualifiedName",
125 public void CreateElement2WithPrefix ()
127 XmlElement element
= document
.CreateElement ("prefix:localName", "namespaceURI");
128 AssertElement (element
, "prefix", "localName", "namespaceURI", 0);
132 public void CreateElement3 ()
134 XmlElement element
= document
.CreateElement ("prefix", "localName", "namespaceURI");
135 AssertElement (element
, "prefix", "localName", "namespaceURI", 0);
139 public void CreateElement3WithNullNamespace ()
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);
147 public void InnerAndOuterXml ()
153 element
= document
.CreateElement ("foo");
154 Assert
.AreEqual (String
.Empty
, element
.InnerXml
);
155 Assert
.AreEqual ("<foo />", element
.OuterXml
);
157 text
= document
.CreateTextNode ("bar");
158 element
.AppendChild (text
);
159 Assert
.AreEqual ("bar", element
.InnerXml
);
160 Assert
.AreEqual ("<foo>bar</foo>", element
.OuterXml
);
162 element
.SetAttribute ("baz", "quux");
163 Assert
.AreEqual ("bar", element
.InnerXml
);
164 Assert
.AreEqual ("<foo baz=\"quux\">bar</foo>", element
.OuterXml
);
166 comment
= document
.CreateComment ("squonk");
167 element
.AppendChild (comment
);
168 Assert
.AreEqual ("bar<!--squonk-->", element
.InnerXml
);
169 Assert
.AreEqual ("<foo baz=\"quux\">bar<!--squonk--></foo>", element
.OuterXml
);
172 element
.AppendChild(document
.CreateElement("hoge"));
173 Assert
.AreEqual ("<hoge />", element
.InnerXml
);
177 public void SetGetAttribute ()
179 XmlElement element
= document
.CreateElement ("foo");
180 element
.SetAttribute ("attr1", "val1");
181 element
.SetAttribute ("attr2", "val2");
182 Assert
.AreEqual ("val1", element
.GetAttribute ("attr1"));
183 Assert
.AreEqual ("val2", element
.GetAttribute ("attr2"));
187 public void GetElementsByTagNameNoNameSpace ()
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>";
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 Assert
.AreEqual (4, bookList
.Count
, "GetElementsByTagName (string) returned incorrect count.");
208 public void GetElementsByTagNameUsingNameSpace ()
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>");
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 Assert
.AreEqual (1, bookList
.Count
, "GetElementsByTagName (string, uri) returned incorrect count.");
235 public void GetElementsByTagNameNs2 ()
237 document
.LoadXml (@"<root>
238 <x:a xmlns:x='urn:foo' id='a'>
239 <y:a xmlns:y='urn:foo' id='b'/>
243 <?a processing instruction ?>
247 // id='b' has different prefix. Should not caught by (name),
248 // while should caught by (name, ns).
249 XmlNodeList nl
= document
.DocumentElement
.GetElementsByTagName ("x:a");
250 Assert
.AreEqual (2, nl
.Count
);
251 Assert
.AreEqual ("a", nl
[0].Attributes
["id"].Value
);
252 Assert
.AreEqual ("c", nl
[1].Attributes
["id"].Value
);
254 nl
= document
.DocumentElement
.GetElementsByTagName ("a", "urn:foo");
255 Assert
.AreEqual (3, nl
.Count
);
256 Assert
.AreEqual ("a", nl
[0].Attributes
["id"].Value
);
257 Assert
.AreEqual ("b", nl
[1].Attributes
["id"].Value
);
258 Assert
.AreEqual ("c", nl
[2].Attributes
["id"].Value
);
261 nl
= document
.DocumentElement
.GetElementsByTagName ("*");
262 Assert
.AreEqual (5, nl
.Count
);
263 Assert
.AreEqual ("a", nl
[0].Attributes
["id"].Value
);
264 Assert
.AreEqual ("b", nl
[1].Attributes
["id"].Value
);
265 Assert
.AreEqual ("c", nl
[2].Attributes
["id"].Value
);
266 Assert
.AreEqual ("d", nl
[3].Attributes
["id"].Value
);
267 Assert
.AreEqual ("e", nl
[4].Attributes
["id"].Value
);
269 // wildcard - local and ns
270 nl
= document
.DocumentElement
.GetElementsByTagName ("*", "*");
271 Assert
.AreEqual (5, nl
.Count
);
272 Assert
.AreEqual ("a", nl
[0].Attributes
["id"].Value
);
273 Assert
.AreEqual ("b", nl
[1].Attributes
["id"].Value
);
274 Assert
.AreEqual ("c", nl
[2].Attributes
["id"].Value
);
275 Assert
.AreEqual ("d", nl
[3].Attributes
["id"].Value
);
276 Assert
.AreEqual ("e", nl
[4].Attributes
["id"].Value
);
278 // namespace wildcard - namespace
279 nl
= document
.DocumentElement
.GetElementsByTagName ("*", "urn:foo");
280 Assert
.AreEqual (4, nl
.Count
);
281 Assert
.AreEqual ("a", nl
[0].Attributes
["id"].Value
);
282 Assert
.AreEqual ("b", nl
[1].Attributes
["id"].Value
);
283 Assert
.AreEqual ("c", nl
[2].Attributes
["id"].Value
);
284 Assert
.AreEqual ("e", nl
[3].Attributes
["id"].Value
);
286 // namespace wildcard - local only. I dare say, such usage is not XML-ish!
287 nl
= document
.DocumentElement
.GetElementsByTagName ("a", "*");
288 Assert
.AreEqual (3, nl
.Count
);
289 Assert
.AreEqual ("a", nl
[0].Attributes
["id"].Value
);
290 Assert
.AreEqual ("b", nl
[1].Attributes
["id"].Value
);
291 Assert
.AreEqual ("c", nl
[2].Attributes
["id"].Value
);
295 public void OuterXmlWithNamespace ()
297 XmlElement element
= document
.CreateElement ("foo", "bar", "#foo");
298 Assert
.AreEqual ("<foo:bar xmlns:foo=\"#foo\" />", element
.OuterXml
);
302 public void RemoveAllAttributes ()
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>");
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 Assert
.AreEqual (false, xmlElement
.HasAttribute ("type"), "attributes not properly removed.");
320 [Ignore ("This test is very implementation dependent and thus .NET 2.0 does not pass. That's why I said http://primates.ximian.com/~atsushi/blog/archives/000416.html and http://svn.myrealbox.com/viewcvs/trunk/mono/web/xml-classes?rev=23598")]
321 public void RemoveDoesNotRemoveDefaultAttributes ()
323 string dtd
= "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root foo CDATA 'def' bar CDATA #IMPLIED>]>";
324 string xml
= dtd
+ "<root bar='baz' />";
325 XmlValidatingReader xvr
= new XmlValidatingReader (xml
, XmlNodeType
.Document
, null);
328 Assert
.IsNotNull (document
.DocumentElement
);
329 Assert
.AreEqual (2, document
.DocumentElement
.Attributes
.Count
, "attrCount #01");
330 Assert
.AreEqual ("baz", document
.DocumentElement
.GetAttribute ("bar"));
331 Assert
.AreEqual ("def", document
.DocumentElement
.GetAttribute ("foo"));
332 Assert
.AreEqual (false, document
.DocumentElement
.GetAttributeNode ("foo").Specified
);
333 document
.DocumentElement
.RemoveAll ();
334 Assert
.AreEqual (1, document
.DocumentElement
.Attributes
.Count
, "attrCount #02");
335 Assert
.AreEqual ("def", document
.DocumentElement
.GetAttribute ("foo"));
336 Assert
.AreEqual (String
.Empty
, document
.DocumentElement
.GetAttribute ("bar"));
338 // RemoveAllAttributes
339 xvr
= new XmlValidatingReader (xml
, XmlNodeType
.Document
, null);
341 document
.DocumentElement
.RemoveAllAttributes ();
342 Assert
.AreEqual (1, document
.DocumentElement
.Attributes
.Count
, "attrCount #03");
344 // RemoveAttribute(name)
345 xvr
= new XmlValidatingReader (xml
, XmlNodeType
.Document
, null);
347 document
.DocumentElement
.RemoveAttribute ("foo");
348 Assert
.AreEqual (2, document
.DocumentElement
.Attributes
.Count
, "attrCount #04");
350 // RemoveAttribute(name, ns)
351 xvr
= new XmlValidatingReader (xml
, XmlNodeType
.Document
, null);
353 document
.DocumentElement
.RemoveAttribute ("foo", String
.Empty
);
354 Assert
.AreEqual (2, document
.DocumentElement
.Attributes
.Count
, "attrCount #05");
357 xvr
= new XmlValidatingReader (xml
, XmlNodeType
.Document
, null);
359 document
.DocumentElement
.RemoveAttributeAt (1);
360 Assert
.AreEqual (2, document
.DocumentElement
.Attributes
.Count
, "attrCount #06");
362 // RemoveAttributeNode
363 xvr
= new XmlValidatingReader (xml
, XmlNodeType
.Document
, null);
365 document
.DocumentElement
.RemoveAttributeNode (document
.DocumentElement
.Attributes
[1]);
366 Assert
.AreEqual (2, document
.DocumentElement
.Attributes
.Count
, "attrCount #07");
368 // RemoveAttributeNode(name, ns)
369 xvr
= new XmlValidatingReader (xml
, XmlNodeType
.Document
, null);
371 document
.DocumentElement
.RemoveAttributeNode ("foo", String
.Empty
);
372 Assert
.AreEqual (2, document
.DocumentElement
.Attributes
.Count
, "attrCount #08");
376 public void SetAttributeNode ()
378 XmlDocument xmlDoc
= new XmlDocument ();
379 XmlElement xmlEl
= xmlDoc
.CreateElement ("TestElement");
380 XmlAttribute xmlAttribute
= xmlEl
.SetAttributeNode ("attr1", "namespace1");
381 XmlAttribute xmlAttribute2
= xmlEl
.SetAttributeNode ("attr2", "namespace2");
382 Assert
.AreEqual (true, xmlAttribute
.Name
.Equals ("attr1"), "attribute name not properly created.");
383 Assert
.AreEqual (true, xmlAttribute
.NamespaceURI
.Equals ("namespace1"), "attribute namespace not properly created.");
387 [ExpectedException (typeof (XmlException
))]
388 public void SetAttributeNodeError ()
390 XmlDocument doc
= new XmlDocument ();
391 doc
.LoadXml ("<root xmlns:x='urn:foo'/>");
392 doc
.DocumentElement
.SetAttributeNode ("x:lang", "urn:foo");
396 public void SetAttributeXmlns ()
398 // should not affect Element node's xmlns
399 XmlElement el
= document
.CreateElement ("root");
400 el
.SetAttribute ("xmlns", "urn:foo");
401 Assert
.AreEqual (String
.Empty
, el
.NamespaceURI
);
405 public void InnerTextAndEvent ()
407 XmlDocument doc
= new XmlDocument ();
408 doc
.LoadXml ("<root><child>text</child><child2><![CDATA[cdata]]></child2></root>");
409 doc
.NodeInserted
+= new XmlNodeChangedEventHandler (
411 doc
.NodeRemoved
+= new XmlNodeChangedEventHandler (
413 // If only one child of the element is Text node,
414 // then no events are fired.
415 doc
.DocumentElement
.FirstChild
.InnerText
= "no events fired.";
416 Assert
.AreEqual (false, Inserted
, "NoInsertEventFired");
417 Assert
.AreEqual (false, Removed
, "NoRemoveEventFired");
418 Assert
.AreEqual ("no events fired.", doc
.DocumentElement
.FirstChild
.InnerText
, "SetInnerTextToSingleText");
422 // if only one child of the element is CDataSection,
423 // then events are fired.
424 doc
.DocumentElement
.LastChild
.InnerText
= "events are fired.";
425 Assert
.AreEqual (true, Inserted
, "InsertedEventFired");
426 Assert
.AreEqual (true, Removed
, "RemovedEventFired");
427 Assert
.AreEqual ("events are fired.", doc
.DocumentElement
.LastChild
.InnerText
, "SetInnerTextToCDataSection");
431 public void InnerXmlSetter ()
433 XmlDocument doc
= new XmlDocument ();
434 doc
.LoadXml ("<root/>");
435 XmlElement el
= doc
.DocumentElement
;
436 Assert
.IsNull (el
.FirstChild
, "#Simple");
437 el
.InnerXml
= "<foo><bar att='baz'/></foo>";
438 XmlElement child
= el
.FirstChild
as XmlElement
;
439 Assert
.IsNotNull (child
, "#Simple.Child");
440 Assert
.AreEqual ("foo", child
.LocalName
, "#Simple.Child.Name");
442 XmlElement grandchild
= child
.FirstChild
as XmlElement
;
443 Assert
.IsNotNull (grandchild
, "#Simple.GrandChild");
444 Assert
.AreEqual ("bar", grandchild
.LocalName
, "#Simple.GrandChild.Name");
445 Assert
.AreEqual ("baz", grandchild
.GetAttribute ("att"), "#Simple.GrandChild.Attr");
447 doc
.LoadXml ("<root xmlns='NS0' xmlns:ns1='NS1'><foo/><ns1:bar/><ns2:bar xmlns:ns2='NS2' /></root>");
448 el
= doc
.DocumentElement
.FirstChild
.NextSibling
as XmlElement
; // ns1:bar
449 Assert
.IsNull (el
.FirstChild
, "#Namespaced.Prepare");
450 el
.InnerXml
= "<ns1:baz />";
451 Assert
.IsNotNull (el
.FirstChild
, "#Namespaced.Child");
452 Assert
.AreEqual ("baz", el
.FirstChild
.LocalName
, "#Namespaced.Child.Name");
453 Assert
.AreEqual ("NS1", el
.FirstChild
.NamespaceURI
, "#Namespaced.Child.NSURI"); // important!
455 el
.InnerXml
= "<hoge />";
456 Assert
.AreEqual ("hoge", el
.FirstChild
.Name
, "#Namespaced.VerifyPreviousCleared");
460 public void InnerXmlSetter2 ()
463 XmlDocument doc
= new XmlDocument ();
464 doc
.LoadXml (@"<type>QPair<QString,int>::
465 <ref refid='classQPair'>QPair</ref>
467 <ref refid='classQString'>QString</ref>
470 XmlElement typeNode
= doc
.DocumentElement
;
471 typeNode
.InnerText
= "QPair<QString, int>";
472 Assert
.AreEqual ("QPair<QString, int>", typeNode
.InnerText
);
476 public void IsEmpty ()
478 document
.LoadXml ("<root><foo/><bar></bar></root>");
479 Assert
.AreEqual (true, ((XmlElement
) document
.DocumentElement
.FirstChild
).IsEmpty
, "Empty");
480 Assert
.AreEqual (false, ((XmlElement
) document
.DocumentElement
.LastChild
).IsEmpty
, "Empty");
484 public void RemoveAttribute ()
486 string xlinkURI
= "http://www.w3.org/1999/XLink";
487 XmlDocument doc
= new XmlDocument ();
488 doc
.LoadXml ("<root a1='1' a2='2' xlink:href='urn:foo' xmlns:xlink='" + xlinkURI
+ "' />");
490 XmlElement el
= doc
.DocumentElement
;
491 el
.RemoveAttribute ("a1");
492 Assert
.IsNull (el
.GetAttributeNode ("a1"), "RemoveAttribute");
493 el
.RemoveAttribute ("xlink:href");
494 Assert
.IsNull (el
.GetAttributeNode ("href", xlinkURI
), "RemoveAttribute");
495 el
.RemoveAllAttributes ();
496 Assert
.IsNull (el
.GetAttributeNode ("a2"), "RemoveAllAttributes");
500 public void WriteToWithDefaultNamespace ()
502 XmlDocument doc
= new XmlDocument ();
503 doc
.LoadXml ("<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />");
504 StringWriter sw
= new StringWriter ();
505 XmlTextWriter xtw
= new XmlTextWriter (sw
);
506 doc
.DocumentElement
.WriteTo (xtw
);
507 Assert
.AreEqual ("<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", sw
.ToString());
511 public void WriteToMakesNonsenseForDefaultNSChildren ()
513 XmlDocument d
= new XmlDocument ();
514 XmlElement x
= d
.CreateElement ("root");
516 XmlElement a
= d
.CreateElement ("a");
517 XmlElement b
= d
.CreateElement ("b");
518 b
.SetAttribute ("xmlns","probe");
521 XmlElement b2
= d
.CreateElement ("p2", "b2", "");
523 Assert
.AreEqual ("<root><a /><b xmlns=\"probe\"><b2 /></b></root>", d
.OuterXml
);
527 public void WriteToWithDeletedNamespacePrefix ()
529 XmlDocument doc
= new XmlDocument ();
530 doc
.LoadXml ("<root xmlns:foo='urn:dummy'><foo foo:bar='baz' /></root>");
531 doc
.DocumentElement
.RemoveAllAttributes ();
533 Assert
.IsTrue (doc
.DocumentElement
.FirstChild
.OuterXml
.IndexOf("xmlns:foo") > 0);
537 public void WriteToWithDifferentNamespaceAttributes ()
539 XmlDocument doc
= new XmlDocument ();
540 doc
.LoadXml ("<root xmlns:foo='urn:dummy' xmlns:html='http://www.w3.org/1999/xhtml' html:style='font-size: 1em'></root>");
541 Assert
.IsTrue (doc
.OuterXml
.IndexOf ("xmlns:html=\"http://www.w3.org/1999/xhtml\"") > 0);
545 public void WriteToDefaultAttribute ()
547 // default attributes should be ignored.
549 string dtd
= "<!DOCTYPE root[<!ATTLIST root hoge CDATA 'hoge-def'><!ENTITY foo 'ent-foo'>]>";
550 string xml
= dtd
+ "<root>&foo;</root>";
551 XmlValidatingReader xvr
= new XmlValidatingReader (xml
, XmlNodeType
.Document
,null);
552 xvr
.EntityHandling
= EntityHandling
.ExpandCharEntities
;
553 xvr
.ValidationType
= ValidationType
.None
;
555 StringWriter sw
= new StringWriter ();
556 XmlTextWriter xtw
= new XmlTextWriter (sw
);
557 document
.DocumentElement
.WriteTo (xtw
);
558 Assert
.AreEqual ("<root>&foo;</root>", sw
.ToString ());
562 public void SetNullPrefix ()
564 XmlDocument doc
= new XmlDocument ();
565 doc
.LoadXml ("<root/>");
566 doc
.DocumentElement
.Prefix
= null;
568 Assert
.AreEqual (string.Empty
, doc
.DocumentElement
.Prefix
, "#1");
569 AssertClearPrefix ((string) null);
573 public void SetEmptyStringPrefix ()
575 XmlDocument doc
= new XmlDocument ();
576 doc
.LoadXml ("<root />");
577 doc
.DocumentElement
.Prefix
= String
.Empty
;
578 Assert
.AreEqual (string.Empty
, doc
.DocumentElement
.Prefix
, "#1");
580 AssertClearPrefix (string.Empty
);
584 private void AssertClearPrefix (string newPrefix
)
586 XmlDocument doc
= new XmlDocument ();
587 doc
.LoadXml ("<x:root xmlns:x=\"http://somenamespace.com\" />");
588 Assert
.AreEqual ("<x:root xmlns:x=\"http://somenamespace.com\" />", doc
.OuterXml
, "#Clear1");
589 Assert
.AreEqual ("<x:root xmlns:x=\"http://somenamespace.com\" />", doc
.DocumentElement
.OuterXml
, "#Clear2");
590 Assert
.AreEqual ("x", doc
.DocumentElement
.Prefix
, "#Clear3");
591 doc
.DocumentElement
.Prefix
= newPrefix
;
592 Assert
.AreEqual ("<root xmlns:x=\"http://somenamespace.com\" xmlns=\"http://somenamespace.com\" />", doc
.OuterXml
, "#Clear4");
593 Assert
.AreEqual ("<root xmlns:x=\"http://somenamespace.com\" xmlns=\"http://somenamespace.com\" />", doc
.DocumentElement
.OuterXml
, "#Clear5");
594 Assert
.AreEqual (string.Empty
, doc
.DocumentElement
.Prefix
, "#Clear6");
598 public void NullPrefix ()
600 new MyXmlElement ("foo", "urn:foo", new XmlDocument ());
603 [Test
] // bug #380720
604 [Category ("Networking")]
605 [Ignore ("The server takes too much time to respond")]
606 public void SetAttributeWithIdentity ()
608 XmlDocument doc
= new XmlDocument ();
609 doc
.LoadXml (@"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd' []>
610 <html xmlns='http://www.w3.org/1999/xhtml'>
612 <body><div id='xxx'>XXX</div><div id='yyy'>YYY</div></body>
614 XmlElement xxx
= (XmlElement
) doc
.GetElementsByTagName ("div") [0];
615 XmlElement yyy
= (XmlElement
) doc
.GetElementsByTagName ("div") [1];
616 yyy
.ParentNode
.RemoveChild (yyy
);
617 yyy
.SetAttribute ("id", "xxx");
621 public void SetAttributeExistingNoInsert () // bug #464394
623 XmlDocument doc
= new XmlDocument ();
624 bool changed
= false;
625 doc
.LoadXml (@"<MyNode Key='ABC' ClientName='xxx' DateIssued='yyy' />");
626 doc
.NodeChanged
+= delegate {
629 doc
.DocumentElement
.SetAttribute ("Key", "");
630 Assert
.IsTrue (changed
);
633 class MyXmlElement
: XmlElement
635 public MyXmlElement (string localName
, string ns
, XmlDocument doc
)
636 : base (null, localName
, ns
, doc
)