當前位置: 首頁>>代碼示例>>C#>>正文


C# StringWriter.XmlWriter方法代碼示例

本文整理匯總了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();
      }
    }
開發者ID:prokhor-ozornin,項目名稱:Catharsis.NET.Commons,代碼行數:45,代碼來源:ObjectExtensionsTest.cs

示例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();
    }
開發者ID:prokhor-ozornin,項目名稱:Catharsis.NET.Commons,代碼行數:18,代碼來源:XmlExtensionsTest.cs

示例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);
    }
開發者ID:prokhor-ozornin,項目名稱:Catharsis.NET.Commons,代碼行數:27,代碼來源:StreamExtensionsTest.cs


注:本文中的System.IO.StringWriter.XmlWriter方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。