Example #1
0
        public void TestWriteMethods()
        {
            //WriteValue
            TextWriter    writer = new StringWriter();
            SimpleElement se     = new SimpleElement("root", "some\ntext");

            se.WriteValue(writer, true);
            Assert.AreEqual(se.GetString(), writer.ToString());

            writer = new StringWriter();
            IndentingWriter indentedWriter = new IndentingWriter(writer, 2);

            se.WriteValue(indentedWriter, true);
            Assert.AreEqual(se.GetString(), writer.ToString());

            //WriteXml
            se     = new SimpleElement("root");
            writer = new StringWriter();
            se.WriteXml(writer, true);
            Assert.AreEqual("<root/>", writer.ToString());

            se.AddAttribute("attr1").SetString("value1");
            writer = new StringWriter();
            se.WriteXml(writer, true);
            Assert.AreEqual("<root attr1='value1'/>", writer.ToString());

            se.Comment = "comment text";
            se.SetString("content");
            writer = new StringWriter();
            se.WriteXml(writer, true);
            Assert.AreEqual("<root attr1='value1'><!--" + writer.NewLine + "comment text" + writer.NewLine + "-->content</root>", writer.ToString());
            writer = new StringWriter();
            se.WriteXml(writer, false);
            Assert.AreEqual("<root attr1='value1'><!--comment text-->content</root>", writer.ToString());

            se.AddElement("child").SetString("child content");
            writer = new StringWriter();
            se.WriteXml(writer, true);
            string result = "<root attr1='value1'>" + writer.NewLine + "  <!--" + writer.NewLine + "  comment text" +
                            writer.NewLine + "  -->" + writer.NewLine + "content" + writer.NewLine + "  <child>child content</child>" +
                            writer.NewLine + "</root>";

            Assert.AreEqual(result, writer.ToString());
            Assert.AreEqual(result, se.ToString());

            writer = new StringWriter();
            se.WriteXml(writer, false);
            result = "<root attr1='value1'><!--comment text-->content<child>child content</child></root>";
            Assert.AreEqual(result, writer.ToString());
        }