I had a requirement, in which I need to remove Xml declaration from incoming xml message. Message was being generated from external system.
Xml Declaration is of the form:
<?xml version="1.0" encoding="UTF-8"?>
Although I don’t see anything wrong in having xml declaration, but I had a requirement to remove it. After googling for some time I didn’t get any working code. Then I tried the following. Sharing here, it might be useful to some other geek.
StreamReader sr = new StreamReader("File.xml");
XmlDocument inDoc = new XmlDocument();
StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
XmlWriter xWriter = XmlWriter.Create(sb, settings);
XmlDocument outDoc = new XmlDocument();