I have an xml file which I need to modify and write it back to an outputfile. The problem is that the result outputfile contains an extra attribute 'standalone' in the root declartion which does not exist in the original inputfile. Is there any way how I can prevent from XmlDocument adding this attribute ?
The code I have tried:
//read input xml
XmlDocument xDoc = new XmlDocument();
xDoc.Load(originalFile);
//do some stuff
//....
//write back to output
using(XmlTextWriter xml2 = new XmlTextWriter(outputFile, Encoding.UTF8) { Formatting = Formatting.Indented })
{
xDoc.CreateXmlDeclaration("1.0", null, "");
xDoc.Save(xml2);
}
inputfile contains this:
<?xml version="1.0" encoding="UTF-8" ?>
...
output.xml contains this:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
...
The standalone parameter should be null or String.empty.
xDoc.CreateXmlDeclaration("1.0", null, null);
Also CreateXmlDecleration just creates a declaration object. You still need to add it to the document, like this:
XmlDeclaration xDecl = xDoc.CreateXmlDeclaration("1.0", null, null);
if (xDoc.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
xDoc.ReplaceChild(xDecl, xDoc.FirstChild);
else
xDoc.InsertBefore(xDecl, xDoc.DocumentElement);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With