码迷,mamicode.com
首页 > 其他好文 > 详细

用XmlTextWriter写出的XML文件没有换行,用记事本打开XML文件的时候都写在了一行上,请问怎样加入换行!

时间:2016-05-04 17:13:21      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

 public partial class Form1 : Form
    {
        private string songlocation = @"D:\codeDemo\xml\xmlText\xmlDemo.xml";

        private string[] songlist = new string[] {"a","b","c" };
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (XmlTextWriter xwriter = new XmlTextWriter(songlocation, Encoding.UTF8))
            {
                //缩进, 不加这个,xml就是一行
                xwriter.Formatting = Formatting.Indented;
                xwriter.WriteStartDocument();
                xwriter.WriteStartElement("myplayer");
                xwriter.WriteStartElement("playingsongs");
                xwriter.WriteAttributeString("total", songlist.Length.ToString());
                for (int i = 0; i < songlist.Length; i++)
                {
                    xwriter.WriteAttributeString("song" + i.ToString(), songlist[i].ToString());
                }
                xwriter.WriteEndElement();
                xwriter.WriteEndElement();
                xwriter.WriteEndDocument();

                //刷新流
                xwriter.Flush();
                xwriter.Close();
            }
        }
    }

 来源:http://social.microsoft.com/Forums/es-ES/visualcshartzhchs/thread/f0b1f040-724c-48d3-aa59-b828e45ddeca

用XmlTextWriter写出的XML文件没有换行,用记事本打开XML文件的时候都写在了一行上,请问怎样加入换行!

标签:

原文地址:http://www.cnblogs.com/leiliu-lucifer/p/5458977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!