public void to_xml() { Assert.Throws<ArgumentNullException>(() => XmlExtensions.ToXml<object>(null)); Assert.Throws<ArgumentNullException>(() => XmlExtensions.ToXml<object>(null, Stream.Null)); Assert.Throws<ArgumentNullException>(() => new object().ToXml((Stream) null)); Assert.Throws<ArgumentNullException>(() => XmlExtensions.ToXml<object>(null, TextWriter.Null)); Assert.Throws<ArgumentNullException>(() => new object().ToXml((TextWriter) null)); Assert.Throws<ArgumentNullException>(() => XmlExtensions.ToXml<object>(null, XmlWriter.Create(Stream.Null))); Assert.Throws<ArgumentNullException>(() => new object().ToXml((XmlWriter) null)); var subject = Guid.NewGuid().ToString(); var xml = subject.ToXml(); var stringWriter = new StringWriter(); stringWriter.XmlWriter().Write(writer => { new XmlSerializer(subject.GetType()).Serialize(writer, subject); Assert.Equal(xml, stringWriter.ToString()); }); Assert.Equal(xml, subject.ToXml((Type[]) null)); Assert.Equal(xml, subject.ToXml(Enumerable.Empty<Type>().ToArray())); Assert.Equal(xml, subject.ToXml((Type[]) null)); using (var stream = new MemoryStream()) { Assert.True(ReferenceEquals(subject.ToXml(stream, Encoding.Unicode), subject)); Assert.Equal(xml, stream.Rewind().Text()); Assert.True(stream.CanWrite); } using (var writer = new StringWriter()) { Assert.True(ReferenceEquals(subject.ToXml(writer), subject)); Assert.Equal(xml, writer.ToString()); writer.WriteLine(); } stringWriter = new StringWriter(); using (var writer = stringWriter.XmlWriter()) { Assert.True(ReferenceEquals(subject.ToXml(writer), subject)); Assert.Equal(xml, stringWriter.ToString()); stringWriter.WriteLine(); } }
public void write() { Assert.Throws<ArgumentNullException>(() => XmlExtensions.Write<XmlWriter>(null, writer => {})); Assert.Throws<ArgumentNullException>(() => XmlExtensions.Write(XmlWriter.Create(Path.GetTempFileName()), null)); const string Xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?><article>text</article>"; var stringWriter = new StringWriter(); var xmlWriter = stringWriter.XmlWriter(true, Encoding.Unicode); Assert.True(ReferenceEquals(xmlWriter.Write(writer => { writer.WriteStartDocument(); writer.WriteElementString("article", "text"); writer.WriteEndDocument(); }), xmlWriter)); Assert.Throws<InvalidOperationException>(() => xmlWriter.WriteRaw(string.Empty)); Assert.Equal(Xml, stringWriter.ToString()); stringWriter.WriteLine(); }
public void xml_writer_text_writer() { Assert.Throws<ArgumentNullException>(() => ((TextWriter) null).XmlWriter()); const string Xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?><article>text</article>"; var textWriter = new StringWriter(); textWriter.XmlWriter().Write(writer => { Assert.False(writer.Settings.CloseOutput); Assert.Equal(Encoding.Unicode.ToString(), writer.Settings.Encoding.ToString()); Assert.False(writer.Settings.Indent); writer.WriteElementString("article", "text"); }); Assert.Equal(Xml, textWriter.ToString()); textWriter.Write(string.Empty); textWriter.Close(); textWriter = new StringWriter(); textWriter.XmlWriter(true).Write(writer => { Assert.True(writer.Settings.CloseOutput); Assert.Equal(Encoding.Unicode.ToString(), writer.Settings.Encoding.ToString()); Assert.False(writer.Settings.Indent); }); textWriter.Write(string.Empty); }