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))
{
/ / Indent, without this, xml is a line
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();
/ / Refresh the stream
xwriter.Flush();
xwriter.Close();
}
}
}
Source: Http://social.microsoft.com/Forums/es-ES/visualcshartzhchs/thread/f0b1f040-724c-48d3-aa59-b828e45ddeca
The XML file written with XmlTextWriter does not wrap, when you open the XML file with Notepad is written on a line, how to add line!