我创建了一个名为test1.xml的XML文档,该文档链接到定义了实体circ的外部dtd mydtd2.dtd。两个文件都保存在同一个文件夹中。但是当使用XmlReader读取XML文件时,我得到错误参考未声明的实体circ。
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE test1 SYSTEM "mydtd2.dtd">
<test1>
print this character ˆ
</test1>
<!ELEMENT test1 >
<!ENTITY circ "^">
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
settings.CheckCharacters = false;
XmlDocument doc = new XmlDocument();
using (XmlReader reader = XmlReader.Create(filename, settings))
{
doc.Load(reader);
}
当我在内部将实体添加到XML文件的顶部时,它可以正常工作。
<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE wow [
<!ENTITY circ "^" >
]>
<test1>
wow can this work ( j ˆy )
</test1>
答案 0 :(得分:2)
我将此添加到设置中。
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultCredentials;
settings.XmlResolver = resolver;