I am working on some code to read an XML fragment that contains an XML declaration, for example. <?xml version="1.0" encoding="utf-8"?>and analyze the encoding. From MSDN, I have to do it as follows:
var nt = new NameTable();
var mgr = new XmlNamespaceManager(nt);
var context = new XmlParserContext(null, mgr, null, XmlSpace.None);
var reader = new System.Xml.XmlTextReader(@"<?xml version=""1.0"" encoding=""UTF-8""?>",
System.Xml.XmlNodeType.XmlDeclaration, context);
However, I get System.Xml.XmlExceptionwhen calling the constructor System.Xml.XmlTextReaderwith an error message:
XmlNodeType XmlDeclaration is not supported for partial content parsing.
I searched for this error in quotation marks - itβs for sure that zero results were found (edit: now there is one result: this post) - and without quotation marks, which does not bring anything useful. I also looked at the MSDN for XmlNodeType and it says nothing that it is not supported.
What am I missing here? How can I get an instance XmlTextReaderfrom an XML declaration fragment ?
Notice my goal here is simply to determine the encoding of a partially constructed XML document , where I assume that it at least contains a node declaration; so I'm trying to get one reader.Encoding. If there is another way to do this, I am open to this.
I am currently processing the ad manually using regular expression, which is not the best approach.