Programmatically insert XML Declaration using C# (.NET)
The following snippet will be useful for inserting the XML Declaration for a given XML file.
private static void Add_Declaration_2_XML(String sXMLFile)
{
try
{
XmlDocument XDoc = new XmlDocument();
XDoc.Load( sXMLFile);
XmlDeclaration XDec;
XDec = XDoc.CreateXmlDeclaration("1.0", "us-ascii", "yes");
XmlElement XElem = XDoc.DocumentElement;
XDoc.InsertBefore(XDec, XElem);
XDoc.Save(sXMLFile + ".new");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
No comments:
Post a Comment