| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925 |
- // System.Xml.XmlDocumentTests
- //
- // Authors:
- // Jason Diamond <[email protected]>
- // Kral Ferch <[email protected]>
- // Martin Willemoes Hansen <[email protected]>
- //
- // (C) 2002 Jason Diamond, Kral Ferch
- // (C) 2003 Martin Willemoes Hansen
- //
- using System;
- using System.Collections;
- using System.Xml;
- using System.IO;
- using System.Text;
- using NUnit.Framework;
- namespace MonoTests.System.Xml
- {
- [TestFixture]
- public class XmlDocumentTests
- {
- private XmlDocument document;
- private ArrayList eventStrings = new ArrayList();
- // These Event* methods support the TestEventNode* Tests in this file.
- // Most of them are event handlers for the XmlNodeChangedEventHandler
- // delegate.
- private void EventStringAdd(string eventName, XmlNodeChangedEventArgs e)
- {
- string oldParent = (e.OldParent != null) ? e.OldParent.Name : "<none>";
- string newParent = (e.NewParent != null) ? e.NewParent.Name : "<none>";
- eventStrings.Add (String.Format ("{0}, {1}, {2}, {3}, {4}", eventName, e.Action.ToString (), e.Node.OuterXml, oldParent, newParent));
- }
- private void EventNodeChanged(Object sender, XmlNodeChangedEventArgs e)
- {
- EventStringAdd ("NodeChanged", e);
- }
- private void EventNodeChanging (Object sender, XmlNodeChangedEventArgs e)
- {
- EventStringAdd ("NodeChanging", e);
- }
- private void EventNodeChangingException (Object sender, XmlNodeChangedEventArgs e)
- {
- throw new Exception ("don't change the value.");
- }
- private void EventNodeInserted(Object sender, XmlNodeChangedEventArgs e)
- {
- EventStringAdd ("NodeInserted", e);
- }
- private void EventNodeInserting(Object sender, XmlNodeChangedEventArgs e)
- {
- EventStringAdd ("NodeInserting", e);
- }
- private void EventNodeInsertingException(Object sender, XmlNodeChangedEventArgs e)
- {
- throw new Exception ("don't insert the element.");
- }
- private void EventNodeRemoved(Object sender, XmlNodeChangedEventArgs e)
- {
- EventStringAdd ("NodeRemoved", e);
- }
- private void EventNodeRemoving(Object sender, XmlNodeChangedEventArgs e)
- {
- EventStringAdd ("NodeRemoving", e);
- }
- private void EventNodeRemovingException(Object sender, XmlNodeChangedEventArgs e)
- {
- throw new Exception ("don't remove the element.");
- }
- [SetUp]
- public void GetReady ()
- {
- document = new XmlDocument ();
- document.PreserveWhitespace = true;
- }
- [Test]
- public void CreateNodeNodeTypeNameEmptyParams ()
- {
- XmlNode node;
- try {
- node = document.CreateNode (null, null, null);
- Assertion.Fail ("Expected an ArgumentException to be thrown.");
- } catch (ArgumentException) {}
- try {
- node = document.CreateNode ("attribute", null, null);
- Assertion.Fail ("Expected a NullReferenceException to be thrown.");
- } catch (NullReferenceException) {}
- try {
- node = document.CreateNode ("attribute", "", null);
- Assertion.Fail ("Expected an ArgumentException to be thrown.");
- } catch (ArgumentException) {}
- try {
- node = document.CreateNode ("element", null, null);
- Assertion.Fail ("Expected a NullReferenceException to be thrown.");
- } catch (NullReferenceException) {}
- try {
- node = document.CreateNode ("element", "", null);
- Assertion.Fail ("Expected an ArgumentException to be thrown.");
- } catch (ArgumentException) {}
- try {
- node = document.CreateNode ("entityreference", null, null);
- Assertion.Fail ("Expected a NullReferenceException to be thrown.");
- } catch (NullReferenceException) {}
- }
- [Test]
- public void CreateNodeInvalidXmlNodeType ()
- {
- XmlNode node;
- try {
- node = document.CreateNode (XmlNodeType.EndElement, null, null);
- Assertion.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
- } catch (ArgumentOutOfRangeException) {}
- try {
- node = document.CreateNode (XmlNodeType.EndEntity, null, null);
- Assertion.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
- } catch (ArgumentOutOfRangeException) {}
- try {
- node = document.CreateNode (XmlNodeType.Entity, null, null);
- Assertion.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
- } catch (ArgumentOutOfRangeException) {}
- try {
- node = document.CreateNode (XmlNodeType.None, null, null);
- Assertion.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
- } catch (ArgumentOutOfRangeException) {}
- try {
- node = document.CreateNode (XmlNodeType.Notation, null, null);
- Assertion.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
- } catch (ArgumentOutOfRangeException) {}
- // TODO: undocumented allowable type.
- node = document.CreateNode (XmlNodeType.XmlDeclaration, null, null);
- Assertion.AssertEquals (XmlNodeType.XmlDeclaration, node.NodeType);
- }
- [Test]
- public void CreateNodeWhichParamIsUsed ()
- {
- XmlNode node;
- // No constructor params for Document, DocumentFragment.
- node = document.CreateNode (XmlNodeType.CDATA, "a", "b", "c");
- Assertion.AssertEquals (String.Empty, ((XmlCDataSection)node).Value);
- node = document.CreateNode (XmlNodeType.Comment, "a", "b", "c");
- Assertion.AssertEquals (String.Empty, ((XmlComment)node).Value);
- node = document.CreateNode (XmlNodeType.DocumentType, "a", "b", "c");
- Assertion.AssertNull (((XmlDocumentType)node).Value);
- // TODO: add this back in to test when it's implemented.
- // node = document.CreateNode (XmlNodeType.EntityReference, "a", "b", "c");
- // Assertion.AssertNull (((XmlEntityReference)node).Value);
- node = document.CreateNode (XmlNodeType.ProcessingInstruction, "a", "b", "c");
- Assertion.AssertEquals (String.Empty, ((XmlProcessingInstruction)node).Value);
- node = document.CreateNode (XmlNodeType.SignificantWhitespace, "a", "b", "c");
- Assertion.AssertEquals (String.Empty, ((XmlSignificantWhitespace)node).Value);
- node = document.CreateNode (XmlNodeType.Text, "a", "b", "c");
- Assertion.AssertEquals (String.Empty, ((XmlText)node).Value);
- node = document.CreateNode (XmlNodeType.Whitespace, "a", "b", "c");
- Assertion.AssertEquals (String.Empty, ((XmlWhitespace)node).Value);
- node = document.CreateNode (XmlNodeType.XmlDeclaration, "a", "b", "c");
- Assertion.AssertEquals ("version=\"1.0\"", ((XmlDeclaration)node).Value);
- }
- [Test]
- public void CreateNodeNodeTypeName ()
- {
- XmlNode node;
- try {
- node = document.CreateNode ("foo", null, null);
- Assertion.Fail ("Expected an ArgumentException to be thrown.");
- } catch (ArgumentException) {}
- node = document.CreateNode("attribute", "foo", null);
- Assertion.AssertEquals (XmlNodeType.Attribute, node.NodeType);
- node = document.CreateNode("cdatasection", null, null);
- Assertion.AssertEquals (XmlNodeType.CDATA, node.NodeType);
- node = document.CreateNode("comment", null, null);
- Assertion.AssertEquals (XmlNodeType.Comment, node.NodeType);
- node = document.CreateNode("document", null, null);
- Assertion.AssertEquals (XmlNodeType.Document, node.NodeType);
- // TODO: test which constructor this ended up calling,
- // i.e. reuse underlying NameTable or not?
- // TODO: add this back in to test when it's implemented.
- // node = document.CreateNode("documentfragment", null, null);
- // Assertion.AssertEquals (XmlNodeType.DocumentFragment, node.NodeType);
- node = document.CreateNode("documenttype", null, null);
- Assertion.AssertEquals (XmlNodeType.DocumentType, node.NodeType);
- node = document.CreateNode("element", "foo", null);
- Assertion.AssertEquals (XmlNodeType.Element, node.NodeType);
- // TODO: add this back in to test when it's implemented.
- // node = document.CreateNode("entityreference", "foo", null);
- // Assertion.AssertEquals (XmlNodeType.EntityReference, node.NodeType);
- node = document.CreateNode("processinginstruction", null, null);
- Assertion.AssertEquals (XmlNodeType.ProcessingInstruction, node.NodeType);
- node = document.CreateNode("significantwhitespace", null, null);
- Assertion.AssertEquals (XmlNodeType.SignificantWhitespace, node.NodeType);
- node = document.CreateNode("text", null, null);
- Assertion.AssertEquals (XmlNodeType.Text, node.NodeType);
- node = document.CreateNode("whitespace", null, null);
- Assertion.AssertEquals (XmlNodeType.Whitespace, node.NodeType);
- }
- [Test]
- public void DocumentElement ()
- {
- Assertion.AssertNull (document.DocumentElement);
- XmlElement element = document.CreateElement ("foo", "bar", "http://foo/");
- Assertion.AssertNotNull (element);
- Assertion.AssertEquals ("foo", element.Prefix);
- Assertion.AssertEquals ("bar", element.LocalName);
- Assertion.AssertEquals ("http://foo/", element.NamespaceURI);
- Assertion.AssertEquals ("foo:bar", element.Name);
- Assertion.AssertSame (element, document.AppendChild (element));
- Assertion.AssertSame (element, document.DocumentElement);
- }
- [Test]
- public void DocumentEmpty()
- {
- Assertion.AssertEquals ("Incorrect output for empty document.", "", document.OuterXml);
- }
- [Test]
- public void EventNodeChanged()
- {
- XmlElement element;
- XmlComment comment;
- document.NodeChanged += new XmlNodeChangedEventHandler (this.EventNodeChanged);
- // Node that is part of the document.
- document.AppendChild (document.CreateElement ("foo"));
- comment = document.CreateComment ("bar");
- document.DocumentElement.AppendChild (comment);
- Assertion.AssertEquals ("<!--bar-->", document.DocumentElement.InnerXml);
- comment.Value = "baz";
- Assertion.Assert (eventStrings.Contains ("NodeChanged, Change, <!--baz-->, foo, foo"));
- Assertion.AssertEquals ("<!--baz-->", document.DocumentElement.InnerXml);
- // Node that isn't part of the document but created by the document.
- element = document.CreateElement ("foo");
- comment = document.CreateComment ("bar");
- element.AppendChild (comment);
- Assertion.AssertEquals ("<!--bar-->", element.InnerXml);
- comment.Value = "baz";
- Assertion.Assert (eventStrings.Contains ("NodeChanged, Change, <!--baz-->, foo, foo"));
- Assertion.AssertEquals ("<!--baz-->", element.InnerXml);
- /*
- TODO: Insert this when XmlNode.InnerText() and XmlNode.InnerXml() have been implemented.
-
- // Node that is part of the document.
- element = document.CreateElement ("foo");
- element.InnerText = "bar";
- document.AppendChild(element);
- element.InnerText = "baz";
- Assertion.Assert(eventStrings.Contains("NodeChanged, Change, baz, foo, foo"));
-
- // Node that isn't part of the document but created by the document.
- element = document.CreateElement("qux");
- element.InnerText = "quux";
- element.InnerText = "quuux";
- Assertion.Assert(eventStrings.Contains("NodeChanged, Change, quuux, qux, qux"));
- */
- }
- [Test]
- public void EventNodeChanging()
- {
- XmlElement element;
- XmlComment comment;
- document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChanging);
- // Node that is part of the document.
- document.AppendChild (document.CreateElement ("foo"));
- comment = document.CreateComment ("bar");
- document.DocumentElement.AppendChild (comment);
- Assertion.AssertEquals ("<!--bar-->", document.DocumentElement.InnerXml);
- comment.Value = "baz";
- Assertion.Assert (eventStrings.Contains ("NodeChanging, Change, <!--bar-->, foo, foo"));
- Assertion.AssertEquals ("<!--baz-->", document.DocumentElement.InnerXml);
- // Node that isn't part of the document but created by the document.
- element = document.CreateElement ("foo");
- comment = document.CreateComment ("bar");
- element.AppendChild (comment);
- Assertion.AssertEquals ("<!--bar-->", element.InnerXml);
- comment.Value = "baz";
- Assertion.Assert (eventStrings.Contains ("NodeChanging, Change, <!--bar-->, foo, foo"));
- Assertion.AssertEquals ("<!--baz-->", element.InnerXml);
- // If an exception is thrown the Document returns to original state.
- document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChangingException);
- element = document.CreateElement("foo");
- comment = document.CreateComment ("bar");
- element.AppendChild (comment);
- Assertion.AssertEquals ("<!--bar-->", element.InnerXml);
- try
- {
- comment.Value = "baz";
- Assertion.Fail("Expected an exception to be thrown by the NodeChanging event handler method EventNodeChangingException().");
- } catch (Exception) {}
- Assertion.AssertEquals ("<!--bar-->", element.InnerXml);
- // Yes it's a bit anal but this tests whether the node changing event exception fires before the
- // ArgumentOutOfRangeException. Turns out it does so that means our implementation needs to raise
- // the node changing event before doing any work.
- try
- {
- comment.ReplaceData(-1, 0, "qux");
- Assertion.Fail("Expected an ArgumentOutOfRangeException to be thrown.");
- }
- catch (Exception) {}
- /*
- TODO: Insert this when XmlNode.InnerText() and XmlNode.InnerXml() have been implemented.
-
- // Node that is part of the document.
- element = document.CreateElement ("foo");
- element.InnerText = "bar";
- document.AppendChild(element);
- element.InnerText = "baz";
- Assertion.Assert(eventStrings.Contains("NodeChanging, Change, bar, foo, foo"));
- // Node that isn't part of the document but created by the document.
- element = document.CreateElement("foo");
- element.InnerText = "bar";
- element.InnerText = "baz";
- Assertion.Assert(eventStrings.Contains("NodeChanging, Change, bar, foo, foo"));
- // If an exception is thrown the Document returns to original state.
- document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChangingException);
- element = document.CreateElement("foo");
- element.InnerText = "bar";
- try {
- element.InnerText = "baz";
- Assertion.Fail("Expected an exception to be thrown by the NodeChanging event handler method EventNodeChangingException().");
- } catch (Exception) {}
- Assertion.AssertEquals("bar", element.InnerText);
- */
- }
- [Test]
- public void EventNodeInserted()
- {
- XmlElement element;
- document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
- // Inserted 'foo' element to the document.
- element = document.CreateElement ("foo");
- document.AppendChild (element);
- Assertion.Assert (eventStrings.Contains ("NodeInserted, Insert, <foo />, <none>, #document"));
- // Append child on node in document
- element = document.CreateElement ("foo");
- document.DocumentElement.AppendChild (element);
- Assertion.Assert (eventStrings.Contains ("NodeInserted, Insert, <foo />, <none>, foo"));
- // Append child on node not in document but created by document
- element = document.CreateElement ("bar");
- element.AppendChild(document.CreateElement ("bar"));
- Assertion.Assert(eventStrings.Contains("NodeInserted, Insert, <bar />, <none>, bar"));
- }
- [Test]
- public void EventNodeInserting()
- {
- XmlElement element;
- document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInserting);
- // Inserting 'foo' element to the document.
- element = document.CreateElement ("foo");
- document.AppendChild (element);
- Assertion.Assert (eventStrings.Contains ("NodeInserting, Insert, <foo />, <none>, #document"));
- // Append child on node in document
- element = document.CreateElement ("foo");
- document.DocumentElement.AppendChild (element);
- Assertion.Assert(eventStrings.Contains ("NodeInserting, Insert, <foo />, <none>, foo"));
- // Append child on node not in document but created by document
- element = document.CreateElement ("bar");
- Assertion.AssertEquals (0, element.ChildNodes.Count);
- element.AppendChild (document.CreateElement ("bar"));
- Assertion.Assert (eventStrings.Contains ("NodeInserting, Insert, <bar />, <none>, bar"));
- Assertion.AssertEquals (1, element.ChildNodes.Count);
- // If an exception is thrown the Document returns to original state.
- document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInsertingException);
- Assertion.AssertEquals (1, element.ChildNodes.Count);
- try
- {
- element.AppendChild (document.CreateElement("baz"));
- Assertion.Fail ("Expected an exception to be thrown by the NodeInserting event handler method EventNodeInsertingException().");
- }
- catch (Exception) {}
- Assertion.AssertEquals (1, element.ChildNodes.Count);
- }
- [Test]
- public void EventNodeRemoved()
- {
- XmlElement element;
- XmlElement element2;
- document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
- // Removed 'bar' element from 'foo' outside document.
- element = document.CreateElement ("foo");
- element2 = document.CreateElement ("bar");
- element.AppendChild (element2);
- Assertion.AssertEquals (1, element.ChildNodes.Count);
- element.RemoveChild (element2);
- Assertion.Assert (eventStrings.Contains ("NodeRemoved, Remove, <bar />, foo, <none>"));
- Assertion.AssertEquals (0, element.ChildNodes.Count);
- /*
- * TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
- // RemoveAll.
- element = document.CreateElement ("foo");
- element2 = document.CreateElement ("bar");
- element.AppendChild(element2);
- Assertion.AssertEquals(1, element.ChildNodes.Count);
- element.RemoveAll();
- Assertion.Assert (eventStrings.Contains ("NodeRemoved, Remove, <bar />, foo, <none>"));
- Assertion.AssertEquals(0, element.ChildNodes.Count);
- */
- // Removed 'bar' element from 'foo' inside document.
- element = document.CreateElement ("foo");
- document.AppendChild (element);
- element = document.CreateElement ("bar");
- document.DocumentElement.AppendChild (element);
- Assertion.AssertEquals (1, document.DocumentElement.ChildNodes.Count);
- document.DocumentElement.RemoveChild (element);
- Assertion.Assert (eventStrings.Contains ("NodeRemoved, Remove, <bar />, foo, <none>"));
- Assertion.AssertEquals (0, document.DocumentElement.ChildNodes.Count);
- }
-
- [Test]
- public void EventNodeRemoving()
- {
- XmlElement element;
- XmlElement element2;
- document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemoving);
- // Removing 'bar' element from 'foo' outside document.
- element = document.CreateElement ("foo");
- element2 = document.CreateElement ("bar");
- element.AppendChild (element2);
- Assertion.AssertEquals (1, element.ChildNodes.Count);
- element.RemoveChild (element2);
- Assertion.Assert (eventStrings.Contains ("NodeRemoving, Remove, <bar />, foo, <none>"));
- Assertion.AssertEquals (0, element.ChildNodes.Count);
- /*
- * TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
- // RemoveAll.
- element = document.CreateElement ("foo");
- element2 = document.CreateElement ("bar");
- element.AppendChild(element2);
- Assertion.AssertEquals(1, element.ChildNodes.Count);
- element.RemoveAll();
- Assertion.Assert (eventStrings.Contains ("NodeRemoving, Remove, <bar />, foo, <none>"));
- Assertion.AssertEquals(0, element.ChildNodes.Count);
- */
- // Removing 'bar' element from 'foo' inside document.
- element = document.CreateElement ("foo");
- document.AppendChild (element);
- element = document.CreateElement ("bar");
- document.DocumentElement.AppendChild (element);
- Assertion.AssertEquals (1, document.DocumentElement.ChildNodes.Count);
- document.DocumentElement.RemoveChild (element);
- Assertion.Assert (eventStrings.Contains ("NodeRemoving, Remove, <bar />, foo, <none>"));
- Assertion.AssertEquals (0, document.DocumentElement.ChildNodes.Count);
- // If an exception is thrown the Document returns to original state.
- document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemovingException);
- element.AppendChild (element2);
- Assertion.AssertEquals (1, element.ChildNodes.Count);
- try
- {
- element.RemoveChild(element2);
- Assertion.Fail ("Expected an exception to be thrown by the NodeRemoving event handler method EventNodeRemovingException().");
- }
- catch (Exception) {}
- Assertion.AssertEquals (1, element.ChildNodes.Count);
- }
- [Test]
- public void GetElementsByTagNameNoNameSpace ()
- {
- string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
- <price>34.95</price></book><book><title>Bear and the Dragon</title>
- <author>Tom Clancy</author><price>6.95</price></book><book>
- <title>Bourne Identity</title><author>Robert Ludlum</author>
- <price>9.95</price></book><Fluffer><Nutter><book>
- <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
- <price>9.95</price></book></Nutter></Fluffer></library>";
- MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
- document = new XmlDocument ();
- document.Load (memoryStream);
- XmlNodeList bookList = document.GetElementsByTagName ("book");
- Assertion.AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
- }
- [Test]
- public void GetElementsByTagNameUsingNameSpace ()
- {
- StringBuilder xml = new StringBuilder ();
- xml.Append ("<?xml version=\"1.0\" ?><library xmlns:North=\"http://www.foo.com\" ");
- xml.Append ("xmlns:South=\"http://www.goo.com\"><North:book type=\"non-fiction\"> ");
- xml.Append ("<North:title type=\"intro\">XML Fun</North:title> " );
- xml.Append ("<North:author>John Doe</North:author> " );
- xml.Append ("<North:price>34.95</North:price></North:book> " );
- xml.Append ("<South:book type=\"fiction\"> " );
- xml.Append ("<South:title>Bear and the Dragon</South:title> " );
- xml.Append ("<South:author>Tom Clancy</South:author> " );
- xml.Append ("<South:price>6.95</South:price></South:book> " );
- xml.Append ("<South:book type=\"fiction\"><South:title>Bourne Identity</South:title> " );
- xml.Append ("<South:author>Robert Ludlum</South:author> " );
- xml.Append ("<South:price>9.95</South:price></South:book></library>");
- MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
- document = new XmlDocument ();
- document.Load (memoryStream);
- XmlNodeList bookList = document.GetElementsByTagName ("book", "http://www.goo.com");
- Assertion.AssertEquals ("GetElementsByTagName (string, uri) returned incorrect count.", 2, bookList.Count);
- }
- [Test]
- public void InnerAndOuterXml ()
- {
- Assertion.AssertEquals (String.Empty, document.InnerXml);
- Assertion.AssertEquals (document.InnerXml, document.OuterXml);
- XmlDeclaration declaration = document.CreateXmlDeclaration ("1.0", null, null);
- document.AppendChild (declaration);
- Assertion.AssertEquals ("<?xml version=\"1.0\"?>", document.InnerXml);
- Assertion.AssertEquals (document.InnerXml, document.OuterXml);
- XmlElement element = document.CreateElement ("foo");
- document.AppendChild (element);
- Assertion.AssertEquals ("<?xml version=\"1.0\"?><foo />", document.InnerXml);
- Assertion.AssertEquals (document.InnerXml, document.OuterXml);
- XmlComment comment = document.CreateComment ("bar");
- document.DocumentElement.AppendChild (comment);
- Assertion.AssertEquals ("<?xml version=\"1.0\"?><foo><!--bar--></foo>", document.InnerXml);
- Assertion.AssertEquals (document.InnerXml, document.OuterXml);
- XmlText text = document.CreateTextNode ("baz");
- document.DocumentElement.AppendChild (text);
- Assertion.AssertEquals ("<?xml version=\"1.0\"?><foo><!--bar-->baz</foo>", document.InnerXml);
- Assertion.AssertEquals (document.InnerXml, document.OuterXml);
- element = document.CreateElement ("quux");
- element.SetAttribute ("quuux", "squonk");
- document.DocumentElement.AppendChild (element);
- Assertion.AssertEquals ("<?xml version=\"1.0\"?><foo><!--bar-->baz<quux quuux=\"squonk\" /></foo>", document.InnerXml);
- Assertion.AssertEquals (document.InnerXml, document.OuterXml);
- }
- [Test]
- public void LoadWithSystemIOStream ()
- {
- string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
- <price>34.95</price></book><book><title>Bear and the Dragon</title>
- <author>Tom Clancy</author><price>6.95</price></book><book>
- <title>Bourne Identity</title><author>Robert Ludlum</author>
- <price>9.95</price></book><Fluffer><Nutter><book>
- <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
- <price>9.95</price></book></Nutter></Fluffer></library>";
- MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
- document = new XmlDocument ();
- document.Load (memoryStream);
- Assertion.AssertEquals ("Not Loaded From IOStream", true, document.HasChildNodes);
- }
- [Test]
- public void LoadXmlCDATA ()
- {
- document.LoadXml ("<foo><![CDATA[bar]]></foo>");
- Assertion.Assert (document.DocumentElement.FirstChild.NodeType == XmlNodeType.CDATA);
- Assertion.AssertEquals ("bar", document.DocumentElement.FirstChild.Value);
- }
- [Test]
- public void LoadXMLComment()
- {
- // XmlTextReader needs to throw this exception
- // try {
- // document.LoadXml("<!--foo-->");
- // Assertion.Fail("XmlException should have been thrown.");
- // }
- // catch (XmlException e) {
- // Assertion.AssertEquals("Exception message doesn't match.", "The root element is missing.", e.Message);
- // }
- document.LoadXml ("<foo><!--Comment--></foo>");
- Assertion.Assert (document.DocumentElement.FirstChild.NodeType == XmlNodeType.Comment);
- Assertion.AssertEquals ("Comment", document.DocumentElement.FirstChild.Value);
- document.LoadXml (@"<foo><!--bar--></foo>");
- Assertion.AssertEquals ("Incorrect target.", "bar", ((XmlComment)document.FirstChild.FirstChild).Data);
- }
- [Test]
- public void LoadXmlElementSingle ()
- {
- Assertion.AssertNull (document.DocumentElement);
- document.LoadXml ("<foo/>");
- Assertion.AssertNotNull (document.DocumentElement);
- Assertion.AssertSame (document.FirstChild, document.DocumentElement);
- Assertion.AssertEquals (String.Empty, document.DocumentElement.Prefix);
- Assertion.AssertEquals ("foo", document.DocumentElement.LocalName);
- Assertion.AssertEquals (String.Empty, document.DocumentElement.NamespaceURI);
- Assertion.AssertEquals ("foo", document.DocumentElement.Name);
- }
- [Test]
- public void LoadXmlElementWithAttributes ()
- {
- Assertion.AssertNull (document.DocumentElement);
- document.LoadXml ("<foo bar='baz' quux='quuux' hoge='hello & world' />");
- XmlElement documentElement = document.DocumentElement;
- Assertion.AssertEquals ("baz", documentElement.GetAttribute ("bar"));
- Assertion.AssertEquals ("quuux", documentElement.GetAttribute ("quux"));
- Assertion.AssertEquals ("hello & world", documentElement.GetAttribute ("hoge"));
- Assertion.AssertEquals ("hello & world", documentElement.Attributes ["hoge"].Value);
- Assertion.AssertEquals (1, documentElement.GetAttributeNode ("hoge").ChildNodes.Count);
- }
- [Test]
- public void LoadXmlElementWithChildElement ()
- {
- document.LoadXml ("<foo><bar/></foo>");
- Assertion.Assert (document.ChildNodes.Count == 1);
- Assertion.Assert (document.FirstChild.ChildNodes.Count == 1);
- Assertion.AssertEquals ("foo", document.DocumentElement.LocalName);
- Assertion.AssertEquals ("bar", document.DocumentElement.FirstChild.LocalName);
- }
- [Test]
- public void LoadXmlElementWithTextNode ()
- {
- document.LoadXml ("<foo>bar</foo>");
- Assertion.Assert (document.DocumentElement.FirstChild.NodeType == XmlNodeType.Text);
- Assertion.AssertEquals ("bar", document.DocumentElement.FirstChild.Value);
- }
- [Test]
- public void LoadXmlExceptionClearsDocument ()
- {
- document.LoadXml ("<foo/>");
- Assertion.Assert (document.FirstChild != null);
-
- try {
- document.LoadXml ("<123/>");
- Assertion.Fail ("An XmlException should have been thrown.");
- } catch (XmlException) {}
- Assertion.Assert (document.FirstChild == null);
- }
- [Test]
- public void LoadXmlProcessingInstruction ()
- {
- document.LoadXml (@"<?foo bar='baaz' quux='quuux'?><quuuux></quuuux>");
- Assertion.AssertEquals ("Incorrect target.", "foo", ((XmlProcessingInstruction)document.FirstChild).Target);
- Assertion.AssertEquals ("Incorrect data.", "bar='baaz' quux='quuux'", ((XmlProcessingInstruction)document.FirstChild).Data);
- }
- [Test]
- public void OuterXml ()
- {
- string xml;
-
- xml = "<root><![CDATA[foo]]></root>";
- document.LoadXml (xml);
- Assertion.AssertEquals("XmlDocument with cdata OuterXml is incorrect.", xml, document.OuterXml);
- xml = "<root><!--foo--></root>";
- document.LoadXml (xml);
- Assertion.AssertEquals("XmlDocument with comment OuterXml is incorrect.", xml, document.OuterXml);
- xml = "<root><?foo bar?></root>";
- document.LoadXml (xml);
- Assertion.AssertEquals("XmlDocument with processing instruction OuterXml is incorrect.", xml, document.OuterXml);
- }
- [Test]
- public void ParentNodes ()
- {
- document.LoadXml ("<foo><bar><baz/></bar></foo>");
- XmlNode node = document.FirstChild.FirstChild.FirstChild;
- Assertion.AssertEquals ("Wrong child found.", "baz", node.LocalName);
- Assertion.AssertEquals ("Wrong parent.", "bar", node.ParentNode.LocalName);
- Assertion.AssertEquals ("Wrong parent.", "foo", node.ParentNode.ParentNode.LocalName);
- Assertion.AssertEquals ("Wrong parent.", "#document", node.ParentNode.ParentNode.ParentNode.LocalName);
- Assertion.AssertNull ("Expected parent to be null.", node.ParentNode.ParentNode.ParentNode.ParentNode);
- }
- [Test]
- public void RemovedElementNextSibling ()
- {
- XmlNode node;
- XmlNode nextSibling;
- document.LoadXml ("<foo><child1/><child2/></foo>");
- node = document.DocumentElement.FirstChild;
- document.DocumentElement.RemoveChild (node);
- nextSibling = node.NextSibling;
- Assertion.AssertNull ("Expected removed node's next sibling to be null.", nextSibling);
- }
- // ImportNode
- [Test]
- public void ImportNode ()
- {
- XmlNode n;
- string xlinkURI = "http://www.w3.org/1999/XLink";
- string xml1 = "<?xml version='1.0' encoding='utf-8' ?><foo xmlns:xlink='" + xlinkURI + "'><bar a1='v1' xlink:href='#foo'><baz><![CDATA[cdata section.\n\titem 1\n\titem 2\n]]>From here, simple text node.</baz></bar></foo>";
- document.LoadXml(xml1);
- XmlDocument newDoc = new XmlDocument();
- newDoc.LoadXml("<hoge><fuga /></hoge>");
- XmlElement bar = document.DocumentElement.FirstChild as XmlElement;
- // Attribute
- n = newDoc.ImportNode(bar.GetAttributeNode("href", xlinkURI), true);
- Assertion.AssertEquals("#ImportNode.Attr.NS.LocalName", "href", n.LocalName);
- Assertion.AssertEquals("#ImportNode.Attr.NS.NSURI", xlinkURI, n.NamespaceURI);
- Assertion.AssertEquals("#ImportNode.Attr.NS.Value", "#foo", n.Value);
- // CDATA
- n = newDoc.ImportNode(bar.FirstChild.FirstChild, true);
- Assertion.AssertEquals("#ImportNode.CDATA", "cdata section.\n\titem 1\n\titem 2\n", n.Value);
- // Element
- XmlElement e = newDoc.ImportNode(bar, true) as XmlElement;
- Assertion.AssertEquals("#ImportNode.Element.Name", "bar", e.Name);
- Assertion.AssertEquals("#ImportNode.Element.Attr", "#foo", e.GetAttribute("href", xlinkURI));
- Assertion.AssertEquals("#ImportNode.Element.deep", "baz", e.FirstChild.Name);
- // Entity Reference:
- // [2002/10/14] CreateEntityReference was not implemented.
- // document.LoadXml("<!DOCTYPE test PUBLIC 'dummy' [<!ENTITY FOOENT 'foo'>]><root>&FOOENT;</root>");
- // n = newDoc.ImportNode(document.DocumentElement.FirstChild);
- // Assertion.AssertEquals("#ImportNode.EntityReference", "FOOENT", n.Name);
- // Assertion.AssertEquals("#ImportNode.EntityReference", "foo_", n.Value);
- // Processing Instruction
- document.LoadXml("<foo><?xml-stylesheet href='foo.xsl' ?></foo>");
- XmlProcessingInstruction pi = (XmlProcessingInstruction)newDoc.ImportNode(document.DocumentElement.FirstChild, false);
- Assertion.AssertEquals("#ImportNode.ProcessingInstruction.Name", "xml-stylesheet", pi.Name);
- Assertion.AssertEquals("#ImportNode.ProcessingInstruction.Data", "href='foo.xsl'", pi.Data.Trim());
-
- // Text
- document.LoadXml(xml1);
- n = newDoc.ImportNode((XmlText)bar.FirstChild.ChildNodes[1], true);
- Assertion.AssertEquals("#ImportNode.Text", "From here, simple text node.", n.Value);
- // XmlDeclaration
- document.LoadXml(xml1);
- XmlDeclaration decl = (XmlDeclaration)newDoc.ImportNode(document.FirstChild, false);
- Assertion.AssertEquals("#ImportNode.XmlDeclaration.Type", XmlNodeType.XmlDeclaration, decl.NodeType);
- Assertion.AssertEquals("#ImportNode.XmlDeclaration.Encoding", "utf-8", decl.Encoding);
- }
- [Test]
- public void NameTable()
- {
- XmlDocument doc = new XmlDocument();
- Assertion.AssertNotNull(doc.NameTable);
- }
- [Test]
- public void SingleEmptyRootDocument()
- {
- XmlDocument doc = new XmlDocument();
- doc.LoadXml("<root />");
- Assertion.AssertNotNull(doc.DocumentElement);
- }
- [Test]
- public void DocumentWithDoctypeDecl ()
- {
- XmlDocument doc = new XmlDocument ();
- try {
- doc.LoadXml ("<!DOCTYPE test><root />");
- } catch (XmlException) {
- Assertion.Fail ("#DoctypeDecl.OnlyName");
- }
- #if NetworkEnabled
- try
- {
- doc.LoadXml ("<!DOCTYPE test SYSTEM 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><root />");
- } catch (XmlException) {
- Assertion.Fail("#DoctypeDecl.System");
- }
- try {
- doc.LoadXml ("<!DOCTYPE test PUBLIC '-//test' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><root />");
- } catch (XmlException) {
- Assertion.Fail ("#DoctypeDecl.Public");
- }
- #endif
- // Should this be commented out?
- try {
- doc.LoadXml ("<!DOCTYPE test [<!ELEMENT foo >]><root />");
- } catch (XmlException) {
- Assertion.Fail("#DoctypeDecl.ElementDecl");
- }
- }
- [Test]
- public void CloneNode ()
- {
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml ("<foo><bar /><baz hoge='fuga'>TEST Text</baz></foo>");
- XmlDocument doc2 = (XmlDocument)doc.CloneNode (false);
- Assertion.AssertEquals ("ShallowCopy", 0, doc2.ChildNodes.Count);
- doc2 = (XmlDocument)doc.CloneNode (true);
- Assertion.AssertEquals ("DeepCopy", "foo", doc2.DocumentElement.Name);
- }
- [Test]
- public void OuterXmlWithDefaultXmlns ()
- {
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml ("<iq type=\"get\" id=\"ATECLIENT_1\"><query xmlns=\"jabber:iq:auth\"><username></username></query></iq>");
- Assertion.AssertEquals ("<iq type=\"get\" id=\"ATECLIENT_1\"><query xmlns=\"jabber:iq:auth\"><username /></query></iq>", doc.OuterXml);
- }
- [Test]
- public void PreserveWhitespace ()
- {
- string input =
- "<?xml version=\"1.0\" encoding=\"utf-8\" ?><!-- --> <foo/>";
- XmlDocument dom = new XmlDocument ();
- XmlTextReader reader = new XmlTextReader (new StringReader (input));
- dom.Load (reader);
- Assertion.AssertEquals (XmlNodeType.Element, dom.FirstChild.NextSibling.NextSibling.NodeType);
- }
- // [Test] Comment out in the meantime.
- public void LoadExternalUri ()
- {
- // set any URL of well-formed XML.
- document.Load ("http://www.go-mono.com/index.rss");
- }
- [Test]
- public void LoadDocumentWithIgnoreSection ()
- {
- // set any URL of well-formed XML.
- document.Load ("xmlfiles/test.xml");
- }
- }
- }
|