write XML

L

Lloyd Dupont

I'm trying to write XML and I am quite unsuccessful so far,
I have a very simple application with 1 button and one click handler like
that, does anyone know what's wrong with this few lines ?
//-----------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
string file = "test.xml";
XmlTextWriter xtw = new XmlTextWriter(file, Encoding.UTF8);
try
{
xtw.WriteStartDocument();
xtw.WriteElementString("Name", "aName");
xtw.WriteStartElement("Data");
xtw.WriteElementString("Item", "anItem");
xtw.WriteElementString("Item", "anItem");
xtw.WriteElementString("Item", "anItem");
xtw.WriteEndElement();
xtw.WriteEndDocument();
}
finally { xtw.Close(); }
}

it throws an InvalidOperationExeption() when I try to
WriteStartElement("Data");
 
L

Lloyd Dupont

no worries, I found it !
I should have a first line setting the namespace ....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top