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();
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();