本文整理汇总了C#中System.IO.StringWriter.XmlWriter方法的典型用法代码示例。如果您正苦于以下问题:C# StringWriter.XmlWriter方法的具体用法?C# StringWriter.XmlWriter怎么用?C# StringWriter.XmlWriter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.StringWriter
的用法示例。
在下文中一共展示了StringWriter.XmlWriter方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: to_xml
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();
}
}
示例2: write
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();
}
示例3: xml_writer_text_writer
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);
}