Share via


Remove xml declaration from xml dynamically

Question

Monday, July 16, 2012 7:44 PM

Hi,

I would like to know if there is a dynamic way to remove the xml declaration from the xml file.

In my case, I do not know what is going to come in the xml declaration since I do not have access to the generatio of the xml.

Sometimes, I will get non sense encoding like Cp1252 or the other stardards

<?xml version="1.0" encoding="Cp1252"?>

<?xml version="1.0" encoding="utf-16"?>

Therefore, I need a dynamic way to remove the declaration based maybe in the beginning of the declaration <?xml version="1.0" or the end. I am not user and would appreciate some help.

Cheers

C

All replies (1)

Tuesday, July 17, 2012 8:42 AM âś…Answered

StreamReader sr = new StreamReader("File.xml");XmlDocument inDoc = new XmlDocument();doc.LoadXml(sr.ReadToEnd());sr.Close();StringBuilder sb = new StringBuilder();XmlWriterSettings settings = new XmlWriterSettings();settings.OmitXmlDeclaration = true;XmlWriter xWriter = XmlWriter.Create(sb, settings);inDoc.Save(xWriter);xWriter.Close();XmlDocument outDoc = new XmlDocument();outDoc.LoadXml(sb.ToString());

Karthikeya