| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328 |
- //
- // System.Xml.XmlTextWriterTests
- //
- // Authors:
- // Kral Ferch <[email protected]>
- // Martin Willemoes Hansen <[email protected]>
- //
- // (C) 2002 Kral Ferch
- // (C) 2003 Martin Willemoes Hansen
- //
- using System;
- using System.Globalization;
- using System.IO;
- using System.Text;
- using System.Xml;
- using NUnit.Framework;
- namespace MonoTests.System.Xml
- {
- [TestFixture]
- public class XmlTextWriterTests
- {
- StringWriter sw;
- XmlTextWriter xtw;
- [SetUp]
- public void GetReady ()
- {
- sw = new StringWriter ();
- CreateXmlTextWriter ();
- }
- private void CreateXmlTextWriter ()
- {
- xtw = new XmlTextWriter (sw);
- xtw.QuoteChar = '\'';
- }
- private string StringWriterText
- {
- get { return sw.GetStringBuilder ().ToString (); }
- }
- [Test]
- public void AttributeNamespacesNonNamespaceAttributeBefore ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString("bar", "baz");
- xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
- Assert.AreEqual ("<foo bar='baz' xmlns:abc='http://abc.def'", StringWriterText);
- }
- [Test]
- public void AttributeNamespacesNonNamespaceAttributeAfter ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
- xtw.WriteAttributeString("bar", "baz");
- Assert.AreEqual ("<foo xmlns:abc='http://abc.def' bar='baz'", StringWriterText);
- }
- [Test]
- public void AttributeNamespacesThreeParamWithNullInNamespaceParam ()
- {
- xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
- Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText);
- }
- [Test]
- public void WriteAttributeString_XmlNs_Valid ()
- {
- xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
- Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("xmlns", "http://www.w3.org/2000/xmlns/", "http://abc.def");
- Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString (null, "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
- Assert.AreEqual ("xmlns:test='http://abc.def'", StringWriterText, "#3");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("", "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
- Assert.AreEqual ("xmlns:test='http://abc.def'", StringWriterText, "#4");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("person");
- xtw.WriteAttributeString ("", "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<person xmlns:test='http://abc.def' />", StringWriterText, "#5");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteAttributeString_XmlNs_Invalid1 ()
- {
- // The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'
- xtw.WriteAttributeString ("xmlns", "http://somenamespace.com", "http://abc.def");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteAttributeString_XmlNs_Invalid2 ()
- {
- // The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'
- xtw.WriteAttributeString (null, "xmlns", "http://somenamespace.com", "http://abc.def");
- }
- [Test]
- public void XmlSpace_Valid () // bug #77084
- {
- xtw.WriteAttributeString ("xml", "space", null, "preserve");
- Assert.AreEqual ("xml:space='preserve'", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("xml", "space", "whatever", "default");
- Assert.AreEqual ("xml:space='default'", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("person");
- xtw.WriteAttributeString ("xml", "space", "whatever", "default");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<person xml:space='default' />", StringWriterText, "#3");
- }
- [Test]
- public void WriteAttributeString_XmlPrefix_Valid ()
- {
- xtw.WriteAttributeString ("xml", "something", "whatever", "default");
- Assert.AreEqual ("xml:something='default'", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("xml", "else", null, "whatever");
- Assert.AreEqual ("xml:else='whatever'", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("person");
- xtw.WriteAttributeString ("xml", "something", "whatever", "default");
- xtw.WriteAttributeString ("xml", "else", null, "whatever");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<person xml:something='default' xml:else='whatever' />",
- StringWriterText, "#3");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteAttributeString_XmlSpace_Invalid ()
- {
- // only preserve and default are valid values for xml:space
- xtw.WriteAttributeString ("xml", "space", null, "something");
- }
- [Test]
- public void AttributeNamespacesThreeParamWithTextInNamespaceParam ()
- {
- xtw.WriteAttributeString ("a", "http://somenamespace.com", "http://abc.def");
- Assert.AreEqual ("d0p1:a='http://abc.def'", StringWriterText, "#1");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeNamespacesWithNullInNamespaceParam ()
- {
- // you cannot use prefix with an empty namespace
- xtw.WriteAttributeString ("a", "abc", null, "http://abc.def");
- }
- [Test]
- public void AttributeNamespacesWithTextInNamespaceParam ()
- {
- xtw.WriteAttributeString ("a", "abc", "http://somenamespace.com", "http://abc.def");
- Assert.AreEqual ("a:abc='http://abc.def'", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("", "abc", "http://somenamespace.com", "http://abc.def");
- Assert.AreEqual ("d0p1:abc='http://abc.def'", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString (null, "abc", "http://somenamespace.com", "http://abc.def");
- Assert.AreEqual ("d0p1:abc='http://abc.def'", StringWriterText, "#3");
- }
- [Test]
- [Ignore ("Due to the (silly) dependency on bug #77088, this test will not be fixed. The test could be rewritten but it depends on the original test author.")]
- public void AutoCreatePrefixes ()
- {
- xtw.WriteStartElement ("root");
- xtw.WriteAttributeString (null, "abc", "http://somenamespace.com", "http://abc.def");
- xtw.WriteAttributeString (null, "def", "http://somenamespace.com", "http://def.ghi");
- xtw.WriteAttributeString (null, "ghi", "http://othernamespace.com", "http://ghi.jkl");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<root d1p1:abc='http://abc.def' d1p1:def='http://def.ghi' d1p2:ghi='http://ghi.jkl' xmlns:d1p2='http://othernamespace.com' xmlns:d1p1='http://somenamespace.com' />", StringWriterText, "#1");
- }
- [Test]
- [Ignore ("Due to the (silly) dependency on bug #77088, this test will not be fixed. The test could be rewritten but it depends on the original test author.")]
- public void AutoCreatePrefixes2 ()
- {
- xtw.WriteStartElement ("person");
- xtw.WriteAttributeString (null, "name", "http://somenamespace.com", "Driesen");
- xtw.WriteAttributeString (null, "initials", "http://othernamespace.com", "GD");
- xtw.WriteAttributeString (null, "firstName", "http://somenamespace.com", "Gert");
- xtw.WriteStartElement ("address");
- xtw.WriteAttributeString (null, "street", "http://somenamespace.com", "Campus");
- xtw.WriteAttributeString (null, "number", "http://othernamespace.com", "1");
- xtw.WriteAttributeString (null, "zip", "http://newnamespace.com", "3000");
- xtw.WriteAttributeString (null, "box", "http://othernamespace.com", "a");
- xtw.WriteEndElement ();
- xtw.WriteEndElement ();
- Assert.AreEqual (
- "<person" +
- " d1p1:name='Driesen'" +
- " d1p2:initials='GD'" +
- " d1p1:firstName='Gert'" +
- " xmlns:d1p2='http://othernamespace.com'" +
- " xmlns:d1p1='http://somenamespace.com'>" +
- "<address" +
- " d1p1:street='Campus'" +
- " d1p2:number='1'" +
- " d2p1:zip='3000'" +
- " d1p2:box='a'" +
- " xmlns:d2p1='http://newnamespace.com' />" +
- "</person>", StringWriterText, "#2");
- }
- [Test]
- public void AttributeNamespacesXmlnsXmlns ()
- {
- xtw.WriteStartElement ("foo");
- // If XmlTextWriter conforms to "Namespaces in XML"
- // when namespaceURI argument is null, then this
- // is not allowed (http://www.w3.org/TR/REC-xml-names/
- // Namespace Constraint: Prefix Declared), but seems
- // like XmlTextWriter just ignores XML namespace
- // constraints when namespaceURI argument is null.
- xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
- //Assert.Fail ("A prefix must not start with \"xml\".");
- }
- [Test]
- public void AttributeNamespacesXmlnsXmlns2 ()
- {
- // It is split from AttributeNamespacesXmlnsXmlns()
- // because depending on XmlWriter it is likely to cause
- // duplicate attribute error (XmlTextWriter is pretty
- // hacky, so it does not raise such errors).
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("", "xmlns", null, "http://abc.def");
- }
- [Test]
- public void WriteAttributeString_EmptyLocalName ()
- {
- xtw.WriteAttributeString ("", "something");
- Assert.AreEqual ("='something'", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("", "", "something");
- Assert.AreEqual ("='something'", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("", "http://somenamespace.com", "something");
- Assert.AreEqual ("d0p1:='something'", StringWriterText, "#3");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("x", "", "http://somenamespace.com", "something");
- Assert.AreEqual ("x:='something'", StringWriterText, "#4");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString (null, "something");
- Assert.AreEqual ("='something'", StringWriterText, "#5");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString (null, "", "something");
- Assert.AreEqual ("='something'", StringWriterText, "#6");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString (null, "http://somenamespace.com", "something");
- Assert.AreEqual ("d0p1:='something'", StringWriterText, "#7");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("x", null, "http://somenamespace.com", "something");
- Assert.AreEqual ("x:='something'", StringWriterText, "#8");
- }
- [Test]
- public void WriteStartAttribute_EmptyLocalName ()
- {
- xtw.WriteStartAttribute ("", "");
- Assert.AreEqual ("='", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("", "", "");
- Assert.AreEqual ("='", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("", "", "http://somenamespace.com");
- Assert.AreEqual ("d0p1:='", StringWriterText, "#3");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("x", "", "http://somenamespace.com");
- Assert.AreEqual ("x:='", StringWriterText, "#4");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("", null);
- Assert.AreEqual ("='", StringWriterText, "#5");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("", null, "");
- Assert.AreEqual ("='", StringWriterText, "#6");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("", null, "http://somenamespace.com");
- Assert.AreEqual ("d0p1:='", StringWriterText, "#7");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("x", null, "http://somenamespace.com");
- Assert.AreEqual ("x:='", StringWriterText, "#8");
- }
- [Test]
- public void AttributeWriteAttributeString ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("foo", "bar");
- Assert.AreEqual ("<foo foo='bar'", StringWriterText);
- xtw.WriteAttributeString ("bar", "");
- Assert.AreEqual ("<foo foo='bar' bar=''", StringWriterText);
- xtw.WriteAttributeString ("baz", null);
- Assert.AreEqual ("<foo foo='bar' bar='' baz=''", StringWriterText);
- xtw.WriteAttributeString ("hoge", "a\nb");
- Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a
b'", StringWriterText);
- xtw.WriteAttributeString ("fuga", " a\t\r\nb\t");
- Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a
b' fuga=' a\t
b\t'", StringWriterText);
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void AttributeWriteAttributeStringNotInsideOpenStartElement ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteString ("bar");
-
- xtw.WriteAttributeString ("baz", "quux");
- }
- [Test]
- public void AttributeWriteAttributeStringWithoutParentElement ()
- {
- xtw.WriteAttributeString ("foo", "bar");
- Assert.AreEqual ("foo='bar'", StringWriterText);
- xtw.WriteAttributeString ("baz", "quux");
- Assert.AreEqual ("foo='bar' baz='quux'", StringWriterText);
- }
- [Test]
- public void WriteStartElement_EmptyLocalName ()
- {
- xtw.WriteStartElement ("", "");
- Assert.AreEqual ("<", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("", "", "");
- Assert.AreEqual ("<", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("", "", "http://somenamespace.com");
- Assert.AreEqual ("<", StringWriterText, "#3");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("x", "", "http://somenamespace.com");
- Assert.AreEqual ("<x:", StringWriterText, "#4");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("", null);
- Assert.AreEqual ("<", StringWriterText, "#5");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("", null, "");
- Assert.AreEqual ("<", StringWriterText, "#6");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("", null, "http://somenamespace.com");
- Assert.AreEqual ("<", StringWriterText, "#7");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("x", null, "http://somenamespace.com");
- Assert.AreEqual ("<x:", StringWriterText, "#8");
- }
- [Test]
- public void WriteElementString_EmptyLocalName ()
- {
- xtw.WriteElementString ("", "");
- Assert.AreEqual ("< />", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString ("", "", "");
- Assert.AreEqual ("< />", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString ("", "http://somenamespace.com", "whatever");
- Assert.AreEqual ("< xmlns='http://somenamespace.com'>whatever</>", StringWriterText, "#3");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString ("", "http://somenamespace.com", "");
- Assert.AreEqual ("< xmlns='http://somenamespace.com' />", StringWriterText, "#4");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString (null, null);
- Assert.AreEqual ("< />", StringWriterText, "#5");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString (null, null, null);
- Assert.AreEqual ("< />", StringWriterText, "#6");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString (null, "http://somenamespace.com", "whatever");
- Assert.AreEqual ("< xmlns='http://somenamespace.com'>whatever</>", StringWriterText, "#7");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString (null, "http://somenamespace.com", null);
- Assert.AreEqual ("< xmlns='http://somenamespace.com' />", StringWriterText, "#8");
- }
- [Test]
- #if ONLY_1_1
- [Category ("NotDotNet")] // MS.NET 1.1 does not allow zero-length namespace URI
- #endif
- public void WriteStartElement_Prefix_EmptyNamespace ()
- {
- xtw.WriteStartElement ("x", "whatever", "");
- Assert.AreEqual ("<whatever", StringWriterText, "#1");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<whatever />", StringWriterText, "#2");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteStartElement_Prefix_NullNamespace ()
- {
- xtw.WriteStartElement ("x", "whatever", null);
- }
- [Test]
- public void WriteStartElement_XmlPrefix ()
- {
- xtw.WriteStartElement ("xml", "something", "http://www.w3.org/XML/1998/namespace");
- Assert.AreEqual ("<xml:something", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("XmL", null, "http://www.w3.org/XML/1998/namespace");
- Assert.AreEqual ("<XmL:", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("xmlsomething", "name", "http://www.w3.org/XML/1998/namespace");
- Assert.AreEqual ("<xmlsomething:name", StringWriterText, "#3");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteStartElement_XmlPrefix_Invalid1 ()
- {
- xtw.WriteStartElement ("xml", null, "http://somenamespace.com");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteStartElement_XmlPrefix_Invalid2 ()
- {
- xtw.WriteStartElement ("XmL", null, "http://somenamespace.com");
- }
- [Test]
- public void WriteStartElement_XmlPrefix_Invalid3 ()
- {
- // from XML 1.0 (third edition) specification:
- //
- // [Definition: A Name is a token beginning with a letter or one of a
- // few punctuation characters, and continuing with letters, digits,
- // hyphens, underscores, colons, or full stops, together known as name
- // characters.] Names beginning with the string "xml", or with any string
- // which would match (('X'|'x') ('M'|'m') ('L'|'l')), are reserved for
- // standardization in this or future versions of this specification.
- //
- // from the Namespaces in XML 1.0 specification:
- //
- // Prefixes beginning with the three-letter sequence x, m, l, in any case
- // combination, are reserved for use by XML and XML-related specifications.
- //
- // should this prefix then not be considered invalid ?
- //
- // both Mono and MS.NET 1.x/2.0 accept it though
- xtw.WriteStartElement ("xmlsomething", null, "http://somenamespace.com");
- Assert.AreEqual ("<xmlsomething:", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("XmLsomething", null, "http://somenamespace.com");
- Assert.AreEqual ("<XmLsomething:", StringWriterText, "#2");
- }
- [Test]
- public void CDataValid ()
- {
- xtw.WriteCData ("foo");
- Assert.AreEqual ("<![CDATA[foo]]>", StringWriterText, "WriteCData had incorrect output.");
- }
- [Test]
- public void CDataNull ()
- {
- xtw.WriteCData (null);
- Assert.AreEqual ("<![CDATA[]]>", StringWriterText, "WriteCData had incorrect output.");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void CDataInvalid ()
- {
- xtw.WriteCData("foo]]>bar");
- }
-
- [Test]
- public void CloseOpenElements ()
- {
- xtw.WriteStartElement("foo");
- xtw.WriteStartElement("bar");
- xtw.WriteStartElement("baz");
- xtw.Close();
- Assert.AreEqual ("<foo><bar><baz /></bar></foo>", StringWriterText,
- "Close didn't write out end elements properly.");
- }
- [Test]
- public void CloseWriteAfter ()
- {
- xtw.WriteElementString ("foo", "bar");
- xtw.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 {
- xtw.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 ("The Writer is closed.", e.Message, "Exception message is incorrect.");
- }
- try {
- xtw.WriteComment ("foo");
- Assert.Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
- } catch (InvalidOperationException) {
- // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
- }
- try {
- xtw.WriteProcessingInstruction ("foo", "bar");
- Assert.Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
- } catch (InvalidOperationException) {
- // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
- }
- try {
- xtw.WriteStartElement ("foo", "bar", "baz");
- Assert.Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
- } catch (InvalidOperationException) {
- // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
- }
- try {
- xtw.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 {
- xtw.WriteString ("foo");
- Assert.Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
- } catch (InvalidOperationException) {
- // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
- }
- }
- [Test]
- public void CommentValid ()
- {
- xtw.WriteComment ("foo");
- Assert.AreEqual ("<!--foo-->", StringWriterText, "WriteComment had incorrect output.");
- }
- [Test]
- public void CommentInvalid ()
- {
- try {
- xtw.WriteComment("foo-");
- Assert.Fail("Should have thrown an ArgumentException.");
- } catch (ArgumentException) { }
- try {
- xtw.WriteComment("foo-->bar");
- Assert.Fail("Should have thrown an ArgumentException.");
- } catch (ArgumentException) { }
- }
- [Test]
- public void ConstructorsAndBaseStream ()
- {
- Assert.IsTrue (Object.ReferenceEquals (null, this.xtw.BaseStream), "BaseStream property returned wrong value.");
- MemoryStream ms;
- StreamReader sr;
- XmlTextWriter xtw;
- ms = new MemoryStream ();
- xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
- xtw.WriteStartDocument ();
- xtw.Flush ();
- ms.Seek (0, SeekOrigin.Begin);
- sr = new StreamReader (ms, Encoding.Unicode);
- string expectedXmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
- string actualXmlDeclaration = sr.ReadToEnd();
- Assert.AreEqual (expectedXmlDeclaration, actualXmlDeclaration);
- Assert.IsTrue (Object.ReferenceEquals (ms, xtw.BaseStream), "BaseStream property returned wrong value.");
- ms = new MemoryStream ();
- xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
- xtw.WriteStartDocument (true);
- xtw.Flush ();
- ms.Seek (0, SeekOrigin.Begin);
- sr = new StreamReader (ms, Encoding.Unicode);
- Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
- ms = new MemoryStream ();
- xtw = new XmlTextWriter (ms, new UTF8Encoding ());
- xtw.WriteStartDocument ();
- xtw.Flush ();
- ms.Seek (0, SeekOrigin.Begin);
- sr = new StreamReader (ms, Encoding.UTF8);
- Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
- ms = new MemoryStream ();
- xtw = new XmlTextWriter (ms, null);
- xtw.WriteStartDocument ();
- xtw.Flush ();
- ms.Seek (0, SeekOrigin.Begin);
- sr = new StreamReader (ms, Encoding.UTF8);
- Assert.AreEqual ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
- ms = new MemoryStream ();
- xtw = new XmlTextWriter (ms, null);
- xtw.WriteStartDocument (true);
- xtw.Flush ();
- ms.Seek (0, SeekOrigin.Begin);
- sr = new StreamReader (ms, Encoding.UTF8);
- Assert.AreEqual ("<?xml version=\"1.0\" standalone=\"yes\"?>", sr.ReadToEnd ());
- Assert.IsTrue (Object.ReferenceEquals (ms, xtw.BaseStream), "BaseStream property returned wrong value.");
- }
- [Test]
- public void DocumentStart ()
- {
- xtw.WriteStartDocument ();
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?>", StringWriterText,
- "XmlDeclaration is incorrect.");
- try {
- xtw.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 ("WriteStartDocument should be the first call.", e.Message, "Exception message is incorrect.");
- }
- xtw = new XmlTextWriter (sw = new StringWriter ());
- xtw.QuoteChar = '\'';
- xtw.WriteStartDocument (true);
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='yes'?>", StringWriterText);
- xtw = new XmlTextWriter (sw = new StringWriter ());
- xtw.QuoteChar = '\'';
- xtw.WriteStartDocument (false);
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", StringWriterText);
- }
- [Test]
- public void ElementAndAttributeSameXmlns ()
- {
- xtw.WriteStartElement ("ped", "foo", "urn:foo");
- xtw.WriteStartAttribute ("ped", "foo", "urn:foo");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<ped:foo ped:foo='' xmlns:ped='urn:foo' />", StringWriterText);
- }
- [Test]
- [Category ("NotDotNet")]
- public void ElementXmlnsNeedEscape ()
- {
- xtw.WriteStartElement ("test", "foo", "'");
- xtw.WriteEndElement ();
- // MS.NET output is : xmlns:test='''
- Assert.AreEqual ("<test:foo xmlns:test=''' />", StringWriterText);
- }
- [Test]
- public void ElementEmpty ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<foo />", StringWriterText, "Incorrect output.");
- }
- [Test]
- public void ElementWriteElementString ()
- {
- xtw.WriteElementString ("foo", "bar");
- Assert.AreEqual ("<foo>bar</foo>", StringWriterText, "WriteElementString has incorrect output.");
- xtw.WriteElementString ("baz", "");
- Assert.AreEqual ("<foo>bar</foo><baz />", StringWriterText, "#2");
- xtw.WriteElementString ("quux", null);
- Assert.AreEqual ("<foo>bar</foo><baz /><quux />", StringWriterText, "#3");
- xtw.WriteElementString ("", "quuux");
- Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText, "#4");
- xtw.WriteElementString (null, "quuuux");
- Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText, "#5");
- }
- [Test]
- public void FormattingTest ()
- {
- xtw.Formatting = Formatting.Indented;
- xtw.WriteStartDocument ();
- xtw.WriteStartElement ("foo");
- xtw.WriteElementString ("bar", "");
- xtw.Close ();
- Assert.AreEqual (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0} <bar />{0}</foo>", Environment.NewLine), StringWriterText);
- }
- [Test]
- public void FormattingInvalidXmlForFun ()
- {
- xtw.Formatting = Formatting.Indented;
- xtw.IndentChar = 'x';
- xtw.WriteStartDocument ();
- xtw.WriteStartElement ("foo");
- xtw.WriteStartElement ("bar");
- xtw.WriteElementString ("baz", "");
- xtw.Close ();
- Assert.AreEqual (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0}xx<bar>{0}xxxx<baz />{0}xx</bar>{0}</foo>", Environment.NewLine), StringWriterText);
- }
- [Test]
- public void FormattingFromRemarks ()
- {
- // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
- xtw.Formatting = Formatting.Indented;
- xtw.WriteStartElement ("ol");
- xtw.WriteStartElement ("li");
- xtw.WriteString ("The big "); // This means "li" now has a mixed content model.
- xtw.WriteElementString ("b", "E");
- xtw.WriteElementString ("i", "lephant");
- xtw.WriteString (" walks slowly.");
- xtw.WriteEndElement ();
- xtw.WriteEndElement ();
- Assert.AreEqual (String.Format ("<ol>{0} <li>The big <b>E</b><i>lephant</i> walks slowly.</li>{0}</ol>", Environment.NewLine), StringWriterText);
- }
- [Test]
- public void LookupPrefix ()
- {
- xtw.WriteStartElement ("root");
- xtw.WriteStartElement ("one");
- xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
- xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
- Assert.AreEqual ("foo", xtw.LookupPrefix ("http://abc.def"), "#1");
- Assert.AreEqual ("bar", xtw.LookupPrefix ("http://ghi.jkl"), "#2");
- xtw.WriteEndElement ();
- xtw.WriteStartElement ("two");
- xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
- xtw.WriteString("quux");
- Assert.AreEqual ("baz", xtw.LookupPrefix ("http://mno.pqr"), "#3");
- Assert.IsNull (xtw.LookupPrefix ("http://abc.def"), "#4");
- Assert.IsNull (xtw.LookupPrefix ("http://ghi.jkl"), "#5");
- Assert.IsNull (xtw.LookupPrefix ("http://bogus"), "#6");
- }
- [Test]
- public void NamespacesAttributesPassingInNamespaces ()
- {
- xtw.Namespaces = false;
- xtw.WriteStartElement ("foo");
- // These shouldn't throw any exceptions since they don't pass in
- // a namespace.
- xtw.WriteAttributeString ("bar", "baz");
- xtw.WriteAttributeString ("", "a", "", "b");
- xtw.WriteAttributeString (null, "c", "", "d");
- xtw.WriteAttributeString ("", "e", null, "f");
- xtw.WriteAttributeString (null, "g", null, "h");
- Assert.AreEqual ("<foo bar='baz' a='b' c='d' e='f' g='h'", StringWriterText);
- }
- [Test]
- public void NamespacesElementsPassingInNamespaces ()
- {
- xtw.Namespaces = false;
- // These shouldn't throw any exceptions since they don't pass in
- // a namespace.
- xtw.WriteElementString ("foo", "bar");
- xtw.WriteStartElement ("baz");
- xtw.WriteStartElement ("quux", "");
- xtw.WriteStartElement ("quuux", null);
- xtw.WriteStartElement (null, "a", null);
- xtw.WriteStartElement (null, "b", "");
- xtw.WriteStartElement ("", "c", null);
- xtw.WriteStartElement ("", "d", "");
- Assert.AreEqual ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", StringWriterText);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesElementsPassingInNamespacesInvalid1 ()
- {
- // These should throw ArgumentException because they pass in a
- // namespace when Namespaces = false.
- xtw.Namespaces = false;
- xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesElementsPassingInNamespacesInvalid2 ()
- {
- xtw.Namespaces = false;
- xtw.WriteStartElement ("foo", "http://netsack.com/");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesElementsPassingInNamespacesInvalid3 ()
- {
- xtw.Namespaces = false;
- xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesElementsPassingInNamespacesInvalid4 ()
- {
- xtw.Namespaces = false;
- xtw.WriteStartElement ("foo", "bar", null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesElementsPassingInNamespacesInvalid5 ()
- {
- xtw.Namespaces = false;
- xtw.WriteStartElement ("foo", "bar", "");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesElementsPassingInNamespacesInvalid6 ()
- {
- xtw.Namespaces = false;
- xtw.WriteStartElement ("foo", "", "");
- }
- [Test]
- public void NamespacesNoNamespaceClearsDefaultNamespace ()
- {
- xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
- xtw.WriteStartElement(String.Empty, "bar", String.Empty);
- xtw.WriteElementString("baz", String.Empty, String.Empty);
- xtw.WriteEndElement();
- xtw.WriteEndElement();
- Assert.AreEqual ("<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>",
- StringWriterText, "XmlTextWriter is incorrectly outputting namespaces.");
- }
- [Test]
- public void NamespacesPrefix ()
- {
- xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
- xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
- xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
- xtw.WriteEndElement ();
- xtw.WriteEndElement ();
- Assert.AreEqual ("<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>",
- StringWriterText, "XmlTextWriter is incorrectly outputting prefixes.");
- }
- [Test]
- #if ONLY_1_1
- [Category ("NotDotNet")] // MS.NET 1.1 does not allow zero-length namespace URI
- #endif
- public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty ()
- {
- xtw.WriteStartElement ("foo", "bar", "");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesPrefixWithEmptyAndNullNamespaceNull ()
- {
- xtw.WriteStartElement ("foo", "bar", null);
- }
- [Test]
- public void NamespacesSettingWhenWriteStateNotStart ()
- {
- xtw.WriteStartElement ("foo");
- try {
- xtw.Namespaces = false;
- Assert.Fail ("Expected an InvalidOperationException.");
- } catch (InvalidOperationException) {}
- Assert.IsTrue (xtw.Namespaces);
- }
- [Test]
- public void ProcessingInstructionValid ()
- {
- xtw.WriteProcessingInstruction("foo", "bar");
- Assert.AreEqual ("<?foo bar?>", StringWriterText, "WriteProcessingInstruction had incorrect output.");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ProcessingInstructionInvalid1 ()
- {
- xtw.WriteProcessingInstruction("fo?>o", "bar");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ProcessingInstructionInvalid2 ()
- {
- xtw.WriteProcessingInstruction("foo", "ba?>r");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ProcessingInstructionInvalid3 ()
- {
- xtw.WriteProcessingInstruction("", "bar");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ProcessingInstructionInvalid4 ()
- {
- xtw.WriteProcessingInstruction(null, "bar");
- }
- [Test]
- public void QuoteCharDoubleQuote ()
- {
- xtw.QuoteChar = '"';
- // version, encoding, standalone
- xtw.WriteStartDocument (true);
-
- // namespace declaration
- xtw.WriteElementString ("foo", "http://netsack.com", "bar");
- Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><foo xmlns=\"http://netsack.com\">bar</foo>", StringWriterText);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void QuoteCharInvalid ()
- {
- xtw.QuoteChar = 'x';
- }
- [Test]
- public void WriteBase64 ()
- {
- UTF8Encoding encoding = new UTF8Encoding();
- byte[] fooBar = encoding.GetBytes("foobar");
- xtw.WriteBase64 (fooBar, 0, 6);
- Assert.AreEqual ("Zm9vYmFy", StringWriterText);
- try {
- xtw.WriteBase64 (fooBar, 3, 6);
- Assert.Fail ("Expected an Argument Exception to be thrown.");
- } catch (ArgumentException) {}
- try {
- xtw.WriteBase64 (fooBar, -1, 6);
- Assert.Fail ("Expected an Argument Exception to be thrown.");
- } catch (ArgumentOutOfRangeException) {}
- try {
- xtw.WriteBase64 (fooBar, 3, -1);
- Assert.Fail ("Expected an Argument Exception to be thrown.");
- } catch (ArgumentOutOfRangeException) {}
- try {
- xtw.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};
- xtw.WriteBinHex (bytes, 0, 11);
- Assert.AreEqual ("040E22365E7286C2FF0005", StringWriterText);
- }
- [Test]
- public void WriteCharEntity ()
- {
- xtw.WriteCharEntity ('a');
- Assert.AreEqual ("a", StringWriterText);
- xtw.WriteCharEntity ('A');
- Assert.AreEqual ("aA", StringWriterText);
- xtw.WriteCharEntity ('1');
- Assert.AreEqual ("aA1", StringWriterText);
- xtw.WriteCharEntity ('K');
- Assert.AreEqual ("aA1K", StringWriterText);
- try {
- xtw.WriteCharEntity ((char)0xd800);
- } catch (ArgumentException) {}
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void WriteEndAttribute ()
- {
- xtw.WriteEndAttribute ();
- }
- [Test]
- public void WriteEndDocument ()
- {
- try {
- xtw.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 ()
- {
- xtw.WriteStartDocument ();
- try
- {
- xtw.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 ()
- {
- xtw.WriteStartDocument ();
- xtw.WriteStartElement ("foo");
- xtw.WriteStartAttribute ("bar", null);
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='", StringWriterText);
- xtw.WriteEndDocument ();
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", StringWriterText);
- Assert.AreEqual (WriteState.Start, xtw.WriteState);
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void WriteEndElement ()
- {
- // no matching StartElement
- xtw.WriteEndElement ();
- }
- [Test]
- public void WriteEndElement2 ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<foo />", StringWriterText);
- xtw.WriteStartElement ("bar");
- xtw.WriteStartAttribute ("baz", null);
- xtw.WriteEndElement ();
- Assert.AreEqual ("<foo /><bar baz='' />", StringWriterText);
- }
- [Test]
- public void FullEndElement ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteFullEndElement ();
- Assert.AreEqual ("<foo></foo>", StringWriterText);
- xtw.WriteStartElement ("bar");
- xtw.WriteAttributeString ("foo", "bar");
- xtw.WriteFullEndElement ();
- Assert.AreEqual ("<foo></foo><bar foo='bar'></bar>", StringWriterText);
- xtw.WriteStartElement ("baz");
- xtw.WriteStartAttribute ("bar", null);
- xtw.WriteFullEndElement ();
- Assert.AreEqual ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", StringWriterText);
- }
- [Test]
- public void WriteQualifiedName ()
- {
- xtw.WriteStartElement (null, "test", null);
- xtw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
- xtw.WriteQualifiedName ("bob", "http://localhost/");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<test xmlns:me='http://localhost/'>me:bob</test>", StringWriterText);
- }
- [Test]
- public void WriteQualifiedNameNonDeclaredAttribute ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteStartAttribute ("a", "");
- xtw.WriteQualifiedName ("attr", "urn:a");
- xtw.WriteWhitespace (" ");
- xtw.WriteQualifiedName ("attr", "urn:b");
- xtw.WriteEndAttribute ();
- xtw.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 ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteQualifiedName ("abc", "urn:abc");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteQualifiedNameNonNCName ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xmlns", "urn:default");
- xtw.WriteStartElement ("child");
- xtw.WriteStartAttribute ("a", "");
- xtw.WriteQualifiedName ("x:def", "urn:def");
- }
- [Test]
- public void WriteRaw ()
- {
- xtw.WriteRaw("&<>\"'");
- Assert.AreEqual ("&<>\"'", StringWriterText);
- xtw.WriteRaw(null);
- Assert.AreEqual ("&<>\"'", StringWriterText);
- xtw.WriteRaw("");
- Assert.AreEqual ("&<>\"'", StringWriterText);
- // bug #77623
- xtw.WriteRaw ("{0}{1}");
- }
- [Test]
- public void WriteRawInvalidInAttribute ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteStartAttribute ("bar", null);
- xtw.WriteRaw ("&<>\"'");
- xtw.WriteEndAttribute ();
- xtw.WriteEndElement ();
- Assert.AreEqual ("<foo bar='&<>\"'' />", StringWriterText);
- }
- [Test]
- public void WriteStateTest ()
- {
- Assert.AreEqual (WriteState.Start, xtw.WriteState);
- xtw.WriteStartDocument ();
- Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
- xtw.WriteStartElement ("root");
- Assert.AreEqual (WriteState.Element, xtw.WriteState);
- xtw.WriteElementString ("foo", "bar");
- Assert.AreEqual (WriteState.Content, xtw.WriteState);
- xtw.Close ();
- Assert.AreEqual (WriteState.Closed, xtw.WriteState);
- }
- [Test]
- public void WriteString ()
- {
- xtw.WriteStartDocument ();
- try {
- xtw.WriteString("foo");
- Assert.Fail ("should raise an error.");
- } catch (InvalidOperationException) {}
- }
- [Test]
- public void WriteString2 ()
- {
- xtw.WriteStartDocument ();
- // Testing attribute values
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("bar", "&<>");
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='&<>'", StringWriterText);
- }
- [Test]
- public void WriteAttributeStringSingleQuoteChar()
- {
- // When QuoteChar is single quote then replaces single quotes within attributes
- // but not double quotes.
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("bar", "\"baz\"");
- xtw.WriteAttributeString ("quux", "'baz'");
- Assert.AreEqual ("<foo bar='\"baz\"' quux=''baz''", StringWriterText);
- }
- [Test]
- public void WriteAttributeStringDoubleQuoteChar()
- {
- // When QuoteChar is double quote then replaces double quotes within attributes
- // but not single quotes.
- xtw.QuoteChar = '"';
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("bar", "\"baz\"");
- xtw.WriteAttributeString ("quux", "'baz'");
- Assert.AreEqual ("<foo bar=\""baz"\" quux=\"'baz'\"", StringWriterText);
- }
- [Test]
- public void WriteStringWithEntities()
- {
- // Testing element values
- xtw.QuoteChar = '\'';
- xtw.WriteElementString ("foo", "&<>\"'");
- Assert.AreEqual ("<foo>&<>\"'</foo>", StringWriterText);
- }
- [Test]
- public void XmlLang ()
- {
- Assert.IsNull (xtw.XmlLang);
-
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
- Assert.AreEqual ("langfoo", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo'", StringWriterText);
- xtw.WriteAttributeString ("boo", "yah");
- Assert.AreEqual ("langfoo", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
-
- xtw.WriteElementString("bar", "baz");
- Assert.AreEqual ("langfoo", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
-
- xtw.WriteString("baz");
- Assert.AreEqual ("langfoo", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
-
- xtw.WriteStartElement ("quux");
- xtw.WriteStartAttribute ("xml", "lang", null);
- Assert.AreEqual ("langfoo", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
-
- xtw.WriteString("langbar");
- // Commented out there: it is implementation-dependent.
- // and incompatible between .NET 1.0 and 1.1
- // Assert.AreEqual ("langfoo", xtw.XmlLang);
- // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
-
- xtw.WriteEndAttribute ();
- // Commented out there: it is implementation-dependent.
- // and incompatible between .NET 1.0 and 1.1
- // Assert.AreEqual ("langbar", xtw.XmlLang);
- // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
- // check if xml:lang repeats output even if same as current scope.
- xtw.WriteStartElement ("joe");
- xtw.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'", StringWriterText);
-
- xtw.WriteElementString ("quuux", "squonk");
- Assert.AreEqual ("langbar", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
- xtw.WriteEndElement ();
- xtw.WriteEndElement ();
- Assert.AreEqual ("langfoo", xtw.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>", StringWriterText);
-
- xtw.WriteEndElement ();
- Assert.IsNull (xtw.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>", StringWriterText);
-
- xtw.Close ();
- Assert.IsNull (xtw.XmlLang);
- }
- // TODO: test operational aspects
- [Test]
- public void XmlSpaceTest ()
- {
- xtw.WriteStartElement ("foo");
- Assert.AreEqual (XmlSpace.None, xtw.XmlSpace, "#1");
- xtw.WriteStartElement ("bar");
- xtw.WriteAttributeString ("xml", "space", null, "preserve");
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#2");
- Assert.AreEqual ("<foo><bar xml:space='preserve'", StringWriterText, "#3");
- xtw.WriteStartElement ("baz");
- xtw.WriteAttributeString ("xml", "space", null, "preserve");
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#4");
- Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText, "#5");
- xtw.WriteStartElement ("quux");
- xtw.WriteStartAttribute ("xml", "space", null);
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#6");
- Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText, "#7");
- // Commented out there: it is implementation-dependent
- // and incompatible between .NET 1.0 and 1.1
- xtw.WriteString ("default");
- // Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
- // Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
-
- xtw.WriteEndAttribute ();
- Assert.AreEqual (XmlSpace.Default, xtw.XmlSpace, "#8");
- Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText, "#9");
- xtw.WriteEndElement ();
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#10");
- xtw.WriteEndElement ();
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#11");
- xtw.WriteEndElement ();
- Assert.AreEqual (XmlSpace.None, xtw.XmlSpace, "#12");
- xtw.WriteStartElement ("quux");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void XmlSpaceTestInvalidValue1 ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xml", "space", null, "bubba");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void XmlSpaceTestInvalidValue2 ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void XmlSpaceTestInvalidValue3 ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xml", "space", null, "Default");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void XmlSpaceTestInvalidValue4 ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xml", "space", null, "bubba");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteWhitespaceNonWhitespace ()
- {
- xtw.WriteWhitespace ("x");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteWhitespace_Null ()
- {
- xtw.WriteWhitespace ((string) null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteWhitespace_Empty ()
- {
- xtw.WriteWhitespace (string.Empty);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteNmToken_Null ()
- {
- xtw.WriteNmToken ((string) null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteNmToken_Empty ()
- {
- xtw.WriteNmToken (string.Empty);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteNmToken_InvalidChars ()
- {
- xtw.WriteNmToken ("\uFFFF");
- }
- [Test]
- public void WriteNmToken ()
- {
- xtw.WriteNmToken ("some:name");
- Assert.AreEqual ("some:name", StringWriterText);
- }
- [Test]
- public void XmlSpaceRaw ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteStartAttribute ("xml", "space", null);
- Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
- Assert.AreEqual ("<foo xml:space='", StringWriterText);
- xtw.WriteString ("default");
- // Commented out there: it is implementation-dependent
- // and incompatible between .NET 1.0 and 1.1
- // Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
- // Assert.AreEqual ("<foo xml:space='", StringWriterText);
- xtw.WriteEndAttribute ();
- Assert.AreEqual (XmlSpace.Default, xtw.XmlSpace);
- Assert.AreEqual ("<foo xml:space='default'", StringWriterText);
- }
- [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 ()
- {
- xtw.WriteStartElement ("a");
- xtw.WriteWhitespace ("\n\t");
- xtw.WriteStartElement ("b");
- xtw.WriteWhitespace ("\n\t");
- xtw.WriteEndElement ();
- xtw.WriteWhitespace ("\n");
- xtw.WriteEndElement ();
- xtw.WriteWhitespace ("\n");
- xtw.Flush ();
- Assert.AreEqual ("<a>\n\t<b>\n\t</b>\n</a>\n", StringWriterText);
- }
- [Test]
- public void FlushDoesntCloseTag ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("bar", "baz");
- xtw.Flush ();
- Assert.AreEqual ("<foo bar='baz'", StringWriterText);
- }
- [Test]
- public void WriteWhitespaceClosesTag ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("bar", "baz");
- xtw.WriteWhitespace (" ");
- Assert.AreEqual ("<foo bar='baz'> ", StringWriterText);
- }
- [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 ()
- {
- xtw.WriteStartDocument ();
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xmlns", "probe");
- Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"), "#1");
- xtw.WriteStartElement ("b");
- Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"), "#2");
- xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
- xtw.WriteEndElement (); // b2
- xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
- xtw.WriteEndElement (); // b2
- xtw.WriteEndElement (); // b
- xtw.WriteEndElement (); // foo
- xtw.WriteEndDocument ();
- xtw.Close ();
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 xmlns='' /></b></foo>", StringWriterText, "#3");
- }
- [Test]
- public void DontOutputRemovalDefaultNSDeclaration2 ()
- {
- xtw.WriteStartDocument ();
- // IMPORTANT DIFFERENCE!! ns = "", not null
- xtw.WriteStartElement ("foo", "");
- xtw.WriteAttributeString ("xmlns", "probe");
- Assert.IsNull (xtw.LookupPrefix ("probe"), "#1");
- xtw.WriteStartElement ("b");
- Assert.IsNull (xtw.LookupPrefix ("probe"), "#2");
- xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
- xtw.WriteEndElement (); // b2
- xtw.WriteStartElement (null, "b2", ""); // *Don't* output xmlns=""
- xtw.WriteEndElement (); // b2
- xtw.WriteEndElement (); // b
- xtw.WriteEndElement (); // foo
- xtw.WriteEndDocument ();
- xtw.Close ();
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 /></b></foo>", StringWriterText, "#3");
- }
- [Test]
- public void DoOutputRemovalDefaultNSDeclaration ()
- {
- xtw.WriteStartElement ("docelem", "a-namespace");
-
- XmlDocument doc = new XmlDocument ();
- doc.CreateElement ("hola").WriteTo (xtw);
- // This means, WriteTo never passes null NamespaceURI argument to XmlWriter.
- xtw.WriteEndElement ();
- xtw.Close ();
- Assert.AreEqual ("<docelem xmlns='a-namespace'><hola xmlns='' /></docelem>", StringWriterText);
- }
- [Test]
- public void WriteAttributeTakePrecedenceOnXmlns ()
- {
- xtw.WriteStartElement ("root", "urn:foo");
- xtw.WriteAttributeString ("xmlns", "urn:bar");
- xtw.WriteEndElement ();
- xtw.Close ();
- Assert.AreEqual ("<root xmlns='urn:bar' />", StringWriterText);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void LookupPrefixNull ()
- {
- xtw.LookupPrefix (null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void LookupPrefixEmpty ()
- {
- xtw.LookupPrefix (String.Empty);
- }
- [Test]
- public void LookupPrefixIgnoresXmlnsAttribute ()
- {
- Assert.IsNull (xtw.LookupPrefix ("urn:foo"));
- xtw.WriteStartElement ("root");
- Assert.IsNull (xtw.LookupPrefix ("urn:foo"));
- xtw.WriteAttributeString ("xmlns", "urn:foo");
- // Surprisingly to say, it is ignored!!
- Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
- xtw.WriteStartElement ("hoge");
- // (still after flushing previous start element.)
- Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
- xtw.WriteStartElement ("fuga", "urn:foo");
- // Is this testing on the correct way? Yes, here it is.
- Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
- }
- [Test]
- public void WriteInvalidNames ()
- {
- xtw.WriteStartElement ("foo<>");
- xtw.WriteAttributeString ("ho<>ge", "value");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeWriteStartAttributePrefixWithoutNS ()
- {
- xtw.WriteStartAttribute ("some", "foo", null);
- }
- [Test]
- public void AttributeWriteStartAttributeXmlnsNullNS ()
- {
- xtw.WriteStartAttribute ("xmlns", "foo", null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeWriteEndAttributeXmlnsNullNs ()
- {
- // This test checks if the specified namespace URI is
- // incorrectly empty or not. Compare it with
- // AttributeWriteStartAttributeXmlnsNullNS().
- xtw.WriteStartAttribute ("xmlns", "foo", null);
- xtw.WriteEndAttribute ();
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS ()
- {
- xtw.WriteStartAttribute ("xmlns", "foo", "urn:foo");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS ()
- {
- xtw.WriteStartAttribute ("", "xmlns", "urn:foo");
- }
- [Test]
- public void WriteRawProceedToProlog ()
- {
- XmlTextWriter xtw = new XmlTextWriter (new StringWriter ());
- xtw.WriteRaw ("");
- Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
- }
- [Test]
- public void Indent ()
- {
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml ("<root><test>test<foo></foo>string</test><test>string</test></root>");
- StringWriter sw = new StringWriter ();
- sw.NewLine = "_";
- XmlTextWriter xtw = new XmlTextWriter (sw);
- xtw.Formatting = Formatting.Indented;
- doc.WriteContentTo (xtw);
- Assert.AreEqual (@"<root>_ <test>test<foo></foo>string</test>_ <test>string</test>_</root>", sw.ToString ());
- }
- [Test]
- public void Indent2 ()
- {
- StringWriter sw = new StringWriter ();
- XmlTextWriter xtw = new XmlTextWriter (sw);
- xtw.Formatting = Formatting.Indented;
- // sadly, this silly usage of this method is actually
- // used in WriteNode() in MS.NET.
- xtw.WriteProcessingInstruction ("xml",
- "version=\"1.0\"");
- xtw.WriteComment ("sample XML fragment");
- Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
- @"<?xml version=""1.0""?>{0}" +
- "<!--sample XML fragment-->", Environment.NewLine),
- sw.ToString ());
- }
- [Test]
- public void Indent3 ()
- {
- XmlDocument doc = new XmlDocument ();
- doc.PreserveWhitespace = true;
- string s;
- doc.LoadXml ("<root><element></element><!-- comment indented --><element>sample <!-- comment non-indented --></element></root>");
- s = GetIndentedOutput (doc.DocumentElement);
- Assert.AreEqual (s, String.Format ("<root>{0} <element>{0} </element>{0} <!-- comment indented -->{0} <element>sample <!-- comment non-indented --></element>{0}</root>", "\n"), "#1");
- doc.LoadXml ("<root> \n<mid> \n<mid> \n<child attr='value'>sample <nested attr='value' /> string</child> <child2 attr='value'>sample string</child2> <empty attr='value'/>\n<a>test</a> \n</mid> <returnValue> <returnType>System.String</returnType> </returnValue> </mid> </root>");
- s = GetIndentedOutput (doc.DocumentElement);
- Assert.AreEqual (s, String.Format ("<root> {0}<mid> {0}<mid> {0}<child attr='value'>sample <nested attr='value' /> string</child> <child2 attr='value'>sample string</child2> <empty attr='value' />{0}<a>test</a> {0}</mid> <returnValue> <returnType>System.String</returnType> </returnValue> </mid> </root>", "\n"), "#2");
- doc.LoadXml ("<!-- after /MemberType and after /returnValue --><root><MemberType>blah</MemberType>\n <returnValue><returnType>System.String</returnType></returnValue>\n <Docs><summary>text</summary><value>text<see cref='ttt' /></value><remarks/></Docs></root>");
- s = GetIndentedOutput (doc.DocumentElement);
- Assert.AreEqual (s, String.Format ("<root>{0} <MemberType>blah</MemberType>{0} <returnValue><returnType>System.String</returnType></returnValue>{0} <Docs><summary>text</summary><value>text<see cref='ttt' /></value><remarks /></Docs></root>", "\n"), "#3");
- }
- string GetIndentedOutput (XmlNode n)
- {
- StringWriter sw = new StringWriter ();
- sw.NewLine = "\n";
- XmlTextWriter xtw = new XmlTextWriter (sw);
- xtw.QuoteChar = '\'';
- xtw.Formatting = Formatting.Indented;
- n.WriteTo (xtw);
- return sw.ToString ();
- }
- [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 ().
- xtw.WriteRaw ("");
- xtw.WriteString ("foo");
- Assert.AreEqual (WriteState.Content, xtw.WriteState);
- }
- [Test]
- public void LookupOverridenPrefix ()
- {
- xtw.WriteStartElement ("out");
- xtw.WriteAttributeString ("xmlns", "baz", "http://www.w3.org/2000/xmlns/", "xyz");
- xtw.WriteStartElement ("baz", "foo", "abc");
- Assert.IsNull (xtw.LookupPrefix ("xyz"));
- }
- [Test]
- public void DuplicatingNamespaceMappingInAttributes ()
- {
- xtw.WriteStartElement ("out");
- xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
- xtw.WriteAttributeString ("p", "bar", "urn:bar", "xyz");
- xtw.WriteAttributeString ("p", "baz", "urn:baz", "xyz");
- xtw.WriteStartElement ("out");
- xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
- xtw.WriteStartElement ("out");
- xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
- xtw.WriteEndElement ();
- xtw.WriteEndElement ();
- xtw.WriteEndElement ();
- string xml = sw.ToString ();
- Assert.IsTrue (xml.IndexOf ("p:foo='xyz'") > 0, "p:foo" + ". output is " + xml);
- Assert.IsTrue (xml.IndexOf ("d1p1:bar='xyz'") > 0, "d1p1:bar" + ". output is " + xml);
- Assert.IsTrue (xml.IndexOf ("d1p2:baz='xyz'") > 0, "d1p1:baz" + ". output is " + xml);
- Assert.IsTrue (xml.IndexOf ("xmlns:d1p2='urn:baz'") > 0, "xmlns:d1p2" + ". output is " + xml);
- Assert.IsTrue (xml.IndexOf ("xmlns:d1p1='urn:bar'") > 0, "xmlns:d1p1" + ". output is " + xml);
- Assert.IsTrue (xml.IndexOf ("xmlns:p='urn:foo'") > 0, "xmlns:p" + ". output is " + xml);
- Assert.IsTrue (xml.IndexOf ("<out p:foo='xyz'><out p:foo='xyz' /></out></out>") > 0, "remaining" + ". output is " + xml);
- }
- [Test]
- public void WriteXmlSpaceIgnoresNS ()
- {
- xtw.WriteStartElement ("root");
- xtw.WriteAttributeString ("xml", "space", "abc", "preserve");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<root xml:space='preserve' />", sw.ToString ());
- }
- [Test] // bug #75546
- public void WriteEmptyNSQNameInAttribute ()
- {
- XmlTextWriter xtw = new XmlTextWriter (TextWriter.Null);
- xtw.WriteStartElement ("foo", "urn:goo");
- xtw.WriteAttributeString ("xmlns:bar", "urn:bar");
- xtw.WriteStartAttribute ("foo", "");
- xtw.WriteQualifiedName ("n1", "urn:bar");
- xtw.WriteEndAttribute ();
- xtw.WriteStartAttribute ("foo", "");
- xtw.WriteQualifiedName ("n2", "");
- xtw.WriteEndAttribute ();
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- // cannot bind any prefix to "http://www.w3.org/2000/xmlns/".
- public void WriteQualifiedNameXmlnsError ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteQualifiedName ("", "http://www.w3.org/2000/xmlns/");
- }
- [Test]
- public void WriteDocType ()
- {
- // we have the following test matrix:
- //
- // | name | publicid | systemid | subset|
- // |------------------------------------|
- // | X | X | X | X | #01
- // | X | E | X | X | #02
- // | X | X | E | X | #03
- // | X | X | X | E | #04
- // | X | E | E | X | #05
- // | X | X | E | E | #06
- // | X | E | X | E | #07
- // | X | E | E | E | #08
- // | X | N | X | X | #09
- // | X | X | N | X | #10
- // | X | X | X | N | #11
- // | X | N | N | X | #12
- // | X | X | N | N | #13
- // | X | N | X | N | #14
- // | X | N | N | N | #15
- //
- // Legend:
- // -------
- // X = Has value
- // E = Zero-length string
- // N = Null
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
- Assert.AreEqual ("<!DOCTYPE test PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'" +
- " 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'[sub]>",
- sw.ToString (), "#01");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", string.Empty,
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
- " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
- sw.ToString (), "#02");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- string.Empty, "sub");
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
- " \"\"[sub]>",
- sw.ToString (), "#03");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
- " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
- sw.ToString (), "#04");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", string.Empty, string.Empty, "sub");
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[sub]>",
- sw.ToString (), "#05");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- string.Empty, string.Empty);
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
- " \"\"[]>",
- sw.ToString (), "#06");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", string.Empty,
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
- " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
- sw.ToString (), "#07");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", string.Empty, string.Empty, string.Empty);
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[]>",
- sw.ToString (), "#08");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", (string) null,
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
- Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
- " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
- sw.ToString (), "#09");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- (string) null, "sub");
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
- " \"\"[sub]>",
- sw.ToString (), "#10");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
- " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
- sw.ToString (), "#11");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", (string) null, (string) null, "sub");
- Assert.AreEqual ("<!DOCTYPE test[sub]>",
- sw.ToString (), "#12");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- (string) null, (string) null);
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
- " \"\">",
- sw.ToString (), "#13");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", (string) null,
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
- Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
- " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
- sw.ToString (), "#14");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", (string) null, (string) null, (string) null);
- Assert.AreEqual ("<!DOCTYPE test>",
- sw.ToString (), "#15");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteDocType_EmptyName ()
- {
- xtw.WriteDocType (string.Empty, "-//W3C//DTD XHTML 1.0 Strict//EN",
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteDocType_NullName ()
- {
- xtw.WriteDocType ((string) null, "-//W3C//DTD XHTML 1.0 Strict//EN",
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
- }
- [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]
- public void InvalidCharIsWrittenAsSillyReferences ()
- {
- // I can't say how MS XmlTextWriter is silly.
- // The expected output is *not* well-formed XML.
- // Everyone have to make sure that he or she does
- // not write invalid characters directly so that
- // the output XML string can be fed by other XML
- // processors.
- // The funny thing is that XmlTextWriter spends
- // significant performance on checking invalid
- // characters, but results in nothing.
- xtw.WriteElementString ("a", "\x0");
- NUnit.Framework.Assert.AreEqual ("<a>�</a>",
- StringWriterText);
- }
- [Test] // see also bug #77082
- public void WriteDocTypeIndent ()
- {
- string expected = String.Format (@"<?xml version='1.0'?>{0}<!DOCTYPE root PUBLIC '' 'urn:foo'[]>{0}<root />", Environment.NewLine);
- xtw.Formatting = Formatting.Indented;
- xtw.WriteProcessingInstruction ("xml", "version='1.0'");
- xtw.WriteDocType ("root", "", "urn:foo", "");
- xtw.WriteStartElement ("root");
- xtw.WriteEndElement ();
- xtw.Close ();
- Assert.AreEqual (expected, StringWriterText);
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void WriteDocTypeTwice ()
- {
- xtw.WriteDocType ("root", "", "urn:foo", "");
- xtw.WriteDocType ("root", "", "urn:foo", "");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void XmlDeclAfterDocType ()
- {
- xtw.WriteDocType ("root", "", "urn:foo", "");
- xtw.WriteStartDocument ();
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void XmlDeclAfterWhitespace ()
- {
- xtw.WriteWhitespace (" ");
- xtw.WriteStartDocument ();
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void XmlDeclAfterPI ()
- {
- xtw.WriteProcessingInstruction ("pi", "");
- xtw.WriteStartDocument ();
- }
- [Test]
- public void WriteRawEmptyCloseStartTag ()
- {
- xtw.WriteStartElement ("stream", "stream","http://etherx.jabber.org/streams");
- xtw.WriteAttributeString ("version", "1.0");
- xtw.WriteAttributeString ("to", "[email protected]");
- xtw.WriteAttributeString ("from", "server");
- xtw.WriteAttributeString ("xmlns", "jabber:client");
- xtw.WriteRaw ("");// Ensure that the tag is closed
- xtw.Flush ();
- Assert.AreEqual ("<stream:stream version='1.0' to='[email protected]' from='server' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>", StringWriterText);
- }
- [Test] // bug #78148
- public void UpdateFormattingOnTheFly ()
- {
- XmlTextWriter w = new XmlTextWriter (TextWriter.Null);
- w.WriteStartElement ("test");
- w.Formatting = Formatting.Indented;
- }
- [Test] // bug #78598
- public void WriteGlobalAttributeInDefaultNS ()
- {
- StringWriter sw = new StringWriter ();
- XmlTextWriter tw = new XmlTextWriter (sw);
- string ns = "http://schemas.xmlsoap.org/soap/envelope/";
- tw.WriteStartElement ("Envelope");
- tw.WriteAttributeString ("xmlns", ns);
- int start = sw.ToString ().Length;
- tw.WriteStartElement ("UserInfo");
- tw.WriteStartAttribute ("actor", ns);
- tw.WriteEndAttribute ();
- tw.WriteEndElement ();
- tw.WriteEndElement ();
- Assert.IsTrue (sw.ToString ().IndexOf (ns, start) > 0);
- }
- [Test]
- public void WriteCommentPIAndIndent ()
- {
- StringWriter sw = new StringWriter ();
- XmlTextWriter w = new XmlTextWriter (sw);
- w.Formatting = Formatting.Indented;
- w.WriteStartElement ("foo");
- w.WriteComment ("test");
- w.WriteProcessingInstruction ("PI", "");
- w.WriteStartElement ("child");
- w.WriteEndElement ();
- w.WriteComment ("test");
- w.WriteString ("STRING");
- w.WriteEndElement ();
- Assert.AreEqual (String.Format (@"<foo>{0} <!--test-->{0} <?PI ?>{0} <child />{0} <!--test-->STRING</foo>", Environment.NewLine), sw.ToString ());
- }
- [Test]
- public void WriteBinHexAttribute () // for bug #79019
- {
- XmlWriter writer = new XmlTextWriter (TextWriter.Null);
- writer.WriteStartElement ("test");
- byte [] buffer1 = new byte [] {200, 155};
- writer.WriteStartAttribute ("key", "");
- writer.WriteBinHex (buffer1, 0, buffer1.Length);
- writer.WriteEndAttribute ();
- writer.WriteEndElement ();
- }
- [Test]
- public void LookupNamespace ()
- {
- StringWriter sw = new StringWriter ();
- XmlTextWriter xw = new XmlTextWriter (sw);
- xw.Formatting = Formatting.Indented;
- string q1 = "urn:test";
- string q1prefix_first= "q1";
- // Ensure we get a different reference for the string "q1"
- string q1prefix_second = ("q1" + "a").Substring(0,2);
- xw.WriteStartElement("document");
- xw.WriteStartElement("item");
- xw.WriteStartElement (q1prefix_first, "addMedia", q1);
- xw.WriteEndElement();
- xw.WriteEndElement();
- xw.WriteStartElement("item");
- xw.WriteStartElement (q1prefix_second, "addMedia", q1);
- xw.WriteEndElement();
- xw.WriteEndElement();
- xw.WriteEndElement();
- string xml = sw.ToString ();
- int first = xml.IndexOf ("xmlns");
- Assert.IsTrue (xml.IndexOf ("xmlns", first + 5) > 0);
- }
- #if NET_2_0
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void RejectWritingAtErrorState ()
- {
- try {
- xtw.WriteEndElement ();
- } catch (Exception) {
- }
- xtw.WriteStartElement ("foo");
- }
- #endif
- }
- }
|