Gab*_* S. 5 .net c# xml dtd xmlreader
有没有办法阻止 .NET 的XmlReader类在读取内容时将 XML 实体扩展为其值?
例如,假设使用以下 XML 作为输入:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE author PUBLIC "ISO 8879:1986//ENTITIES Added Latin 1//EN//XML" "http://www.oasis-open.org/docbook/xmlcharent/0.3/iso-lat1.ent" >
<author>á</author>
Run Code Online (Sandbox Code Playgroud)
我们假设不可能达到扩展急性实体所需的外部 OASIS DTD。我希望读者按顺序读取author 元素,然后是type 的aacute 节点EntityReference,最后是author end 元素,而不会抛出任何错误。我怎样才能实现这个目标?
更新:我还想防止字符实体的扩展,例如á.
一种方法是使用“XmlTextReader”,如下所示:
using (var reader = new XmlTextReader(@"your url"))
{
// note this
reader.EntityHandling = EntityHandling.ExpandCharEntities;
while (reader.Read())
{
// here it will be EntityReference with no exceptions
}
}
Run Code Online (Sandbox Code Playgroud)
如果这不是一个选项 - 您可以对 执行相同的操作XmlReader,但需要进行一些反思(至少我不知道还有其他方法):
using (var reader = XmlReader.Create(@"your url", new XmlReaderSettings() {
DtdProcessing = DtdProcessing.Ignore // or Parse
})) {
// get internal property which has the same function as above in XmlTextReader
reader.GetType().GetProperty("EntityHandling", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(reader, EntityHandling.ExpandCharEntities);
while (reader.Read()) {
// here it will be EntityReference with no exceptions
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1832 次 |
| 最近记录: |