`

C# XmlWriter write to a string rather than write to File

    博客分类:
  • C#
阅读更多

XmlWriter is a stream based Xml processor. The Stream based XML process is more effeicent in memory and low in CPU usage. however, it does not have convenient wasy as the DOM based one, where you can easily load and write to string.

 

 

here is an example shows how you can write with XmlWriter to a string.

 

 

 

[Test]
    public void Test_XmlWriter_to_String_Should_be_Valid()
    {
      //var xmlWriter = new XmlWriter();

      // basically this is going to write to StringWriter
      // what you need to do is just to apply the decorator pattern, 
      // XmlWriter -> StringWriter

      using (var sw = new StringWriter())
      {
        using (var xmlWriter = XmlWriter.Create(sw))
        {
          xmlWriter.WriteStartElement("Metrics");
          xmlWriter.WriteStartElement("Keys");
          xmlWriter.WriteStartElement("SessionID");
          xmlWriter.WriteAttributeString("Type", "System.String");
          xmlWriter.WriteString("{sessionId}");
          xmlWriter.WriteEndElement();
          xmlWriter.WriteEndElement();
          xmlWriter.WriteEndElement();

          xmlWriter.Flush();
        }


        Assert.AreEqual("<?xmlversion=\"1.0\"encoding=\"utf-16\"?><Metrics><Keys><SessionIDType=\"System.String\">{sessionId}</SessionID></Keys></Metrics>",  sw.ToString().Replace("\r\n", "").Replace(" ", ""));
      }

 

 

next we are going to discuss the Formatter and etc...

 

 

The discussion C# XmlTextWriter to provide simple formatting when writing to StringWriter. 

分享到:
评论

相关推荐

    C# XML ReadWrite示例

    XML ReadWrite经典实例,有注释. 用C#实现XML文件读写的示例,比较详尽,简单易懂,认真看两三分钟即可掌握如何创建一个较复杂的XML文档 供有需要的同学学习参考.

    使用XmlWriter写入XML文档

    使用XmlWriter写入XML文档,用户可以在文本框中输入数据。点击“写入”按钮后,程序即可在程序文件夹中自动生成一个名为“Customers.xml”的XML文档。

    高清彩版 Functional Programming in C# How to write better C# code

    Functional Programming in C# How to write better C# code

    C# XML文件读取示例

    C# XML文件读取示例

    C# and XML Primer

    Write XML with XMLDocument, XDocument, and XMLWriter Modifiy your XML with XML Document and XDocument Who This Book Is For Experienced Windows application programmers/developers using .NET and C# who ...

    C# xml多个同名节点操作

    方便对xml中任意节(多个同名节点)点属性进行获取、设置。... public bool SetNodeValueByIndex(XmlDocument xmlContent, string parentNode, int index,string sonNode,string grandSonNode, string valueStr)

    Visual C# XMLWriter XML读取器

    Visual C# XMLWriter XML读取器,可了解到如何使用C#写XML声明,引用样式,写DOCTYPE声明,写一个元素(根元素)、写入 CDATA、写标记元素、缓冲器内的内容写入文件,将XML文件的内容显示在控制台中。 运行环境:...

    c# xml操作基类

    c# xml 操作 基类

    C# XML demo,C# XML入门,XmlHelper

    After working with a couple of XML projects for the last six months, I am ready to share a class I wrote called XmlHelper which got me through all the complexity of XML and made my life a lot simpler....

    C#XML入门经典(源码)

    C#编程人员必备的XML技能-C#XML入门经典源码 C#编程人员必备的XML技能-C#XML入门经典源码 C#编程人员必备的XML技能-C#XML入门经典源码 C#编程人员必备的XML技能-C#XML入门经典源码

    C# XML操作类库(加强版)

    最近做项目,要用C#写,而且数据格式是那种节点套节点能把人套死的XML,看着都觉得晕,而且节点跳跃性很大,真心觉得单纯用环境自带的XMLDocument类完全达不到自己的需要,因为XML里面涉及到节点循环,循环里面又套...

    C#读取XML 的节点,以及属性

    C#读取XML相关节点,属性,修改XMl

    RSA PEM PKCS8 to c# xml 1.12

    RSA PEM PKCS8 to c# xml 1.11 最近学C# 做了个RSA PEM PKCS8 TO C# XML 的密钥转换工具,水平有限大家测试下,需要dotnetframework支持.

    C#操作XML文件完整源码(十分经典)

    C#操作XML文件完整源码(十分经典)

    C#读取XML文件并写入数据库

    C#读取XML文件并写入数据库的三种方法,直接写入和先读取到内存再写入数据库

    C# xmlToList xml转换成对象

    主要实现读取xml文件内容然后转换成List对象,两种实现方法。

    基于C#中XmlWriter写入Xml的深入分析

    假定创建了XmlWriter的实例变量xmlWriter,下文中将使用此实例变量写Xml1.如何使用XmlWriter写Xml文档声明 代码如下:// WriteStartDocument方法可以接受一个bool参数(表示standalone,是否为独立文档)或者不指定...

    C# to Java 代码转换工具

    Running from the command line: C# to Java Converter can be launched directly for a specific project, folder, or file conversion from the command line. Command line.(命令行执行) 其他一些特点: 1. ...

    Functional Programming in C# How to write better C# code 无水印转化版pdf

    Functional Programming in C# How to write better C# code 英文无水印转化版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如有侵权,请联系上传...

Global site tag (gtag.js) - Google Analytics