Get internal Declaration property of a XmlDocument
May 25, 2009
0 Comments
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); }