2 // System.Xml.XmlTextWriterTests
5 // Kral Ferch <kral_ferch@hotmail.com>
6 // Martin Willemoes Hansen <mwh@sysrq.dk>
9 // (C) 2003 Martin Willemoes Hansen
17 using NUnit.Framework;
19 namespace MonoTests.System.Xml
22 public class XmlTextWriterTests : Assertion
28 public void GetReady ()
30 sw = new StringWriter ();
31 xtw = new XmlTextWriter (sw);
35 private string StringWriterText
37 get { return sw.GetStringBuilder ().ToString (); }
41 public void AttributeNamespacesNonNamespaceAttributeBefore ()
43 xtw.WriteStartElement ("foo");
44 xtw.WriteAttributeString("bar", "baz");
45 xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
46 AssertEquals ("<foo bar='baz' xmlns:abc='http://abc.def'", StringWriterText);
50 public void AttributeNamespacesNonNamespaceAttributeAfter ()
52 xtw.WriteStartElement ("foo");
54 xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
55 xtw.WriteAttributeString("bar", "baz");
56 AssertEquals ("<foo xmlns:abc='http://abc.def' bar='baz'", StringWriterText);
60 public void AttributeNamespacesThreeParamWithNullInNamespaceParam ()
62 xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
63 AssertEquals ("xmlns='http://abc.def'", StringWriterText);
67 public void AttributeNamespacesThreeParamWithTextInNamespaceParam ()
71 xtw.WriteAttributeString ("xmlns", "http://somenamespace.com", "http://abc.def");
73 catch (ArgumentException) {}
77 public void AttributeNamespacesWithNullInNamespaceParam ()
79 xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
80 AssertEquals ("xmlns:abc='http://abc.def'", StringWriterText);
84 public void AttributeNamespacesWithTextInNamespaceParam ()
87 xtw.WriteAttributeString ("xmlns", "abc", "http://somenamespace.com", "http://abc.def");
88 } catch (ArgumentException) {}
92 public void AttributeNamespacesXmlnsXmlns ()
94 xtw.WriteStartElement ("foo");
96 xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
97 // This should not be allowed, even though
98 // MS.NET doesn't treat as an error.
99 Fail ("any prefix which name starts from \"xml\" must not be allowed.");
101 catch (ArgumentException) {}
102 xtw.WriteAttributeString ("", "xmlns", null, "http://abc.def");
106 public void AttributeWriteAttributeString ()
108 xtw.WriteStartElement ("foo");
110 xtw.WriteAttributeString ("foo", "bar");
111 AssertEquals ("<foo foo='bar'", StringWriterText);
113 xtw.WriteAttributeString ("bar", "");
114 AssertEquals ("<foo foo='bar' bar=''", StringWriterText);
116 xtw.WriteAttributeString ("baz", null);
117 AssertEquals ("<foo foo='bar' bar='' baz=''", StringWriterText);
119 xtw.WriteAttributeString ("hoge", "a\nb");
120 AssertEquals ("<foo foo='bar' bar='' baz='' hoge='a
b'", StringWriterText);
122 xtw.WriteAttributeString ("fuga", " a\t\r\nb\t");
123 AssertEquals ("<foo foo='bar' bar='' baz='' hoge='a
b' fuga=' a\t
b\t'", StringWriterText);
126 // Why does this pass Microsoft?
127 // Anyway, Mono should not allow such code.
128 xtw.WriteAttributeString ("", "quux");
129 // AssertEquals ("<foo foo='bar' bar='' baz='' ='quux'", StringWriterText);
130 Fail ("empty name not allowed.");
131 } catch (Exception) {
135 // Why does this pass Microsoft?
136 // Anyway, Mono should not allow such code.
137 xtw.WriteAttributeString (null, "quuux");
138 // AssertEquals ("<foo foo='bar' bar='' baz='' ='quux' ='quuux'", StringWriterText);
139 Fail ("null name not allowed.");
140 } catch (Exception) {
145 [ExpectedException (typeof (InvalidOperationException))]
146 public void AttributeWriteAttributeStringNotInsideOpenStartElement ()
148 xtw.WriteStartElement ("foo");
149 xtw.WriteString ("bar");
151 xtw.WriteAttributeString ("baz", "quux");
155 public void AttributeWriteAttributeStringWithoutParentElement ()
157 xtw.WriteAttributeString ("foo", "bar");
158 AssertEquals ("foo='bar'", StringWriterText);
160 xtw.WriteAttributeString ("baz", "quux");
161 AssertEquals ("foo='bar' baz='quux'", StringWriterText);
165 public void CDataValid ()
167 xtw.WriteCData ("foo");
168 AssertEquals ("WriteCData had incorrect output.", "<![CDATA[foo]]>", StringWriterText);
172 [ExpectedException (typeof (ArgumentException))]
173 public void CDataInvalid ()
175 xtw.WriteCData("foo]]>bar");
179 public void CloseOpenElements ()
181 xtw.WriteStartElement("foo");
182 xtw.WriteStartElement("bar");
183 xtw.WriteStartElement("baz");
185 AssertEquals ("Close didn't write out end elements properly.", "<foo><bar><baz /></bar></foo>", StringWriterText);
189 public void CloseWriteAfter ()
191 xtw.WriteElementString ("foo", "bar");
194 // WriteEndElement and WriteStartDocument aren't tested here because
195 // they will always throw different exceptions besides 'The Writer is closed.'
196 // and there are already tests for those exceptions.
199 xtw.WriteCData ("foo");
200 Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
202 catch (InvalidOperationException) {
203 // Don't rely on English message assertion.
204 // It is enough to check an exception occurs.
205 // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
209 xtw.WriteComment ("foo");
210 Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
212 catch (InvalidOperationException) {
213 // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
217 xtw.WriteProcessingInstruction ("foo", "bar");
218 Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
220 catch (InvalidOperationException) {
221 // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
225 xtw.WriteStartElement ("foo", "bar", "baz");
226 Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
228 catch (InvalidOperationException) {
229 // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
234 xtw.WriteAttributeString ("foo", "bar");
235 Fail ("WriteAttributeString after Close Should have thrown an InvalidOperationException.");
237 catch (InvalidOperationException)
239 // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
243 xtw.WriteString ("foo");
244 Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
246 catch (InvalidOperationException) {
247 // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
252 public void CommentValid ()
254 xtw.WriteComment ("foo");
255 AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", StringWriterText);
259 public void CommentInvalid ()
262 xtw.WriteComment("foo-");
263 Fail("Should have thrown an ArgumentException.");
265 catch (ArgumentException) { }
268 xtw.WriteComment("foo-->bar");
269 Fail("Should have thrown an ArgumentException.");
271 catch (ArgumentException) { }
275 public void ConstructorsAndBaseStream ()
277 Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (null, this.xtw.BaseStream));
283 ms = new MemoryStream ();
284 xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
285 xtw.WriteStartDocument ();
287 ms.Seek (0, SeekOrigin.Begin);
288 sr = new StreamReader (ms, Encoding.Unicode);
289 string expectedXmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
290 string actualXmlDeclaration = sr.ReadToEnd();
291 AssertEquals (expectedXmlDeclaration, actualXmlDeclaration);
292 Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
294 ms = new MemoryStream ();
295 xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
296 xtw.WriteStartDocument (true);
298 ms.Seek (0, SeekOrigin.Begin);
299 sr = new StreamReader (ms, Encoding.Unicode);
300 AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
302 ms = new MemoryStream ();
303 xtw = new XmlTextWriter (ms, new UTF8Encoding ());
304 xtw.WriteStartDocument ();
306 ms.Seek (0, SeekOrigin.Begin);
307 sr = new StreamReader (ms, Encoding.UTF8);
308 AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
310 ms = new MemoryStream ();
311 xtw = new XmlTextWriter (ms, null);
312 xtw.WriteStartDocument ();
314 ms.Seek (0, SeekOrigin.Begin);
315 sr = new StreamReader (ms, Encoding.UTF8);
316 AssertEquals ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
318 ms = new MemoryStream ();
319 xtw = new XmlTextWriter (ms, null);
320 xtw.WriteStartDocument (true);
322 ms.Seek (0, SeekOrigin.Begin);
323 sr = new StreamReader (ms, Encoding.UTF8);
324 AssertEquals ("<?xml version=\"1.0\" standalone=\"yes\"?>", sr.ReadToEnd ());
325 Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
329 public void DocumentStart ()
331 xtw.WriteStartDocument ();
332 AssertEquals ("XmlDeclaration is incorrect.", "<?xml version='1.0' encoding='utf-16'?>", StringWriterText);
336 xtw.WriteStartDocument ();
337 Fail("Should have thrown an InvalidOperationException.");
339 catch (InvalidOperationException) {
340 // Don't rely on English message assertion.
341 // It is enough to check an exception occurs.
342 // AssertEquals ("Exception message is incorrect.",
343 // "WriteStartDocument should be the first call.", e.Message);
346 xtw = new XmlTextWriter (sw = new StringWriter ());
347 xtw.QuoteChar = '\'';
348 xtw.WriteStartDocument (true);
349 AssertEquals ("<?xml version='1.0' encoding='utf-16' standalone='yes'?>", StringWriterText);
351 xtw = new XmlTextWriter (sw = new StringWriter ());
352 xtw.QuoteChar = '\'';
353 xtw.WriteStartDocument (false);
354 AssertEquals ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", StringWriterText);
358 public void ElementEmpty ()
360 xtw.WriteStartElement ("foo");
361 xtw.WriteEndElement ();
362 AssertEquals ("Incorrect output.", "<foo />", StringWriterText);
366 public void ElementWriteElementString ()
368 xtw.WriteElementString ("foo", "bar");
369 AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", StringWriterText);
371 xtw.WriteElementString ("baz", "");
372 AssertEquals ("<foo>bar</foo><baz />", StringWriterText);
374 xtw.WriteElementString ("quux", null);
375 AssertEquals ("<foo>bar</foo><baz /><quux />", StringWriterText);
377 xtw.WriteElementString ("", "quuux");
378 AssertEquals ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText);
380 xtw.WriteElementString (null, "quuuux");
381 AssertEquals ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText);
385 public void FormattingTest ()
387 xtw.Formatting = Formatting.Indented;
388 xtw.WriteStartDocument ();
389 xtw.WriteStartElement ("foo");
390 xtw.WriteElementString ("bar", "");
392 AssertEquals (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0} <bar />{0}</foo>", Environment.NewLine), StringWriterText);
396 public void FormattingInvalidXmlForFun ()
398 xtw.Formatting = Formatting.Indented;
399 xtw.IndentChar = 'x';
400 xtw.WriteStartDocument ();
401 xtw.WriteStartElement ("foo");
402 xtw.WriteStartElement ("bar");
403 xtw.WriteElementString ("baz", "");
405 AssertEquals (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);
409 public void FormattingFromRemarks ()
411 // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
412 xtw.Formatting = Formatting.Indented;
413 xtw.WriteStartElement ("ol");
414 xtw.WriteStartElement ("li");
415 xtw.WriteString ("The big "); // This means "li" now has a mixed content model.
416 xtw.WriteElementString ("b", "E");
417 xtw.WriteElementString ("i", "lephant");
418 xtw.WriteString (" walks slowly.");
419 xtw.WriteEndElement ();
420 xtw.WriteEndElement ();
421 AssertEquals (String.Format ("<ol>{0} <li>The big <b>E</b><i>lephant</i> walks slowly.</li>{0}</ol>", Environment.NewLine), StringWriterText);
425 public void LookupPrefix ()
427 xtw.WriteStartElement ("root");
429 xtw.WriteStartElement ("one");
430 xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
431 xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
432 AssertEquals ("foo", xtw.LookupPrefix ("http://abc.def"));
433 AssertEquals ("bar", xtw.LookupPrefix ("http://ghi.jkl"));
434 xtw.WriteEndElement ();
436 xtw.WriteStartElement ("two");
437 xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
438 xtw.WriteString("quux");
439 AssertEquals ("baz", xtw.LookupPrefix ("http://mno.pqr"));
440 AssertNull (xtw.LookupPrefix ("http://abc.def"));
441 AssertNull (xtw.LookupPrefix ("http://ghi.jkl"));
443 AssertNull (xtw.LookupPrefix ("http://bogus"));
447 public void NamespacesAttributesPassingInNamespaces ()
449 xtw.Namespaces = false;
450 xtw.WriteStartElement ("foo");
452 // These shouldn't throw any exceptions since they don't pass in
454 xtw.WriteAttributeString ("bar", "baz");
455 xtw.WriteAttributeString ("", "a", "", "b");
456 xtw.WriteAttributeString (null, "c", "", "d");
457 xtw.WriteAttributeString ("", "e", null, "f");
458 xtw.WriteAttributeString (null, "g", null, "h");
460 AssertEquals ("<foo bar='baz' a='b' c='d' e='f' g='h'", StringWriterText);
462 // These should throw ArgumentException because they pass in a
463 // namespace when Namespaces = false.
467 public void NamespacesElementsPassingInNamespaces ()
469 xtw.Namespaces = false;
471 // These shouldn't throw any exceptions since they don't pass in
473 xtw.WriteElementString ("foo", "bar");
474 xtw.WriteStartElement ("baz");
475 xtw.WriteStartElement ("quux", "");
476 xtw.WriteStartElement ("quuux", null);
477 xtw.WriteStartElement (null, "a", null);
478 xtw.WriteStartElement (null, "b", "");
479 xtw.WriteStartElement ("", "c", null);
480 xtw.WriteStartElement ("", "d", "");
482 AssertEquals ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", StringWriterText);
484 // These should throw ArgumentException because they pass in a
485 // namespace when Namespaces = false.
487 xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
488 Fail ("Expected an ArgumentException.");
489 } catch (ArgumentException) {}
492 xtw.WriteStartElement ("foo", "http://netsack.com/");
493 Fail ("Expected an ArgumentException.");
494 } catch (ArgumentException) {}
497 xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
498 Fail ("Expected an ArgumentException.");
499 } catch (ArgumentException) {}
502 xtw.WriteStartElement ("foo", "bar", null);
503 Fail ("Expected an ArgumentException.");
504 } catch (ArgumentException) {}
507 xtw.WriteStartElement ("foo", "bar", "");
508 Fail ("Expected an ArgumentException.");
509 } catch (ArgumentException) {}
512 xtw.WriteStartElement ("foo", "", "");
513 Fail ("Expected an ArgumentException.");
514 } catch (ArgumentException) {}
518 public void NamespacesNoNamespaceClearsDefaultNamespace ()
520 xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
521 xtw.WriteStartElement(String.Empty, "bar", String.Empty);
522 xtw.WriteElementString("baz", String.Empty, String.Empty);
523 xtw.WriteEndElement();
524 xtw.WriteEndElement();
525 AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
526 "<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>", StringWriterText);
530 public void NamespacesPrefix ()
532 xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
533 xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
534 xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
535 xtw.WriteEndElement ();
536 xtw.WriteEndElement ();
537 AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
538 "<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>", StringWriterText);
542 public void NamespacesPrefixWithEmptyAndNullNamespace ()
545 xtw.WriteStartElement ("foo", "bar", "");
546 Fail ("Should have thrown an ArgumentException.");
547 } catch (ArgumentException) {}
551 xtw.WriteStartElement ("foo", "bar", null);
552 Fail ("Should have thrown an ArgumentException.");
554 catch (ArgumentException) {}
558 public void NamespacesSettingWhenWriteStateNotStart ()
560 xtw.WriteStartElement ("foo");
563 xtw.Namespaces = false;
564 Fail ("Expected an InvalidOperationException.");
566 catch (InvalidOperationException) {}
567 AssertEquals (true, xtw.Namespaces);
571 public void ProcessingInstructionValid ()
573 xtw.WriteProcessingInstruction("foo", "bar");
574 AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", StringWriterText);
578 public void ProcessingInstructionInvalid ()
582 xtw.WriteProcessingInstruction("fo?>o", "bar");
583 Fail("Should have thrown an ArgumentException.");
585 catch (ArgumentException) { }
589 xtw.WriteProcessingInstruction("foo", "ba?>r");
590 Fail("Should have thrown an ArgumentException.");
592 catch (ArgumentException) { }
596 xtw.WriteProcessingInstruction("", "bar");
597 Fail("Should have thrown an ArgumentException.");
599 catch (ArgumentException) { }
603 xtw.WriteProcessingInstruction(null, "bar");
604 Fail("Should have thrown an ArgumentException.");
606 catch (ArgumentException) { }
610 public void QuoteCharDoubleQuote ()
614 // version, encoding, standalone
615 xtw.WriteStartDocument (true);
617 // namespace declaration
618 xtw.WriteElementString ("foo", "http://netsack.com", "bar");
620 AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><foo xmlns=\"http://netsack.com\">bar</foo>", StringWriterText);
626 [ExpectedException (typeof (ArgumentException))]
627 public void QuoteCharInvalid ()
633 public void WriteBase64 ()
635 UTF8Encoding encoding = new UTF8Encoding();
636 byte[] fooBar = encoding.GetBytes("foobar");
637 xtw.WriteBase64 (fooBar, 0, 6);
638 AssertEquals("Zm9vYmFy", StringWriterText);
641 xtw.WriteBase64 (fooBar, 3, 6);
642 Fail ("Expected an Argument Exception to be thrown.");
643 } catch (ArgumentException) {}
646 xtw.WriteBase64 (fooBar, -1, 6);
647 Fail ("Expected an Argument Exception to be thrown.");
648 } catch (ArgumentOutOfRangeException) {}
651 xtw.WriteBase64 (fooBar, 3, -1);
652 Fail ("Expected an Argument Exception to be thrown.");
653 } catch (ArgumentOutOfRangeException) {}
656 xtw.WriteBase64 (null, 0, 6);
657 Fail ("Expected an Argument Exception to be thrown.");
658 } catch (ArgumentNullException) {}
662 public void WriteCharEntity ()
664 xtw.WriteCharEntity ('a');
665 AssertEquals ("a", StringWriterText);
667 xtw.WriteCharEntity ('A');
668 AssertEquals ("aA", StringWriterText);
670 xtw.WriteCharEntity ('1');
671 AssertEquals ("aA1", StringWriterText);
673 xtw.WriteCharEntity ('K');
674 AssertEquals ("aA1K", StringWriterText);
677 xtw.WriteCharEntity ((char)0xd800);
678 } catch (ArgumentException) {}
682 [ExpectedException (typeof (InvalidOperationException))]
683 public void WriteEndAttribute ()
685 xtw.WriteEndAttribute ();
689 public void WriteEndDocument ()
692 xtw.WriteEndDocument ();
693 Fail ("Expected an ArgumentException.");
694 } catch (ArgumentException) {}
696 xtw.WriteStartDocument ();
700 xtw.WriteEndDocument ();
701 Fail ("Expected an ArgumentException.");
703 catch (ArgumentException) {}
705 xtw.WriteStartElement ("foo");
706 xtw.WriteStartAttribute ("bar", null);
707 AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='", StringWriterText);
709 xtw.WriteEndDocument ();
710 AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", StringWriterText);
711 AssertEquals (WriteState.Start, xtw.WriteState);
715 public void WriteEndElement ()
718 xtw.WriteEndElement ();
719 Fail ("Should have thrown an InvalidOperationException.");
720 } catch (InvalidOperationException) {
721 // Don't rely on English message assertion.
722 // It is enough to check an exception occurs.
723 // AssertEquals ("Exception message is incorrect.", "There was no XML start tag open.", e.Message);
726 xtw.WriteStartElement ("foo");
727 xtw.WriteEndElement ();
728 AssertEquals ("<foo />", StringWriterText);
730 xtw.WriteStartElement ("bar");
731 xtw.WriteStartAttribute ("baz", null);
732 xtw.WriteEndElement ();
733 AssertEquals ("<foo /><bar baz='' />", StringWriterText);
737 public void FullEndElement ()
739 xtw.WriteStartElement ("foo");
740 xtw.WriteFullEndElement ();
741 AssertEquals ("<foo></foo>", StringWriterText);
743 xtw.WriteStartElement ("bar");
744 xtw.WriteAttributeString ("foo", "bar");
745 xtw.WriteFullEndElement ();
746 AssertEquals ("<foo></foo><bar foo='bar'></bar>", StringWriterText);
748 xtw.WriteStartElement ("baz");
749 xtw.WriteStartAttribute ("bar", null);
750 xtw.WriteFullEndElement ();
751 AssertEquals ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", StringWriterText);
755 public void WriteQualifiedName ()
757 xtw.WriteStartElement (null, "test", null);
758 xtw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
759 xtw.WriteQualifiedName ("bob", "http://localhost/");
760 xtw.WriteEndElement ();
762 AssertEquals ("<test xmlns:me='http://localhost/'>me:bob</test>", StringWriterText);
766 public void WriteRaw ()
768 xtw.WriteRaw("&<>\"'");
769 AssertEquals ("&<>\"'", StringWriterText);
772 AssertEquals ("&<>\"'", StringWriterText);
775 AssertEquals ("&<>\"'", StringWriterText);
779 public void WriteRawInvalidInAttribute ()
781 xtw.WriteStartElement ("foo");
782 xtw.WriteStartAttribute ("bar", null);
783 xtw.WriteRaw ("&<>\"'");
784 xtw.WriteEndAttribute ();
785 xtw.WriteEndElement ();
786 AssertEquals ("<foo bar='&<>\"'' />", StringWriterText);
790 public void WriteStateTest ()
792 AssertEquals (WriteState.Start, xtw.WriteState);
793 xtw.WriteStartDocument ();
794 AssertEquals (WriteState.Prolog, xtw.WriteState);
795 xtw.WriteStartElement ("root");
796 AssertEquals (WriteState.Element, xtw.WriteState);
797 xtw.WriteElementString ("foo", "bar");
798 AssertEquals (WriteState.Content, xtw.WriteState);
800 AssertEquals (WriteState.Closed, xtw.WriteState);
804 public void WriteString ()
806 xtw.WriteStartDocument ();
808 xtw.WriteString("foo");
809 } catch (InvalidOperationException) {}
811 // Testing attribute values
813 xtw.WriteStartElement ("foo");
814 xtw.WriteAttributeString ("bar", "&<>");
815 AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='&<>'", StringWriterText);
819 public void WriteAttributeStringSingleQuoteChar()
821 // When QuoteChar is single quote then replaces single quotes within attributes
822 // but not double quotes.
823 xtw.WriteStartElement ("foo");
824 xtw.WriteAttributeString ("bar", "\"baz\"");
825 xtw.WriteAttributeString ("quux", "'baz'");
826 AssertEquals ("<foo bar='\"baz\"' quux=''baz''", StringWriterText);
830 public void WriteAttributeStringDoubleQuoteChar()
832 // When QuoteChar is double quote then replaces double quotes within attributes
833 // but not single quotes.
835 xtw.WriteStartElement ("foo");
836 xtw.WriteAttributeString ("bar", "\"baz\"");
837 xtw.WriteAttributeString ("quux", "'baz'");
838 AssertEquals ("<foo bar=\""baz"\" quux=\"'baz'\"", StringWriterText);
842 public void WriteStringWithEntities()
844 // Testing element values
845 xtw.QuoteChar = '\'';
846 xtw.WriteElementString ("foo", "&<>\"'");
847 AssertEquals ("<foo>&<>\"'</foo>", StringWriterText);
851 public void XmlLang ()
853 AssertNull (xtw.XmlLang);
855 xtw.WriteStartElement ("foo");
856 xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
857 AssertEquals ("langfoo", xtw.XmlLang);
858 AssertEquals ("<foo xml:lang='langfoo'", StringWriterText);
860 xtw.WriteAttributeString ("boo", "yah");
861 AssertEquals ("langfoo", xtw.XmlLang);
862 AssertEquals ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
864 xtw.WriteElementString("bar", "baz");
865 AssertEquals ("langfoo", xtw.XmlLang);
866 AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
868 xtw.WriteString("baz");
869 AssertEquals ("langfoo", xtw.XmlLang);
870 AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
872 xtw.WriteStartElement ("quux");
873 xtw.WriteStartAttribute ("xml", "lang", null);
874 AssertEquals ("langfoo", xtw.XmlLang);
875 AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
877 xtw.WriteString("langbar");
878 AssertEquals ("langfoo", xtw.XmlLang);
879 AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
881 xtw.WriteEndAttribute ();
882 AssertEquals ("langbar", xtw.XmlLang);
883 AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
885 // check if xml:lang repeats output even if same as current scope.
886 xtw.WriteStartElement ("joe");
887 xtw.WriteAttributeString ("xml", "lang", null, "langbar");
888 AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", StringWriterText);
891 xtw.WriteElementString ("quuux", "squonk");
892 AssertEquals ("langbar", xtw.XmlLang);
893 AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
895 xtw.WriteEndElement ();
896 xtw.WriteEndElement ();
897 AssertEquals ("langfoo", xtw.XmlLang);
898 AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux>", StringWriterText);
900 xtw.WriteEndElement ();
901 AssertNull (xtw.XmlLang);
902 AssertEquals ("<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);
905 AssertNull (xtw.XmlLang);
908 // TODO: test operational aspects
910 public void XmlSpaceTest ()
912 xtw.WriteStartElement ("foo");
913 AssertEquals (XmlSpace.None, xtw.XmlSpace);
915 xtw.WriteStartElement ("bar");
916 xtw.WriteAttributeString ("xml", "space", null, "preserve");
917 AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
918 AssertEquals ("<foo><bar xml:space='preserve'", StringWriterText);
920 xtw.WriteStartElement ("baz");
921 xtw.WriteAttributeString ("xml", "space", null, "preserve");
922 AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
923 AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText);
925 xtw.WriteStartElement ("quux");
926 xtw.WriteStartAttribute ("xml", "space", null);
927 AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
928 AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
930 xtw.WriteString ("default");
931 AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
932 AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
934 xtw.WriteEndAttribute ();
935 AssertEquals (XmlSpace.Default, xtw.XmlSpace);
936 AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText);
938 xtw.WriteEndElement ();
939 AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
940 xtw.WriteEndElement ();
941 AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
942 xtw.WriteEndElement ();
943 AssertEquals (XmlSpace.None, xtw.XmlSpace);
945 xtw.WriteStartElement ("quux");
947 xtw.WriteAttributeString ("xml", "space", null, "bubba");
948 } catch (ArgumentException) {}
951 xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
952 } catch (ArgumentException) {}
955 xtw.WriteAttributeString ("xml", "space", null, "Preserve");
956 } catch (ArgumentException) {}
959 xtw.WriteAttributeString ("xml", "space", null, "Default");
960 } catch (ArgumentException) {}
963 xtw.WriteWhitespace ("x");
964 } catch (ArgumentException) { }
968 public void XmlSpaceRaw ()
970 xtw.WriteStartElement ("foo");
971 xtw.WriteStartAttribute ("xml", "space", null);
972 AssertEquals (XmlSpace.None, xtw.XmlSpace);
973 AssertEquals ("<foo xml:space='", StringWriterText);
975 xtw.WriteString ("default");
976 AssertEquals (XmlSpace.None, xtw.XmlSpace);
977 AssertEquals ("<foo xml:space='", StringWriterText);
979 xtw.WriteEndAttribute ();
980 AssertEquals (XmlSpace.Default, xtw.XmlSpace);
981 AssertEquals ("<foo xml:space='default'", StringWriterText);
985 public void WriteAttributes ()
987 XmlDocument doc = new XmlDocument();
988 StringWriter sw = new StringWriter();
989 XmlWriter wr = new XmlTextWriter(sw);
990 StringBuilder sb = sw.GetStringBuilder();
991 XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
992 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);
994 xtr.Read(); // read XMLDecl
995 wr.WriteAttributes(xtr, false);
996 // This method don't always have to take this double-quoted style...
997 AssertEquals("#WriteAttributes.XmlDecl.1", "version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString().Trim());
999 sb.Remove(0, sb.Length); // init
1000 ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
1001 xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
1002 xtr.Read(); // read XMLDecl
1003 AssertEquals (XmlNodeType.XmlDeclaration, xtr.NodeType);
1004 sw = new StringWriter ();
1005 wr = new XmlTextWriter (sw);
1007 // This block raises an error on MS.NET 1.0.
1008 wr.WriteAttributes(xtr, false);
1009 // This method don't always have to take this double-quoted style...
1010 AssertEquals("#WriteAttributes.XmlDecl.2", "version=\"1.0\" standalone=\"no\"", sw.ToString().Trim());
1012 sw = new StringWriter ();
1013 wr = new XmlTextWriter (sw);
1014 sb.Remove(0, sb.Length); // init
1016 xtr.Read(); // read root
1017 AssertEquals (XmlNodeType.Element, xtr.NodeType);
1018 wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
1019 wr.WriteAttributes(xtr, false);
1020 wr.WriteEndElement();
1022 // This method don't always have to take this double-quoted style...
1023 AssertEquals("#WriteAttributes.Element", "<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString().Trim());
1027 public void WriteWhitespace ()
1029 xtw.WriteStartElement ("a");
1030 xtw.WriteWhitespace ("\n\t");
1031 xtw.WriteStartElement ("b");
1032 xtw.WriteWhitespace ("\n\t");
1033 xtw.WriteEndElement ();
1034 xtw.WriteWhitespace ("\n");
1035 xtw.WriteEndElement ();
1036 xtw.WriteWhitespace ("\n");
1038 AssertEquals ("<a>\n\t<b>\n\t</b>\n</a>\n", StringWriterText);
1042 public void FlushDoesntCloseTag ()
1044 xtw.WriteStartElement ("foo");
1045 xtw.WriteAttributeString ("bar", "baz");
1047 AssertEquals ("<foo bar='baz'", StringWriterText);
1051 public void WriteWhitespaceClosesTag ()
1053 xtw.WriteStartElement ("foo");
1054 xtw.WriteAttributeString ("bar", "baz");
1055 xtw.WriteWhitespace (" ");
1056 AssertEquals ("<foo bar='baz'> ", StringWriterText);
1060 public void DontOutputMultipleXmlns ()
1062 XmlDocument doc = new XmlDocument();
\r
1063 doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
\r
1064 XmlDocument doc2 = new XmlDocument();
\r
1065 doc2.LoadXml(doc.InnerXml);
1066 AssertEquals ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",