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
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 Assertion.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 Assertion.AssertEquals ("<foo xmlns:abc='http://abc.def' bar='baz'", StringWriterText);
60 public void AttributeNamespacesThreeParamWithNullInNamespaceParam ()
62 xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
63 Assertion.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 Assertion.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");
97 xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
98 Assertion.Fail ("any prefix which name starts from \"xml\" must not be allowed.");
100 catch (ArgumentException e) {}
104 public void AttributeWriteAttributeString ()
106 xtw.WriteStartElement ("foo");
108 xtw.WriteAttributeString ("foo", "bar");
109 Assertion.AssertEquals ("<foo foo='bar'", StringWriterText);
111 xtw.WriteAttributeString ("bar", "");
112 Assertion.AssertEquals ("<foo foo='bar' bar=''", StringWriterText);
114 xtw.WriteAttributeString ("baz", null);
115 Assertion.AssertEquals ("<foo foo='bar' bar='' baz=''", StringWriterText);
118 // Why does this pass Microsoft?
119 // Anyway, Mono should not allow such code.
120 xtw.WriteAttributeString ("", "quux");
121 // Assertion.AssertEquals ("<foo foo='bar' bar='' baz='' ='quux'", StringWriterText);
122 Assertion.Fail ("empty name not allowed.");
123 } catch (Exception) {
127 // Why does this pass Microsoft?
128 // Anyway, Mono should not allow such code.
129 xtw.WriteAttributeString (null, "quuux");
130 // Assertion.AssertEquals ("<foo foo='bar' bar='' baz='' ='quux' ='quuux'", StringWriterText);
131 Assertion.Fail ("null name not allowed.");
132 } catch (Exception) {
137 public void AttributeWriteAttributeStringNotInsideOpenStartElement ()
139 xtw.WriteStartElement ("foo");
140 xtw.WriteString ("bar");
144 xtw.WriteAttributeString ("baz", "quux");
145 Assertion.Fail ("Expected an InvalidOperationException to be thrown.");
147 catch (InvalidOperationException) {}
151 public void AttributeWriteAttributeStringWithoutParentElement ()
153 xtw.WriteAttributeString ("foo", "bar");
154 Assertion.AssertEquals ("foo='bar'", StringWriterText);
156 xtw.WriteAttributeString ("baz", "quux");
157 Assertion.AssertEquals ("foo='bar' baz='quux'", StringWriterText);
161 public void CDataValid ()
163 xtw.WriteCData ("foo");
164 Assertion.AssertEquals ("WriteCData had incorrect output.", "<![CDATA[foo]]>", StringWriterText);
168 public void CDataInvalid ()
171 xtw.WriteCData("foo]]>bar");
172 Assertion.Fail("Should have thrown an ArgumentException.");
174 catch (ArgumentException) { }
178 public void CloseOpenElements ()
180 xtw.WriteStartElement("foo");
181 xtw.WriteStartElement("bar");
182 xtw.WriteStartElement("baz");
184 Assertion.AssertEquals ("Close didn't write out end elements properly.", "<foo><bar><baz /></bar></foo>", StringWriterText);
188 public void CloseWriteAfter ()
190 xtw.WriteElementString ("foo", "bar");
193 // WriteEndElement and WriteStartDocument aren't tested here because
194 // they will always throw different exceptions besides 'The Writer is closed.'
195 // and there are already tests for those exceptions.
198 xtw.WriteCData ("foo");
199 Assertion.Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
201 catch (InvalidOperationException e) {
202 Assertion.AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
206 xtw.WriteComment ("foo");
207 Assertion.Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
209 catch (InvalidOperationException e) {
210 Assertion.AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
214 xtw.WriteProcessingInstruction ("foo", "bar");
215 Assertion.Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
217 catch (InvalidOperationException e) {
218 Assertion.AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
222 xtw.WriteStartElement ("foo", "bar", "baz");
223 Assertion.Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
225 catch (InvalidOperationException e) {
226 Assertion.AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
231 xtw.WriteAttributeString ("foo", "bar");
232 Assertion.Fail ("WriteAttributeString after Close Should have thrown an InvalidOperationException.");
234 catch (InvalidOperationException e)
236 Assertion.AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
240 xtw.WriteString ("foo");
241 Assertion.Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
243 catch (InvalidOperationException e) {
244 Assertion.AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
249 public void CommentValid ()
251 xtw.WriteComment ("foo");
252 Assertion.AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", StringWriterText);
256 public void CommentInvalid ()
259 xtw.WriteComment("foo-");
260 Assertion.Fail("Should have thrown an ArgumentException.");
262 catch (ArgumentException) { }
265 xtw.WriteComment("foo-->bar");
266 Assertion.Fail("Should have thrown an ArgumentException.");
268 catch (ArgumentException) { }
272 public void ConstructorsAndBaseStream ()
274 Assertion.Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (null, this.xtw.BaseStream));
280 ms = new MemoryStream ();
281 xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
282 xtw.WriteStartDocument ();
284 ms.Seek (0, SeekOrigin.Begin);
285 sr = new StreamReader (ms, Encoding.Unicode);
286 string expectedXmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
287 string actualXmlDeclaration = sr.ReadToEnd();
288 Assertion.AssertEquals (expectedXmlDeclaration, actualXmlDeclaration);
289 Assertion.Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
291 ms = new MemoryStream ();
292 xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
293 xtw.WriteStartDocument (true);
295 ms.Seek (0, SeekOrigin.Begin);
296 sr = new StreamReader (ms, Encoding.Unicode);
297 Assertion.AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
299 ms = new MemoryStream ();
300 xtw = new XmlTextWriter (ms, new UTF8Encoding ());
301 xtw.WriteStartDocument ();
303 ms.Seek (0, SeekOrigin.Begin);
304 sr = new StreamReader (ms, Encoding.UTF8);
305 Assertion.AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
307 ms = new MemoryStream ();
308 xtw = new XmlTextWriter (ms, null);
309 xtw.WriteStartDocument ();
311 ms.Seek (0, SeekOrigin.Begin);
312 sr = new StreamReader (ms, Encoding.UTF8);
313 Assertion.AssertEquals ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
315 ms = new MemoryStream ();
316 xtw = new XmlTextWriter (ms, null);
317 xtw.WriteStartDocument (true);
319 ms.Seek (0, SeekOrigin.Begin);
320 sr = new StreamReader (ms, Encoding.UTF8);
321 Assertion.AssertEquals ("<?xml version=\"1.0\" standalone=\"yes\"?>", sr.ReadToEnd ());
322 Assertion.Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
326 public void DocumentStart ()
328 xtw.WriteStartDocument ();
329 Assertion.AssertEquals ("XmlDeclaration is incorrect.", "<?xml version='1.0' encoding='utf-16'?>", StringWriterText);
333 xtw.WriteStartDocument ();
334 Assertion.Fail("Should have thrown an InvalidOperationException.");
336 catch (InvalidOperationException e) {
337 Assertion.AssertEquals ("Exception message is incorrect.",
338 "WriteStartDocument should be the first call.", e.Message);
341 xtw = new XmlTextWriter (sw = new StringWriter ());
342 xtw.QuoteChar = '\'';
343 xtw.WriteStartDocument (true);
344 Assertion.AssertEquals ("<?xml version='1.0' encoding='utf-16' standalone='yes'?>", StringWriterText);
346 xtw = new XmlTextWriter (sw = new StringWriter ());
347 xtw.QuoteChar = '\'';
348 xtw.WriteStartDocument (false);
349 Assertion.AssertEquals ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", StringWriterText);
353 public void ElementEmpty ()
355 xtw.WriteStartElement ("foo");
356 xtw.WriteEndElement ();
357 Assertion.AssertEquals ("Incorrect output.", "<foo />", StringWriterText);
361 public void ElementWriteElementString ()
363 xtw.WriteElementString ("foo", "bar");
364 Assertion.AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", StringWriterText);
366 xtw.WriteElementString ("baz", "");
367 Assertion.AssertEquals ("<foo>bar</foo><baz />", StringWriterText);
369 xtw.WriteElementString ("quux", null);
370 Assertion.AssertEquals ("<foo>bar</foo><baz /><quux />", StringWriterText);
372 xtw.WriteElementString ("", "quuux");
373 Assertion.AssertEquals ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText);
375 xtw.WriteElementString (null, "quuuux");
376 Assertion.AssertEquals ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText);
380 public void FormattingTest ()
382 xtw.Formatting = Formatting.Indented;
383 xtw.WriteStartDocument ();
384 xtw.WriteStartElement ("foo");
385 xtw.WriteElementString ("bar", "");
387 Assertion.AssertEquals (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0} <bar />{0}</foo>", Environment.NewLine), StringWriterText);
391 public void FormattingInvalidXmlForFun ()
393 xtw.Formatting = Formatting.Indented;
394 xtw.IndentChar = 'x';
395 xtw.WriteStartDocument ();
396 xtw.WriteStartElement ("foo");
397 xtw.WriteStartElement ("bar");
398 xtw.WriteElementString ("baz", "");
400 Assertion.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);
404 public void FormattingFromRemarks ()
406 // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
407 xtw.Formatting = Formatting.Indented;
408 xtw.WriteStartElement ("ol");
409 xtw.WriteStartElement ("li");
410 xtw.WriteString ("The big "); // This means "li" now has a mixed content model.
411 xtw.WriteElementString ("b", "E");
412 xtw.WriteElementString ("i", "lephant");
413 xtw.WriteString (" walks slowly.");
414 xtw.WriteEndElement ();
415 xtw.WriteEndElement ();
416 Assertion.AssertEquals (String.Format ("<ol>{0} <li>The big <b>E</b><i>lephant</i> walks slowly.</li>{0}</ol>", Environment.NewLine), StringWriterText);
420 public void LookupPrefix ()
422 xtw.WriteStartElement ("root");
424 xtw.WriteStartElement ("one");
425 xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
426 xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
427 Assertion.AssertEquals ("foo", xtw.LookupPrefix ("http://abc.def"));
428 Assertion.AssertEquals ("bar", xtw.LookupPrefix ("http://ghi.jkl"));
429 xtw.WriteEndElement ();
431 xtw.WriteStartElement ("two");
432 xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
433 xtw.WriteString("quux");
434 Assertion.AssertEquals ("baz", xtw.LookupPrefix ("http://mno.pqr"));
435 Assertion.AssertNull (xtw.LookupPrefix ("http://abc.def"));
436 Assertion.AssertNull (xtw.LookupPrefix ("http://ghi.jkl"));
438 Assertion.AssertNull (xtw.LookupPrefix ("http://bogus"));
442 public void NamespacesAttributesPassingInNamespaces ()
444 xtw.Namespaces = false;
445 xtw.WriteStartElement ("foo");
447 // These shouldn't throw any exceptions since they don't pass in
449 xtw.WriteAttributeString ("bar", "baz");
450 xtw.WriteAttributeString ("", "a", "", "b");
451 xtw.WriteAttributeString (null, "c", "", "d");
452 xtw.WriteAttributeString ("", "e", null, "f");
453 xtw.WriteAttributeString (null, "g", null, "h");
455 Assertion.AssertEquals ("<foo bar='baz' a='b' c='d' e='f' g='h'", StringWriterText);
457 // These should throw ArgumentException because they pass in a
458 // namespace when Namespaces = false.
462 public void NamespacesElementsPassingInNamespaces ()
464 xtw.Namespaces = false;
466 // These shouldn't throw any exceptions since they don't pass in
468 xtw.WriteElementString ("foo", "bar");
469 xtw.WriteStartElement ("baz");
470 xtw.WriteStartElement ("quux", "");
471 xtw.WriteStartElement ("quuux", null);
472 xtw.WriteStartElement (null, "a", null);
473 xtw.WriteStartElement (null, "b", "");
474 xtw.WriteStartElement ("", "c", null);
475 xtw.WriteStartElement ("", "d", "");
477 Assertion.AssertEquals ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", StringWriterText);
479 // These should throw ArgumentException because they pass in a
480 // namespace when Namespaces = false.
482 xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
483 Assertion.Fail ("Expected an ArgumentException.");
484 } catch (ArgumentException) {}
487 xtw.WriteStartElement ("foo", "http://netsack.com/");
488 Assertion.Fail ("Expected an ArgumentException.");
489 } catch (ArgumentException) {}
492 xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
493 Assertion.Fail ("Expected an ArgumentException.");
494 } catch (ArgumentException) {}
497 xtw.WriteStartElement ("foo", "bar", null);
498 Assertion.Fail ("Expected an ArgumentException.");
499 } catch (ArgumentException) {}
502 xtw.WriteStartElement ("foo", "bar", "");
503 Assertion.Fail ("Expected an ArgumentException.");
504 } catch (ArgumentException) {}
507 xtw.WriteStartElement ("foo", "", "");
508 Assertion.Fail ("Expected an ArgumentException.");
509 } catch (ArgumentException) {}
513 public void NamespacesNoNamespaceClearsDefaultNamespace ()
515 xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
516 xtw.WriteStartElement(String.Empty, "bar", String.Empty);
517 xtw.WriteElementString("baz", String.Empty, String.Empty);
518 xtw.WriteEndElement();
519 xtw.WriteEndElement();
520 Assertion.AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
521 "<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>", StringWriterText);
525 public void NamespacesPrefix ()
527 xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
528 xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
529 xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
530 xtw.WriteEndElement ();
531 xtw.WriteEndElement ();
532 Assertion.AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
533 "<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>", StringWriterText);
537 public void NamespacesPrefixWithEmptyAndNullNamespace ()
540 xtw.WriteStartElement ("foo", "bar", "");
541 Assertion.Fail ("Should have thrown an ArgumentException.");
542 } catch (ArgumentException) {}
546 xtw.WriteStartElement ("foo", "bar", null);
547 Assertion.Fail ("Should have thrown an ArgumentException.");
549 catch (ArgumentException) {}
553 public void NamespacesSettingWhenWriteStateNotStart ()
555 xtw.WriteStartElement ("foo");
558 xtw.Namespaces = false;
559 Assertion.Fail ("Expected an InvalidOperationException.");
561 catch (InvalidOperationException) {}
562 Assertion.AssertEquals (true, xtw.Namespaces);
566 public void ProcessingInstructionValid ()
568 xtw.WriteProcessingInstruction("foo", "bar");
569 Assertion.AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", StringWriterText);
573 public void ProcessingInstructionInvalid ()
577 xtw.WriteProcessingInstruction("fo?>o", "bar");
578 Assertion.Fail("Should have thrown an ArgumentException.");
580 catch (ArgumentException) { }
584 xtw.WriteProcessingInstruction("foo", "ba?>r");
585 Assertion.Fail("Should have thrown an ArgumentException.");
587 catch (ArgumentException) { }
591 xtw.WriteProcessingInstruction("", "bar");
592 Assertion.Fail("Should have thrown an ArgumentException.");
594 catch (ArgumentException) { }
598 xtw.WriteProcessingInstruction(null, "bar");
599 Assertion.Fail("Should have thrown an ArgumentException.");
601 catch (ArgumentException) { }
605 public void QuoteCharDoubleQuote ()
609 // version, encoding, standalone
610 xtw.WriteStartDocument (true);
612 // namespace declaration
613 xtw.WriteElementString ("foo", "http://netsack.com", "bar");
615 Assertion.AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><foo xmlns=\"http://netsack.com\">bar</foo>", StringWriterText);
621 public void QuoteCharInvalid ()
625 Assertion.Fail ("Should have thrown an ArgumentException.");
626 } catch (ArgumentException) {}
630 public void WriteBase64 ()
632 UTF8Encoding encoding = new UTF8Encoding();
633 byte[] fooBar = encoding.GetBytes("foobar");
634 xtw.WriteBase64 (fooBar, 0, 6);
635 Assertion.AssertEquals("Zm9vYmFy", StringWriterText);
638 xtw.WriteBase64 (fooBar, 3, 6);
639 Assertion.Fail ("Expected an Argument Exception to be thrown.");
640 } catch (ArgumentException) {}
643 xtw.WriteBase64 (fooBar, -1, 6);
644 Assertion.Fail ("Expected an Argument Exception to be thrown.");
645 } catch (ArgumentOutOfRangeException) {}
648 xtw.WriteBase64 (fooBar, 3, -1);
649 Assertion.Fail ("Expected an Argument Exception to be thrown.");
650 } catch (ArgumentOutOfRangeException) {}
653 xtw.WriteBase64 (null, 0, 6);
654 Assertion.Fail ("Expected an Argument Exception to be thrown.");
655 } catch (ArgumentNullException) {}
659 public void WriteCharEntity ()
661 xtw.WriteCharEntity ('a');
662 Assertion.AssertEquals ("a", StringWriterText);
664 xtw.WriteCharEntity ('A');
665 Assertion.AssertEquals ("aA", StringWriterText);
667 xtw.WriteCharEntity ('1');
668 Assertion.AssertEquals ("aA1", StringWriterText);
670 xtw.WriteCharEntity ('K');
671 Assertion.AssertEquals ("aA1K", StringWriterText);
674 xtw.WriteCharEntity ((char)0xd800);
675 } catch (ArgumentException) {}
679 public void WriteEndAttribute ()
683 xtw.WriteEndAttribute ();
684 Assertion.Fail ("Should have thrown an InvalidOperationException.");
686 catch (InvalidOperationException) {}
690 public void WriteEndDocument ()
693 xtw.WriteEndDocument ();
694 Assertion.Fail ("Expected an ArgumentException.");
695 } catch (ArgumentException) {}
697 xtw.WriteStartDocument ();
701 xtw.WriteEndDocument ();
702 Assertion.Fail ("Expected an ArgumentException.");
704 catch (ArgumentException) {}
706 xtw.WriteStartElement ("foo");
707 xtw.WriteStartAttribute ("bar", null);
708 Assertion.AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='", StringWriterText);
710 xtw.WriteEndDocument ();
711 Assertion.AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", StringWriterText);
712 Assertion.AssertEquals (WriteState.Start, xtw.WriteState);
716 public void WriteEndElement ()
719 xtw.WriteEndElement ();
720 Assertion.Fail ("Should have thrown an InvalidOperationException.");
721 } catch (InvalidOperationException e) {
722 Assertion.AssertEquals ("Exception message is incorrect.", "There was no XML start tag open.", e.Message);
725 xtw.WriteStartElement ("foo");
726 xtw.WriteEndElement ();
727 Assertion.AssertEquals ("<foo />", StringWriterText);
729 xtw.WriteStartElement ("bar");
730 xtw.WriteStartAttribute ("baz", null);
731 xtw.WriteEndElement ();
732 Assertion.AssertEquals ("<foo /><bar baz='' />", StringWriterText);
736 public void FullEndElement ()
738 xtw.WriteStartElement ("foo");
739 xtw.WriteFullEndElement ();
740 Assertion.AssertEquals ("<foo></foo>", StringWriterText);
742 xtw.WriteStartElement ("bar");
743 xtw.WriteAttributeString ("foo", "bar");
744 xtw.WriteFullEndElement ();
745 Assertion.AssertEquals ("<foo></foo><bar foo='bar'></bar>", StringWriterText);
747 xtw.WriteStartElement ("baz");
748 xtw.WriteStartAttribute ("bar", null);
749 xtw.WriteFullEndElement ();
750 Assertion.AssertEquals ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", StringWriterText);
754 public void WriteQualifiedName ()
756 xtw.WriteStartElement (null, "test", null);
757 xtw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
758 xtw.WriteQualifiedName ("bob", "http://localhost/");
759 xtw.WriteEndElement ();
761 Assertion.AssertEquals ("<test xmlns:me='http://localhost/'>me:bob</test>", StringWriterText);
765 public void WriteRaw ()
767 xtw.WriteRaw("&<>\"'");
768 Assertion.AssertEquals ("&<>\"'", StringWriterText);
771 Assertion.AssertEquals ("&<>\"'", StringWriterText);
774 Assertion.AssertEquals ("&<>\"'", StringWriterText);
778 public void WriteRawInvalidInAttribute ()
780 xtw.WriteStartElement ("foo");
781 xtw.WriteStartAttribute ("bar", null);
782 xtw.WriteRaw ("&<>\"'");
783 xtw.WriteEndAttribute ();
784 xtw.WriteEndElement ();
785 Assertion.AssertEquals ("<foo bar='&<>\"'' />", StringWriterText);
789 public void WriteStateTest ()
791 Assertion.AssertEquals (WriteState.Start, xtw.WriteState);
792 xtw.WriteStartDocument ();
793 Assertion.AssertEquals (WriteState.Prolog, xtw.WriteState);
794 xtw.WriteStartElement ("root");
795 Assertion.AssertEquals (WriteState.Element, xtw.WriteState);
796 xtw.WriteElementString ("foo", "bar");
797 Assertion.AssertEquals (WriteState.Content, xtw.WriteState);
799 Assertion.AssertEquals (WriteState.Closed, xtw.WriteState);
803 public void WriteString ()
805 xtw.WriteStartDocument ();
807 xtw.WriteString("foo");
808 } catch (InvalidOperationException) {}
810 // Testing attribute values
812 xtw.WriteStartElement ("foo");
813 xtw.WriteAttributeString ("bar", "&<>");
814 Assertion.AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='&<>'", StringWriterText);
818 public void WriteAttributeStringSingleQuoteChar()
820 // When QuoteChar is single quote then replaces single quotes within attributes
821 // but not double quotes.
822 xtw.WriteStartElement ("foo");
823 xtw.WriteAttributeString ("bar", "\"baz\"");
824 xtw.WriteAttributeString ("quux", "'baz'");
825 Assertion.AssertEquals ("<foo bar='\"baz\"' quux=''baz''", StringWriterText);
829 public void WriteAttributeStringDoubleQuoteChar()
831 // When QuoteChar is double quote then replaces double quotes within attributes
832 // but not single quotes.
834 xtw.WriteStartElement ("foo");
835 xtw.WriteAttributeString ("bar", "\"baz\"");
836 xtw.WriteAttributeString ("quux", "'baz'");
837 Assertion.AssertEquals ("<foo bar=\""baz"\" quux=\"'baz'\"", StringWriterText);
841 public void WriteStringWithEntities()
843 // Testing element values
844 xtw.QuoteChar = '\'';
845 xtw.WriteElementString ("foo", "&<>\"'");
846 Assertion.AssertEquals ("<foo>&<>\"'</foo>", StringWriterText);
850 public void XmlLang ()
852 Assertion.AssertNull (xtw.XmlLang);
854 xtw.WriteStartElement ("foo");
855 xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
856 Assertion.AssertEquals ("langfoo", xtw.XmlLang);
857 Assertion.AssertEquals ("<foo xml:lang='langfoo'", StringWriterText);
859 xtw.WriteAttributeString ("boo", "yah");
860 Assertion.AssertEquals ("langfoo", xtw.XmlLang);
861 Assertion.AssertEquals ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
863 xtw.WriteElementString("bar", "baz");
864 Assertion.AssertEquals ("langfoo", xtw.XmlLang);
865 Assertion.AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
867 xtw.WriteString("baz");
868 Assertion.AssertEquals ("langfoo", xtw.XmlLang);
869 Assertion.AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
871 xtw.WriteStartElement ("quux");
872 xtw.WriteStartAttribute ("xml", "lang", null);
873 Assertion.AssertEquals ("langfoo", xtw.XmlLang);
874 Assertion.AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
876 xtw.WriteString("langbar");
877 Assertion.AssertEquals ("langfoo", xtw.XmlLang);
878 Assertion.AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
880 xtw.WriteEndAttribute ();
881 Assertion.AssertEquals ("langbar", xtw.XmlLang);
882 Assertion.AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
884 // check if xml:lang repeats output even if same as current scope.
885 xtw.WriteStartElement ("joe");
886 xtw.WriteAttributeString ("xml", "lang", null, "langbar");
887 Assertion.AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", StringWriterText);
890 xtw.WriteElementString ("quuux", "squonk");
891 Assertion.AssertEquals ("langbar", xtw.XmlLang);
892 Assertion.AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
894 xtw.WriteEndElement ();
895 xtw.WriteEndElement ();
896 Assertion.AssertEquals ("langfoo", xtw.XmlLang);
897 Assertion.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);
899 xtw.WriteEndElement ();
900 Assertion.AssertNull (xtw.XmlLang);
901 Assertion.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);
904 Assertion.AssertNull (xtw.XmlLang);
907 // TODO: test operational aspects
909 public void XmlSpaceTest ()
911 xtw.WriteStartElement ("foo");
912 Assertion.AssertEquals (XmlSpace.None, xtw.XmlSpace);
914 xtw.WriteStartElement ("bar");
915 xtw.WriteAttributeString ("xml", "space", null, "preserve");
916 Assertion.AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
917 Assertion.AssertEquals ("<foo><bar xml:space='preserve'", StringWriterText);
919 xtw.WriteStartElement ("baz");
920 xtw.WriteAttributeString ("xml", "space", null, "preserve");
921 Assertion.AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
922 Assertion.AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText);
924 xtw.WriteStartElement ("quux");
925 xtw.WriteStartAttribute ("xml", "space", null);
926 Assertion.AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
927 Assertion.AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
929 xtw.WriteString ("default");
930 Assertion.AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
931 Assertion.AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
933 xtw.WriteEndAttribute ();
934 Assertion.AssertEquals (XmlSpace.Default, xtw.XmlSpace);
935 Assertion.AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText);
937 xtw.WriteEndElement ();
938 Assertion.AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
939 xtw.WriteEndElement ();
940 Assertion.AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
941 xtw.WriteEndElement ();
942 Assertion.AssertEquals (XmlSpace.None, xtw.XmlSpace);
944 xtw.WriteStartElement ("quux");
946 xtw.WriteAttributeString ("xml", "space", null, "bubba");
947 } catch (ArgumentException) {}
950 xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
951 } catch (ArgumentException) {}
954 xtw.WriteAttributeString ("xml", "space", null, "Preserve");
955 } catch (ArgumentException) {}
958 xtw.WriteAttributeString ("xml", "space", null, "Default");
959 } catch (ArgumentException) {}
962 xtw.WriteWhitespace ("x");
963 } catch (ArgumentException) { }
967 public void XmlSpaceRaw ()
969 xtw.WriteStartElement ("foo");
970 xtw.WriteStartAttribute ("xml", "space", null);
971 Assertion.AssertEquals (XmlSpace.None, xtw.XmlSpace);
972 Assertion.AssertEquals ("<foo xml:space='", StringWriterText);
974 xtw.WriteString ("default");
975 Assertion.AssertEquals (XmlSpace.None, xtw.XmlSpace);
976 Assertion.AssertEquals ("<foo xml:space='", StringWriterText);
978 xtw.WriteEndAttribute ();
979 Assertion.AssertEquals (XmlSpace.Default, xtw.XmlSpace);
980 Assertion.AssertEquals ("<foo xml:space='default'", StringWriterText);
984 public void WriteAttributes ()
986 XmlDocument doc = new XmlDocument();
987 StringWriter sw = new StringWriter();
988 XmlWriter wr = new XmlTextWriter(sw);
989 StringBuilder sb = sw.GetStringBuilder();
990 XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
991 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);
993 xtr.Read(); // read XMLDecl
994 wr.WriteAttributes(xtr, false);
995 // This method don't always have to take this double-quoted style...
996 Assertion.AssertEquals("#WriteAttributes.XmlDecl.1", "version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString().Trim());
998 sb.Remove(0, sb.Length); // init
999 ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
1000 xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
1001 xtr.Read(); // read XMLDecl
1002 wr.WriteAttributes(xtr, false);
1003 // This method don't always have to take this double-quoted style...
1004 Assertion.AssertEquals("#WriteAttributes.XmlDecl.2", "version=\"1.0\" standalone=\"no\"", sw.ToString().Trim());
1006 sb.Remove(0, sb.Length); // init
1007 xtr.Read(); // read root
1008 wr.WriteStartElement(xtr.LocalName, xtr.Value);
1009 wr.WriteAttributes(xtr, false);
1010 wr.WriteEndElement();
1012 // This method don't always have to take this double-quoted style...
1013 Assertion.AssertEquals("#WriteAttributes.Element", "<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString().Trim());