编写CRLF [英] Writing CRLF

查看:100
本文介绍了编写CRLF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将回车符+换行符+制表符写入xml文件,这怎么可能?

I need to write a carriage return + line feed + tab to an xml file, how is this possible?

FileStream fs = new FileStream(_file, FileMode.Create);
            XmlTextWriter w = new XmlTextWriter(fs, null);
            w.WriteStartDocument();
            w.WriteStartElement("articles");
            w.WriteComment("This file was generated by the XmlTextWriter class");
            w.WriteChars... :confused:

推荐答案

您的意思是ControlChars.CrLf?


Environment.NewLine可以为您提供此功能.
Environment.NewLine gives you that.


我发现添加以下行会格式化我的 xml文件:
I found that adding the following lines formats my xml file:
 w.Formatting = Formatting.Indented;
w.Indentation = 5;


用于写入XML文件(C#)的完整代码段


Complete code snippet for writing to an XML file (C#)

using System;
using System.Xml;
using System.IO;
namespace WriteToXML
{
    class Program
    {
        static void Main(string[] args)
        {
            string _path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            _path = _path.Substring(6, _path.Length - 6);
            string _file =  _path + "\\article.xml";
            FileStream fs = new FileStream(_file, FileMode.Create);
            XmlTextWriter w = new XmlTextWriter(fs, null);
            w.Formatting = Formatting.Indented;
            w.Indentation = 5;
            w.WriteStartDocument();
            w.WriteStartElement("articles");
            w.WriteComment("This file was generated by the XmlTextWriter class");
            //write content
            w.WriteStartElement("article");
            w.WriteStartElement("timestamp");
            w.WriteString(DateTime.Now.ToString());
            w.WriteEndElement();
            w.WriteStartElement("subject");
            w.WriteString("subject");
            w.WriteEndElement();
            w.WriteStartElement("body");
            w.WriteString("This is a test");
            w.WriteEndElement();
            w.WriteStartElement("entryID");
            w.WriteEndElement();
            w.WriteStartElement("deleted");
            w.WriteString("No");
            w.WriteEndElement();
            //close root element
            w.WriteEndElement();
            w.WriteEndDocument();
            w.Close();
        }
    }
}


注意:
1. Matthew MacDonald,在C#2008 2nd Edition中开始ASP.NET 3.5
出版快点ISBN-13 978-1-59059-891-7. P.647至P.688


NOTE:
1. Matthew MacDonald, Beginning ASP.NET 3.5 in C# 2008 2nd Edition
Publ. Apress. ISBN-13 978-1-59059-891-7. P. 647 to P.688


这篇关于编写CRLF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆