Error in xml file after write

G

Guest

I read and write from an xml file. Running the code below removes the first
part of the xml declaration. That is, instead of "<?xml...>" I get "ml...>"
in the begin of the xml file. The code is part of a service. I hope someone
can help.

Regards
Martin

XmlDocument scanDoc = new XmlDocument();
scanDoc.Load(@"scan.xml");

XmlNode newElem = scanDoc.CreateNode(XmlNodeType.Element, "tag", null);
newElem.InnerXml = "<tag1></tag1>" + "<tag2></tag2> +
... +"<tagX></tagX>";

newElem["tag1"].InnerText = argTag1;
XmlElement valueElem = scanDoc.CreateElement("tag1");
....
newElem["tagX"].InnerText = argTagX;
XmlElement commentElem = scanDoc.CreateElement("tagX");

scanDoc.DocumentElement.PrependChild(newElem);

scanDoc.PreserveWhitespace = true;
XmlTextWriter xmlWrtr = new XmlTextWriter(@"scan.xml", Encoding.ASCII);
xmlWrtr.Formatting = Formatting.Indented;
xmlWrtr.Indentation = 2;
scanDoc.Normalize();
scanDoc.WriteTo(xmlWrtr);
xmlWrtr.Close();
 

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