| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311 |
- //
- // XmlSimpleDictionaryWriterTest.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // Copyright (C) 2005 Novell, Inc. http://www.novell.com
- //
- // Copied from System.XML/Test/System.Xml/XmlTextWriterTests.cs
- //
- // Authors:
- // Kral Ferch <[email protected]>
- // Martin Willemoes Hansen <[email protected]>
- //
- // (C) 2002 Kral Ferch
- // (C) 2003 Martin Willemoes Hansen
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.IO;
- using System.Text;
- using System.Xml;
- using NUnit.Framework;
- namespace MonoTests.System.Xml
- {
- [TestFixture]
- public class XmlSimpleDictionaryWriterTest
- {
- [Test]
- public void WriteXmlnsAttribute ()
- {
- xw.WriteStartElement ("l1");
- xw.WriteXmlAttribute ("lang", "ja");
- xw.WriteXmlnsAttribute ("f", "urn:foo");
- xw.WriteStartElement ("l2");
- xw.WriteXmlnsAttribute ("", "");
- xw.WriteEndElement ();
- xw.WriteStartElement ("l2");
- xw.WriteXmlnsAttribute ("", "urn:bar");
- xw.WriteEndElement ();
- xw.WriteEndElement ();
- xw.Flush ();
- Assert.AreEqual ("<l1 xml:lang='ja' xmlns:f='urn:foo'><l2 xmlns='' /><l2 xmlns='urn:bar' /></l1>", Output);
- }
- [Test]
- public void WriteXmlnsAttributeNullPrefix ()
- {
- xw.WriteStartElement ("root", "urn:x");
- xw.WriteXmlnsAttribute (null, "urn:foo");
- xw.WriteEndElement ();
- xw.Close ();
- Assert.AreEqual ("<root xmlns:d1p1='urn:foo' xmlns='urn:x' />", Output);
- }
- #region Copied from XmlTextWriterTests.cs
- StringWriter sw;
- XmlDictionaryWriter xw;
- [SetUp]
- public void GetReady ()
- {
- sw = new StringWriter ();
- XmlTextWriter xtw = new XmlTextWriter (sw);
- xtw.QuoteChar = '\'';
- xw = XmlDictionaryWriter.CreateDictionaryWriter (xtw);
- }
- private string Output
- {
- get { return sw.ToString (); }
- }
- [Test]
- public void AttributeNamespacesNonNamespaceAttributeBefore ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString("bar", "baz");
- xw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
- Assert.AreEqual ("<foo bar='baz' xmlns:abc='http://abc.def'", Output);
- }
- [Test]
- public void AttributeNamespacesNonNamespaceAttributeAfter ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
- xw.WriteAttributeString("bar", "baz");
- Assert.AreEqual ("<foo xmlns:abc='http://abc.def' bar='baz'", Output);
- }
- [Test]
- public void AttributeNamespacesThreeParamWithNullInNamespaceParam ()
- {
- xw.WriteAttributeString ("xmlns", null, "http://abc.def");
- Assert.AreEqual ("xmlns='http://abc.def'", Output);
- }
- [Test]
- public void AttributeNamespacesThreeParamWithTextInNamespaceParam ()
- {
- try
- {
- xw.WriteAttributeString ("xmlns", "http://somenamespace.com", "http://abc.def");
- }
- catch (ArgumentException) {}
- }
- [Test]
- public void AttributeNamespacesWithNullInNamespaceParam ()
- {
- xw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
- Assert.AreEqual ("xmlns:abc='http://abc.def'", Output);
- }
- [Test]
- public void AttributeNamespacesWithTextInNamespaceParam ()
- {
- try {
- xw.WriteAttributeString ("xmlns", "abc", "http://somenamespace.com", "http://abc.def");
- } catch (ArgumentException) {}
- }
- [Test]
- public void AttributeNamespacesXmlnsXmlns ()
- {
- xw.WriteStartElement ("foo");
- // When namespaceURI argument is null, constraints by
- // namespaces in XML are ignored.
- xw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
- xw.WriteAttributeString ("", "xmlns", null, "http://abc.def");
- }
- [Test]
- public void AttributeWriteAttributeString ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString ("foo", "bar");
- Assert.AreEqual ("<foo foo='bar'", Output);
- xw.WriteAttributeString ("bar", "");
- Assert.AreEqual ("<foo foo='bar' bar=''", Output);
- xw.WriteAttributeString ("baz", null);
- Assert.AreEqual ("<foo foo='bar' bar='' baz=''", Output);
- xw.WriteAttributeString ("hoge", "a\nb");
- Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a
b'", Output);
- xw.WriteAttributeString ("fuga", " a\t\r\nb\t");
- Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a
b' fuga=' a\t
b\t'", Output);
- try {
- // Why does this pass Microsoft?
- // Anyway, Mono should not allow such code.
- xw.WriteAttributeString ("", "quux");
- // Assert.AreEqual ("<foo foo='bar' bar='' baz='' ='quux'", Output);
- Assert.Fail ("empty name not allowed.");
- } catch (Exception) {
- }
- try {
- // Why does this pass Microsoft?
- // Anyway, Mono should not allow such code.
- xw.WriteAttributeString (null, "quuux");
- // Assert.AreEqual ("<foo foo='bar' bar='' baz='' ='quux' ='quuux'", Output);
- Assert.Fail ("null name not allowed.");
- } catch (Exception) {
- }
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void AttributeWriteAttributeStringNotInsideOpenStartElement ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteString ("bar");
-
- xw.WriteAttributeString ("baz", "quux");
- }
- [Test]
- public void AttributeWriteAttributeStringWithoutParentElement ()
- {
- xw.WriteAttributeString ("foo", "bar");
- Assert.AreEqual ("foo='bar'", Output);
- xw.WriteAttributeString ("baz", "quux");
- Assert.AreEqual ("foo='bar' baz='quux'", Output);
- }
- [Test]
- public void CDataValid ()
- {
- xw.WriteCData ("foo");
- Assert.AreEqual ("<![CDATA[foo]]>", Output,
- "WriteCData had incorrect output.");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void CDataInvalid ()
- {
- xw.WriteCData("foo]]>bar");
- }
-
- [Test]
- public void CloseOpenElements ()
- {
- xw.WriteStartElement("foo");
- xw.WriteStartElement("bar");
- xw.WriteStartElement("baz");
- xw.Close();
- Assert.AreEqual ("<foo><bar><baz /></bar></foo>", Output,
- "Close didn't write out end elements properly.");
- }
- [Test]
- public void CloseWriteAfter ()
- {
- xw.WriteElementString ("foo", "bar");
- xw.Close ();
- // WriteEndElement and WriteStartDocument aren't tested here because
- // they will always throw different exceptions besides 'The Writer is closed.'
- // and there are already tests for those exceptions.
- try {
- xw.WriteCData ("foo");
- Assert.Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
- }
- catch (InvalidOperationException) {
- // Don't rely on English message assertion.
- // It is enough to check an exception occurs.
- // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
- }
- try {
- xw.WriteComment ("foo");
- Assert.Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
- }
- catch (InvalidOperationException) {
- // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
- }
- try {
- xw.WriteProcessingInstruction ("foo", "bar");
- Assert.Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
- }
- catch (InvalidOperationException) {
- // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
- }
- try {
- xw.WriteStartElement ("foo", "bar", "baz");
- Assert.Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
- }
- catch (InvalidOperationException) {
- // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
- }
- try
- {
- xw.WriteAttributeString ("foo", "bar");
- Assert.Fail ("WriteAttributeString after Close Should have thrown an InvalidOperationException.");
- }
- catch (InvalidOperationException)
- {
- // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
- }
- try {
- xw.WriteString ("foo");
- Assert.Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
- }
- catch (InvalidOperationException) {
- // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
- }
- }
- [Test]
- public void CommentValid ()
- {
- xw.WriteComment ("foo");
- Assert.AreEqual ("<!--foo-->", Output,
- "WriteComment had incorrect output.");
- }
- [Test]
- public void CommentInvalid ()
- {
- try {
- xw.WriteComment("foo-");
- Assert.Fail ("Should have thrown an ArgumentException.");
- }
- catch (ArgumentException) { }
- try {
- xw.WriteComment("foo-->bar");
- Assert.Fail ("Should have thrown an ArgumentException.");
- }
- catch (ArgumentException) { }
- }
- [Test]
- public void DocumentStart ()
- {
- xw.WriteStartDocument ();
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?>", Output,
- "XmlDeclaration is incorrect.");
- try
- {
- xw.WriteStartDocument ();
- Assert.Fail ("Should have thrown an InvalidOperationException.");
- }
- catch (InvalidOperationException) {
- // Don't rely on English message assertion.
- // It is enough to check an exception occurs.
- // Assert.AreEqual ("Exception message is incorrect.",
- // "WriteStartDocument should be the first call.", e.Message);
- }
- GetReady ();
- xw.WriteStartDocument (true);
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='yes'?>", Output);
- GetReady ();
- xw.WriteStartDocument (false);
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", Output);
- }
- [Test]
- public void ElementAndAttributeSameXmlns ()
- {
- xw.WriteStartElement ("ped", "foo", "urn:foo");
- xw.WriteStartAttribute ("ped", "foo", "urn:foo");
- xw.WriteEndElement ();
- Assert.AreEqual ("<ped:foo ped:foo='' xmlns:ped='urn:foo' />", Output);
- }
- [Test]
- [Category ("NotDotNet")]
- public void ElementXmlnsNeedEscape ()
- {
- xw.WriteStartElement ("test", "foo", "'");
- xw.WriteEndElement ();
- // MS.NET output is : xmlns:test='''
- Assert.AreEqual ("<test:foo xmlns:test=''' />", Output);
- }
- [Test]
- public void ElementEmpty ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteEndElement ();
- Assert.AreEqual ("<foo />", Output);
- }
- [Test]
- public void ElementWriteElementString ()
- {
- xw.WriteElementString ("foo", "bar");
- Assert.AreEqual ("<foo>bar</foo>", Output,
- "WriteElementString has incorrect output.");
- xw.WriteElementString ("baz", "");
- Assert.AreEqual ("<foo>bar</foo><baz />", Output);
- xw.WriteElementString ("quux", null);
- Assert.AreEqual ("<foo>bar</foo><baz /><quux />", Output);
- xw.WriteElementString ("", "quuux");
- Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</>", Output);
- xw.WriteElementString (null, "quuuux");
- Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", Output);
- }
- [Test]
- public void LookupPrefix ()
- {
- xw.WriteStartElement ("root");
- xw.WriteStartElement ("one");
- xw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
- xw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
- Assert.AreEqual ("foo", xw.LookupPrefix ("http://abc.def"));
- Assert.AreEqual ("bar", xw.LookupPrefix ("http://ghi.jkl"));
- xw.WriteEndElement ();
- xw.WriteStartElement ("two");
- xw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
- xw.WriteString("quux");
- Assert.AreEqual ("baz", xw.LookupPrefix ("http://mno.pqr"));
- Assert.IsNull (xw.LookupPrefix ("http://abc.def"));
- Assert.IsNull (xw.LookupPrefix ("http://ghi.jkl"));
- Assert.IsNull (xw.LookupPrefix ("http://bogus"));
- }
- [Test]
- public void NamespacesNoNamespaceClearsDefaultNamespace ()
- {
- xw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
- xw.WriteStartElement(String.Empty, "bar", String.Empty);
- xw.WriteElementString("baz", String.Empty, String.Empty);
- xw.WriteEndElement();
- xw.WriteEndElement();
- Assert.AreEqual (
- "<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>",
- Output,
- "XmlTextWriter is incorrectly outputting namespaces.");
- }
- [Test]
- public void NamespacesPrefix ()
- {
- xw.WriteStartElement ("foo", "bar", "http://netsack.com/");
- xw.WriteStartElement ("foo", "baz", "http://netsack.com/");
- xw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
- xw.WriteEndElement ();
- xw.WriteEndElement ();
- Assert.AreEqual ("<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>",
- Output,
- "XmlTextWriter is incorrectly outputting prefixes.");
- }
- [Test]
- // [ExpectedException (typeof (ArgumentException))]
- public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty ()
- {
- xw.WriteStartElement ("foo", "bar", "");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesPrefixWithEmptyAndNullNamespaceNull ()
- {
- xw.WriteStartElement ("foo", "bar", null);
- }
- [Test]
- public void ProcessingInstructionValid ()
- {
- xw.WriteProcessingInstruction("foo", "bar");
- Assert.AreEqual ("<?foo bar?>", Output, "WriteProcessingInstruction had incorrect output.");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ProcessingInstructionInvalid1 ()
- {
- xw.WriteProcessingInstruction("fo?>o", "bar");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ProcessingInstructionInvalid2 ()
- {
- xw.WriteProcessingInstruction("foo", "ba?>r");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ProcessingInstructionInvalid3 ()
- {
- xw.WriteProcessingInstruction("", "bar");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ProcessingInstructionInvalid4 ()
- {
- xw.WriteProcessingInstruction(null, "bar");
- }
- [Test]
- public void WriteBase64 ()
- {
- UTF8Encoding encoding = new UTF8Encoding();
- byte[] fooBar = encoding.GetBytes("foobar");
- xw.WriteBase64 (fooBar, 0, 6);
- Assert.AreEqual("Zm9vYmFy", Output);
- try {
- xw.WriteBase64 (fooBar, 3, 6);
- Assert.Fail ("Expected an Argument Exception to be thrown.");
- } catch (ArgumentException) {}
- try {
- xw.WriteBase64 (fooBar, -1, 6);
- Assert.Fail ("Expected an Argument Exception to be thrown.");
- } catch (ArgumentOutOfRangeException) {}
- try {
- xw.WriteBase64 (fooBar, 3, -1);
- Assert.Fail ("Expected an Argument Exception to be thrown.");
- } catch (ArgumentOutOfRangeException) {}
- try {
- xw.WriteBase64 (null, 0, 6);
- Assert.Fail ("Expected an Argument Exception to be thrown.");
- } catch (ArgumentNullException) {}
- }
- [Test]
- public void WriteBinHex ()
- {
- byte [] bytes = new byte [] {4,14,34, 54,94,114, 134,194,255, 0,5};
- xw.WriteBinHex (bytes, 0, 11);
- Assert.AreEqual ("040E22365E7286C2FF0005", Output);
- }
- [Test]
- public void WriteCharEntity ()
- {
- xw.WriteCharEntity ('a');
- Assert.AreEqual ("a", Output);
- xw.WriteCharEntity ('A');
- Assert.AreEqual ("aA", Output);
- xw.WriteCharEntity ('1');
- Assert.AreEqual ("aA1", Output);
- xw.WriteCharEntity ('K');
- Assert.AreEqual ("aA1K", Output);
- try {
- xw.WriteCharEntity ((char)0xd800);
- } catch (ArgumentException) {}
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void WriteEndAttribute ()
- {
- xw.WriteEndAttribute ();
- }
- [Test]
- public void WriteEndDocument ()
- {
- try {
- xw.WriteEndDocument ();
- Assert.Fail ("Expected an Exception.");
- // in .NET 2.0 it is InvalidOperationException.
- // in .NET 1,1 it is ArgumentException.
- } catch (Exception) {}
- }
- [Test]
- public void WriteEndDocument2 ()
- {
- xw.WriteStartDocument ();
- try
- {
- xw.WriteEndDocument ();
- Assert.Fail ("Expected an Exception.");
- // in .NET 2.0 it is InvalidOperationException.
- // in .NET 1,1 it is ArgumentException.
- } catch (Exception) {}
- }
- [Test]
- public void WriteEndDocument3 ()
- {
- xw.WriteStartDocument ();
- xw.WriteStartElement ("foo");
- xw.WriteStartAttribute ("bar", null);
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='", Output);
- xw.WriteEndDocument ();
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", Output);
- Assert.AreEqual (WriteState.Start, xw.WriteState);
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void WriteEndElement ()
- {
- // no matching StartElement
- xw.WriteEndElement ();
- }
- [Test]
- public void WriteEndElement2 ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteEndElement ();
- Assert.AreEqual ("<foo />", Output);
- xw.WriteStartElement ("bar");
- xw.WriteStartAttribute ("baz", null);
- xw.WriteEndElement ();
- Assert.AreEqual ("<foo /><bar baz='' />", Output);
- }
- [Test]
- public void FullEndElement ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteFullEndElement ();
- Assert.AreEqual ("<foo></foo>", Output);
- xw.WriteStartElement ("bar");
- xw.WriteAttributeString ("foo", "bar");
- xw.WriteFullEndElement ();
- Assert.AreEqual ("<foo></foo><bar foo='bar'></bar>", Output);
- xw.WriteStartElement ("baz");
- xw.WriteStartAttribute ("bar", null);
- xw.WriteFullEndElement ();
- Assert.AreEqual ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", Output);
- }
- [Test]
- public void WriteQualifiedName ()
- {
- xw.WriteStartElement (null, "test", null);
- xw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
- xw.WriteQualifiedName ("bob", "http://localhost/");
- xw.WriteEndElement ();
- Assert.AreEqual ("<test xmlns:me='http://localhost/'>me:bob</test>", Output);
- }
- [Test]
- public void WriteQualifiedNameNonDeclaredAttribute ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteStartAttribute ("a", "");
- xw.WriteQualifiedName ("attr", "urn:a");
- xw.WriteWhitespace (" ");
- xw.WriteQualifiedName ("attr", "urn:b");
- xw.WriteEndAttribute ();
- xw.WriteEndElement ();
- string xml = sw.ToString ();
- Assert.IsTrue (
- xml.IndexOf ("<foo ") >= 0,
- "foo");
- Assert.IsTrue (
- xml.IndexOf ("a='d1p1:attr d1p2:attr'") > 0,
- "qnames");
- Assert.IsTrue (
- xml.IndexOf (" xmlns:d1p1='urn:a'") > 0,
- "xmlns:a");
- Assert.IsTrue (
- xml.IndexOf (" xmlns:d1p2='urn:b'") > 0,
- "xmlns:b");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteQualifiedNameNonDeclaredContent ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteQualifiedName ("abc", "urn:abc");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteQualifiedNameNonNCName ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString ("xmlns", "urn:default");
- xw.WriteStartElement ("child");
- xw.WriteStartAttribute ("a", "");
- xw.WriteQualifiedName ("x:def", "urn:def");
- }
- [Test]
- public void WriteRaw ()
- {
- xw.WriteRaw("&<>\"'");
- Assert.AreEqual ("&<>\"'", Output);
- xw.WriteRaw(null);
- Assert.AreEqual ("&<>\"'", Output);
- xw.WriteRaw("");
- Assert.AreEqual ("&<>\"'", Output);
- }
- [Test]
- public void WriteRawInvalidInAttribute ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteStartAttribute ("bar", null);
- xw.WriteRaw ("&<>\"'");
- xw.WriteEndAttribute ();
- xw.WriteEndElement ();
- Assert.AreEqual ("<foo bar='&<>\"'' />", Output);
- }
- [Test]
- public void WriteStateTest ()
- {
- Assert.AreEqual (WriteState.Start, xw.WriteState);
- xw.WriteStartDocument ();
- Assert.AreEqual (WriteState.Prolog, xw.WriteState);
- xw.WriteStartElement ("root");
- Assert.AreEqual (WriteState.Element, xw.WriteState);
- xw.WriteElementString ("foo", "bar");
- Assert.AreEqual (WriteState.Content, xw.WriteState);
- xw.Close ();
- Assert.AreEqual (WriteState.Closed, xw.WriteState);
- }
- [Test]
- public void WriteString ()
- {
- xw.WriteStartDocument ();
- try {
- xw.WriteString("foo");
- } catch (InvalidOperationException) {}
- }
- [Test]
- public void WriteString2 ()
- {
- xw.WriteStartDocument ();
- // Testing attribute values
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString ("bar", "&<>");
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='&<>'", Output);
- }
- [Test]
- public void WriteStringWithEntities()
- {
- // Testing element values
- xw.WriteElementString ("foo", "&<>\"'");
- Assert.AreEqual ("<foo>&<>\"'</foo>", Output);
- }
- [Test]
- public void XmlLang ()
- {
- Assert.IsNull (xw.XmlLang);
-
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString ("xml", "lang", null, "langfoo");
- Assert.AreEqual ("langfoo", xw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo'", Output);
- xw.WriteAttributeString ("boo", "yah");
- Assert.AreEqual ("langfoo", xw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'", Output);
-
- xw.WriteElementString("bar", "baz");
- Assert.AreEqual ("langfoo", xw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", Output);
-
- xw.WriteString("baz");
- Assert.AreEqual ("langfoo", xw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", Output);
-
- xw.WriteStartElement ("quux");
- xw.WriteStartAttribute ("xml", "lang", null);
- Assert.AreEqual ("langfoo", xw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", Output);
-
- xw.WriteString("langbar");
- // Commented out there: it is implementation-dependent.
- // and incompatible between .NET 1.0 and 1.1
- // Assert.AreEqual ("langfoo", xw.XmlLang);
- // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", Output);
-
- xw.WriteEndAttribute ();
- // Commented out there: it is implementation-dependent.
- // and incompatible between .NET 1.0 and 1.1
- // Assert.AreEqual ("langbar", xw.XmlLang);
- // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", Output);
- // check if xml:lang repeats output even if same as current scope.
- xw.WriteStartElement ("joe");
- xw.WriteAttributeString ("xml", "lang", null, "langbar");
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", Output);
-
- xw.WriteElementString ("quuux", "squonk");
- Assert.AreEqual ("langbar", xw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", Output);
- xw.WriteEndElement ();
- xw.WriteEndElement ();
- Assert.AreEqual ("langfoo", xw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux>", Output);
-
- xw.WriteEndElement ();
- Assert.IsNull (xw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux></foo>", Output);
-
- xw.Close ();
- Assert.IsNull (xw.XmlLang);
- }
- // TODO: test operational aspects
- [Test]
- public void XmlSpaceTest ()
- {
- xw.WriteStartElement ("foo");
- Assert.AreEqual (XmlSpace.None, xw.XmlSpace);
- xw.WriteStartElement ("bar");
- xw.WriteAttributeString ("xml", "space", null, "preserve");
- Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace);
- Assert.AreEqual ("<foo><bar xml:space='preserve'", Output);
- xw.WriteStartElement ("baz");
- xw.WriteAttributeString ("xml", "space", null, "preserve");
- Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace);
- Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", Output);
- xw.WriteStartElement ("quux");
- xw.WriteStartAttribute ("xml", "space", null);
- Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace);
- Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", Output);
- // Commented out there: it is implementation-dependent
- // and incompatible between .NET 1.0 and 1.1
- xw.WriteString ("default");
- // Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace);
- // Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", Output);
-
- xw.WriteEndAttribute ();
- Assert.AreEqual (XmlSpace.Default, xw.XmlSpace);
- Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", Output);
- xw.WriteEndElement ();
- Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace);
- xw.WriteEndElement ();
- Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace);
- xw.WriteEndElement ();
- Assert.AreEqual (XmlSpace.None, xw.XmlSpace);
- xw.WriteStartElement ("quux");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void XmlSpaceTestInvalidValue1 ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString ("xml", "space", null, "bubba");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void XmlSpaceTestInvalidValue2 ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString ("xml", "space", null, "PRESERVE");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void XmlSpaceTestInvalidValue3 ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString ("xml", "space", null, "Default");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void XmlSpaceTestInvalidValue4 ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString ("xml", "space", null, "bubba");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteWhitespaceNonWhitespace ()
- {
- xw.WriteWhitespace ("x");
- }
- [Test]
- public void XmlSpaceRaw ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteStartAttribute ("xml", "space", null);
- Assert.AreEqual (XmlSpace.None, xw.XmlSpace);
- Assert.AreEqual ("<foo xml:space='", Output);
- xw.WriteString ("default");
- // Commented out there: it is implementation-dependent
- // and incompatible between .NET 1.0 and 1.1
- // Assert.AreEqual (XmlSpace.None, xw.XmlSpace);
- // Assert.AreEqual ("<foo xml:space='", Output);
- xw.WriteEndAttribute ();
- Assert.AreEqual (XmlSpace.Default, xw.XmlSpace);
- Assert.AreEqual ("<foo xml:space='default'", Output);
- }
- [Test]
- public void WriteAttributes ()
- {
- XmlDocument doc = new XmlDocument();
- StringWriter sw = new StringWriter();
- XmlWriter wr = new XmlTextWriter(sw);
- StringBuilder sb = sw.GetStringBuilder();
- XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
- XmlTextReader xtr = new XmlTextReader("<?xml version='1.0' encoding='utf-8' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
- xtr.Read(); // read XMLDecl
- wr.WriteAttributes(xtr, false);
- // This method don't always have to take this double-quoted style...
- Assert.AreEqual("version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString().Trim(), "#WriteAttributes.XmlDecl.1");
- sb.Remove(0, sb.Length); // init
- ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
- xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
- xtr.Read(); // read XMLDecl
- Assert.AreEqual (XmlNodeType.XmlDeclaration, xtr.NodeType);
- sw = new StringWriter ();
- wr = new XmlTextWriter (sw);
- // This block raises an error on MS.NET 1.0.
- wr.WriteAttributes(xtr, false);
- // This method don't always have to take this double-quoted style...
- Assert.AreEqual("version=\"1.0\" standalone=\"no\"", sw.ToString().Trim(), "#WriteAttributes.XmlDecl.2");
- sw = new StringWriter ();
- wr = new XmlTextWriter (sw);
- sb.Remove(0, sb.Length); // init
- xtr.Read(); // read root
- Assert.AreEqual (XmlNodeType.Element, xtr.NodeType);
- wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
- wr.WriteAttributes(xtr, false);
- wr.WriteEndElement();
- wr.Close();
- // This method don't always have to take this double-quoted style...
- Assert.AreEqual("<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString().Trim(), "#WriteAttributes.Element");
- xtr.Close ();
- }
- [Test]
- public void WriteWhitespace ()
- {
- xw.WriteStartElement ("a");
- xw.WriteWhitespace ("\n\t");
- xw.WriteStartElement ("b");
- xw.WriteWhitespace ("\n\t");
- xw.WriteEndElement ();
- xw.WriteWhitespace ("\n");
- xw.WriteEndElement ();
- xw.WriteWhitespace ("\n");
- xw.Flush ();
- Assert.AreEqual ("<a>\n\t<b>\n\t</b>\n</a>\n", Output);
- }
- [Test]
- public void FlushDoesntCloseTag ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString ("bar", "baz");
- xw.Flush ();
- Assert.AreEqual ("<foo bar='baz'", Output);
- }
- [Test]
- public void WriteWhitespaceClosesTag ()
- {
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString ("bar", "baz");
- xw.WriteWhitespace (" ");
- Assert.AreEqual ("<foo bar='baz'> ", Output);
- }
- [Test]
- public void DontOutputMultipleXmlns ()
- {
- XmlDocument doc = new XmlDocument();
- doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
- XmlDocument doc2 = new XmlDocument();
- doc2.LoadXml(doc.InnerXml);
- Assert.AreEqual ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",
- doc2.OuterXml);
- }
- [Test]
- public void DontOutputNonDeclaredXmlns ()
- {
- string xml = "<x:a foo='foo' xmlns:x='urn:foo'><b /></x:a>";
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(xml);
- XmlDocument doc2 = new XmlDocument();
- doc2.LoadXml(doc.InnerXml);
- Assert.AreEqual (xml.Replace ('\'', '"'), doc2.OuterXml);
- }
- [Test]
- public void DontOutputRemovalDefaultNSDeclaration ()
- {
- xw.WriteStartDocument ();
- xw.WriteStartElement ("foo");
- xw.WriteAttributeString ("xmlns", "probe");
- Assert.AreEqual (String.Empty, xw.LookupPrefix ("probe"));
- xw.WriteStartElement ("b");
- Assert.AreEqual (String.Empty, xw.LookupPrefix ("probe"));
- xw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
- xw.WriteEndElement (); // b2
- xw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
- xw.WriteEndElement (); // b2
- xw.WriteEndElement (); // b
- xw.WriteEndElement (); // foo
- xw.WriteEndDocument ();
- xw.Close ();
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 xmlns='' /></b></foo>", Output);
- }
- [Test]
- public void DontOutputRemovalDefaultNSDeclaration2 ()
- {
- xw.WriteStartDocument ();
- // IMPORTANT DIFFERENCE!! ns = "", not null
- xw.WriteStartElement ("foo", "");
- xw.WriteAttributeString ("xmlns", "probe");
- Assert.IsNull (xw.LookupPrefix ("probe"));
- xw.WriteStartElement ("b");
- Assert.IsNull (xw.LookupPrefix ("probe"));
- xw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
- xw.WriteEndElement (); // b2
- xw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
- xw.WriteEndElement (); // b2
- xw.WriteEndElement (); // b
- xw.WriteEndElement (); // foo
- xw.WriteEndDocument ();
- xw.Close ();
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 /></b></foo>", Output);
- }
- [Test]
- public void DoOutputRemovalDefaultNSDeclaration ()
- {
- xw.WriteStartElement ("docelem", "a-namespace");
-
- XmlDocument doc = new XmlDocument ();
- doc.CreateElement ("hola").WriteTo (xw);
- // This means, WriteTo never passes null NamespaceURI argument to XmlWriter.
- xw.WriteEndElement ();
- xw.Close ();
- Assert.AreEqual ("<docelem xmlns='a-namespace'><hola xmlns='' /></docelem>", Output);
- }
- [Test]
- public void WriteAttributeTakePrecedenceOnXmlns ()
- {
- xw.WriteStartElement ("root", "urn:foo");
- xw.WriteAttributeString ("xmlns", "urn:bar");
- xw.WriteEndElement ();
- xw.Close ();
- Assert.AreEqual ("<root xmlns='urn:bar' />", Output);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void LookupPrefixNull ()
- {
- xw.LookupPrefix (null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void LookupPrefixEmpty ()
- {
- xw.LookupPrefix (String.Empty);
- }
- [Test]
- public void LookupPrefixIgnoresXmlnsAttribute ()
- {
- Assert.IsNull (xw.LookupPrefix ("urn:foo"));
- xw.WriteStartElement ("root");
- Assert.IsNull (xw.LookupPrefix ("urn:foo"));
- xw.WriteAttributeString ("xmlns", "urn:foo");
- // Surprisingly to say, it is ignored!!
- Assert.AreEqual (String.Empty, xw.LookupPrefix ("urn:foo"));
- xw.WriteStartElement ("hoge");
- // (still after flushing previous start element.)
- Assert.AreEqual (String.Empty, xw.LookupPrefix ("urn:foo"));
- xw.WriteStartElement ("fuga", "urn:foo");
- // Is this testing on the correct way? Yes, here it is.
- Assert.AreEqual (String.Empty, xw.LookupPrefix ("urn:foo"));
- }
- [Test]
- public void WriteInvalidNames ()
- {
- xw.WriteStartElement ("foo<>");
- xw.WriteAttributeString ("ho<>ge", "value");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeWriteStartAttributePrefixWithoutNS ()
- {
- xw.WriteStartAttribute ("some", "foo", null);
- }
- [Test]
- public void AttributeWriteStartAttributeXmlnsNullNS ()
- {
- xw.WriteStartAttribute ("xmlns", "foo", null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeWriteEndAttributeXmlnsNullNs ()
- {
- // Compare with the test AttributeWriteStartAttributeXmlnsNullNS().
- xw.WriteStartAttribute ("xmlns", "foo", null);
- xw.WriteEndAttribute ();
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS ()
- {
- xw.WriteStartAttribute ("xmlns", "foo", "urn:foo");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS ()
- {
- xw.WriteStartAttribute ("", "xmlns", "urn:foo");
- }
- [Test]
- public void WriteRawProceedToProlog ()
- {
- XmlTextWriter xw = new XmlTextWriter (new StringWriter ());
- xw.WriteRaw ("");
- Assert.AreEqual (WriteState.Prolog, xw.WriteState);
- }
- [Test]
- public void CloseTwice ()
- {
- StringWriter sw = new StringWriter ();
- XmlTextWriter writer = new XmlTextWriter (sw);
- writer.Close ();
- // should not result in an exception
- writer.Close ();
- }
- [Test]
- public void WriteRawWriteString ()
- {
- // WriteRaw () -> WriteString ().
- xw.WriteRaw ("");
- xw.WriteString ("foo");
- Assert.AreEqual (WriteState.Content, xw.WriteState);
- }
- [Test]
- public void LookupOverridenPrefix ()
- {
- xw.WriteStartElement ("out");
- xw.WriteAttributeString ("xmlns", "baz", "http://www.w3.org/2000/xmlns/", "xyz");
- xw.WriteStartElement ("baz", "foo", "abc");
- Assert.IsNull (xw.LookupPrefix ("xyz"));
- }
- [Test]
- public void DuplicatingNamespaceMappingInAttributes ()
- {
- xw.WriteStartElement ("out");
- xw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
- xw.WriteAttributeString ("p", "bar", "urn:bar", "xyz");
- xw.WriteAttributeString ("p", "baz", "urn:baz", "xyz");
- xw.WriteStartElement ("out");
- xw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
- xw.WriteStartElement ("out");
- xw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
- xw.WriteEndElement ();
- xw.WriteEndElement ();
- xw.WriteEndElement ();
- string xml = sw.ToString ();
- Assert.IsTrue (
- xml.IndexOf ("p:foo='xyz'") > 0,
- "p:foo");
- Assert.IsTrue (
- xml.IndexOf ("d1p1:bar='xyz'") > 0,
- "d1p1:bar");
- Assert.IsTrue (
- xml.IndexOf ("d1p2:baz='xyz'") > 0,
- "d1p1:baz");
- Assert.IsTrue (
- xml.IndexOf ("xmlns:d1p2='urn:baz'") > 0,
- "xmlns:d1p2");
- Assert.IsTrue (
- xml.IndexOf ("xmlns:d1p1='urn:bar'") > 0,
- "xmlns:d1p1");
- Assert.IsTrue (
- xml.IndexOf ("xmlns:p='urn:foo'") > 0,
- "xmlns:p");
- Assert.IsTrue (
- xml.IndexOf ("<out p:foo='xyz'><out p:foo='xyz' /></out></out>") > 0,
- "remaining");
- }
- [Test]
- public void WriteXmlSpaceIgnoresNS ()
- {
- xw.WriteStartElement ("root");
- xw.WriteAttributeString ("xml", "space", "abc", "preserve");
- xw.WriteEndElement ();
- Assert.AreEqual ("<root xml:space='preserve' />", sw.ToString ());
- }
- [Test] // bug #75546
- public void WriteEmptyNSQNameInAttribute ()
- {
- XmlTextWriter xw = new XmlTextWriter (TextWriter.Null);
- xw.WriteStartElement ("foo", "urn:goo");
- xw.WriteAttributeString ("xmlns:bar", "urn:bar");
- xw.WriteStartAttribute ("foo", "");
- xw.WriteQualifiedName ("n1", "urn:bar");
- xw.WriteEndAttribute ();
- xw.WriteStartAttribute ("foo", "");
- xw.WriteQualifiedName ("n2", "");
- xw.WriteEndAttribute ();
- }
- [Test] // bug #76095
- public void SurrogatePairsInWriteString ()
- {
- MemoryStream ms = new MemoryStream ();
- XmlWriter writer = new XmlTextWriter(ms, null);
- writer.WriteElementString("a", "\ud800\udf39");
- writer.Close();
- byte [] referent = new byte [] {0x3c, 0x61, 0x3e, 0xf0,
- 0x90, 0x8c, 0xb9, 0x3c, 0x2f, 0x61, 0x3e};
- NUnit.Framework.Assert.AreEqual (referent, ms.ToArray ());
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void RejectWritingAtErrorState ()
- {
- try {
- xw.WriteEndElement ();
- } catch (Exception) {
- }
- xw.WriteStartElement ("foo");
- }
- #endregion
- [Test]
- public void WriteBooleanArray ()
- {
- bool [] array = new bool [] {true, false, true, true, false};
- xw.WriteArray ("", "root", "", array, 1, 3);
- Assert.AreEqual ("<root>false</root><root>true</root><root>true</root>", Output, "#1");
- }
- [Test]
- public void WriteNode ()
- {
- string s = @"<Resolve xmlns='http://schemas.microsoft.com/net/2006/05/peer' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><ClientId>79310c9f-18d4-4337-a95a-1865ca54a66e</ClientId><MaxAddresses>3</MaxAddresses><MeshId>amesh</MeshId></Resolve>".Replace ('\'', '"');
- var sw = new StringWriter ();
- var xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, new XmlWriterSettings () { OmitXmlDeclaration = true }));
- var xr = XmlDictionaryReader.CreateDictionaryReader (XmlReader.Create (new StringReader (s)));
- xr.MoveToContent ();
- while (!xr.EOF && xr.NodeType != XmlNodeType.EndElement)
- xw.WriteNode (xr, false);
- xw.Flush ();
- Assert.AreEqual (s, sw.ToString ());
- }
- }
- }
|