从XML声明片段获取XML编码:部分内容解析不支持XmlDeclaration [英] Obtaining the XML encoding from an XML declaration fragment: XmlDeclaration is not supported for partial content parsing
问题描述
我正在处理一些代码以读取包含XML声明的XML片段,例如<?xml version="1.0" encoding="utf-8"?>
并解析编码.从 MSDN 中,我应该能够为此:
I'm working on some code to read an XML fragment which contains an XML declaration, e.g. <?xml version="1.0" encoding="utf-8"?>
and parse the encoding. From MSDN, I should be able to do it like this:
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);
但是,在对System.Xml.XmlTextReader
构造函数的调用中出现System.Xml.XmlException
并出现错误消息:
However, I'm getting a System.Xml.XmlException
on the call to the System.Xml.XmlTextReader
constructor with an error message:
XmlNodeType XmlDeclaration不支持部分内容 解析.
XmlNodeType XmlDeclaration is not supported for partial content parsing.
我已经用引号对这个错误进行了搜索-找到的结果恰好为零(现在有一个结果:该帖子)-并且没有引号,这没有任何用处.我还查看了 MSDN XmlNodeType ,并且没有说明它不受支持.
I've googled this error in quotes -- exactly zero results found (edit: now there's one result: this post) -- and without quotes, which yields nothing useful. I've also looked at MSDN for the XmlNodeType, and it doesn't say anything about it not being supported.
我在这里想念什么? 如何从XML声明片段中获取XmlTextReader
实例?
What am I missing here? How can I get an XmlTextReader
instance from an XML declaration fragment?
请注意,我的目标只是确定部分构建的XML文档的编码,在此我假设它至少包含一个声明节点.因此,我正在尝试获取reader.Encoding
.如果还有其他方法可以做到,那么我很乐意.
Note, my goal here is just to determine the encoding of a partially-built XML document where I'm making the assumption that it at least contains a declaration node; thus, I'm trying to get reader.Encoding
. If there's another way to do that, I'm open to that.
目前,我正在使用正则表达式手动解析声明,这不是最好的方法.
At present, I'm parsing the declaration manually using regex, which is not the best approach.
推荐答案
更新:从XML文档或XML片段获取编码: