The XmlDocument class contains a internal property Declaration of type XmlDeclaration. 
To use the information in this object use the following code:
 
public string GetXmlDocumentDeclarationText()
        {
            string xmlText = @"<?xml version=""1.0"" encoding=""utf-8""?>
<RootNode>
    <SubNode>
    </SubNode>
</RootNode>
";
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(xmlText);
            System.Xml.XmlDeclaration dec = (System.Xml.XmlDeclaration)this.GetPrivateProperty(doc, "Declaration");
            return dec.OuterXml.Trim();
        }
        public object GetPrivateField(object target, string field)
        {
            System.Reflection.FieldInfo fi = target.GetType().GetField(field, 
BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
            return fi.GetValue(target);
        }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.