trouble with xml writer

A

Aahz

asp.net 1.1 and C#
Here is code:
xmlfile = new XmlDocument();
xmlfilename = Server.MapPath("doc.xml");
writer = new XmlTextWriter(xmlfilename, System.Text.Encoding.UTF8);
writer.Formatting = System.Xml.Formatting.Indented;

writer.WriteStartDocument(true);
writer.WriteComment("this is a comment");
writer.WriteStartElement("cities");

writer.WriteStartElement("city");
writer.WriteAttributeString("latitude","4.778");
writer.WriteAttributeString("longitude","5.45");
writer.WriteAttributeString("name",namestring);
writer.WriteEndElement();

writer.WriteEndElement();
writer.WriteEndDocument();
xmlfile.Save(xmlfilename);
writer.Flush();
writer.Close();

This is supose to crate and save doc.xml with data but every time I
got error message:

"This an invalid XML document, The document does not have a root
element"

What am I doing wrong ????

Thank you
 
M

Marc Gravell

Remove the "xmlfile" lines ("new" and "Save"); it isn't doing anything
useful here, and the xmlfile.Save(...) is potentially breaking
everything.

Marc
 
M

Michael Rubinstein

Aahz, looks like the problem is writer.WriteAttributeString() between
writer.WriteStartElement() and writer.WriteEndElement() . Should be
writer.WriteElementString().

Michael
 

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