我从事 Unity3d 项目,该项目使用 .NET 平台的 Mono 实现。我在包含高和低 unicode 代理项的 XML 上使用 XmlReader,例如:
<node data="�� ...
^ Here's the symbol referenced in exception
XML 没有标签——这很糟糕,但为了向后兼容不能更改。(出于同样的原因,整个 xml 几乎无法更改)。因此,我的理解是 XmlReader 假定 XML 版本 1.0,其中 unicode 代理项是非法字符。
但是,我不希望 XmlReader 检查字符的合法性,并使用 CheckCharacter 设置告诉他不要这样做。这是代码:
public void Load(MemoryStream stream)
{
using (var reader = System.Xml.XmlReader.Create(stream))
{
reader.Settings.CheckCharacters = false;
m_Document = XDocument.Load(reader); // Here goes exception
}
}
但是,我仍然得到这个异常:
XmlException: Referenced character was not allowed in XML. Normalization is True, checkCharacters = True Line 1, position 580.
Mono.Xml2.XmlTextReader.ReadCharacterReference ()
Mono.Xml2.XmlTextReader.ReadAttributeValueTokens (Int32 dummyQuoteChar)
Mono.Xml2.XmlTextReader.ReadAttributes (Boolean isXmlDecl)
Mono.Xml2.XmlTextReader.ReadStartTag ()
Mono.Xml2.XmlTextReader.ReadContent ()
Mono.Xml2.XmlTextReader.Read ()
System.Xml.XmlTextReader.Read ()
Mono.Xml.XmlFilterReader.Read ()
System.Xml.XmlReader.ReadEndElement ()
System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XDocument.ReadContent (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XDocument.LoadCore (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XDocument.Load (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XDocument.Load (System.Xml.XmlReader reader)
(My code that you can see above)
真正奇怪的是,当我将其设置为 false 时,它会显示“checkCharacters = True”。我究竟做错了什么?