Wednesday, November 10, 2010

C# Tutorial: Save Text to XML File [Save ListBox Items]

Here is the code

using System.Xml;
///////////////////////////
XmlTextWriter xwriter = new XmlTextWriter("savefile.xml", Encoding.Unicode);
xwriter.WriteStartDocument();
xwriter.WriteStartElement("XMLFILE");

xwriter.WriteStartElement("Title");
xwriter.WriteString(textBox1.Text);
xwriter.WriteEndElement();

foreach(String item in listBox1.Items)
{
    xwriter.WriteStartElement("Item");
    xwriter.WriteString(item);
    xwriter.WriteEndElement();
}

xwriter.WriteEndElement();
xwriter.WriteEndDocument();
xwriter.Close();

1 comment:

  1. How does it work with load an txt file an d convert to xml?

    ReplyDelete