Is there a way to prevent .NET\'s XmlReader
class from expanding XML entities into their value when reading the content?
For instance, suppose the follo
One way to do that is use `XmlTextReader', like this:
using (var reader = new XmlTextReader(@"your url"))
{
// note this
reader.EntityHandling = EntityHandling.ExpandCharEntities;
while (reader.Read())
{
// here it will be EntityReference with no exceptions
}
}
If that is not an option - you can do the same with XmlReader
, but some reflection will be required (at least I don't aware of another way):
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
}
}