public partial class Form1 : Form
{
private string songlocation = @"D:codeDemoxmlxmlTextxmlDemo.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