4

I need to set the MaxCharactersFromEntities on the XmlTextReader, this is my code so far :

xmlDocument = new XmlDocument();

xmlTextReader = new XmlTextReader(fileInfo.FullName);
xmlTextReader.Settings = new XmlReaderSettings();
xmlTextReader.Settings.MaxCharactersFromEntities = 0;
var vr = new XmlValidatingReader(xmlTextReader);
vr.ValidationType = ValidationType.None;
vr.EntityHandling = EntityHandling.ExpandEntities;

xmlDocument.Load(vr);

The Settings property is read-only so it can´t be set and its null? How is this supposed to work?

helb
  • 7,609
  • 8
  • 36
  • 58
Banshee
  • 15,376
  • 38
  • 128
  • 219
  • Closely related: http://stackoverflow.com/questions/1551912/difference-between-xmlreader-create-and-new-xmltextreader/1551916#1551916 – helb Jun 05 '15 at 08:40

2 Answers2

2

You're supposed to pass XmlReaderSettings instance upon construction of the XmlReader instance in the first place, rather than updating the reader's Settings property later -which is impossible as the property doesn't have setter- :

var xmlDocument = new XmlDocument();

//create XmlReaderSettings first
var settings = new XmlReaderSettings();
settings.MaxCharactersFromEntities = 80; //0 doesn't make sense here, as it's the default value

//create XmlReader later, passing the pre-defined settings
var xmlReader = XmlReader.Create(fileInfo.FullName, settings);

//the rest of the codes remain untouched
var vr = new XmlValidatingReader(xmlReader);
vr.ValidationType = ValidationType.None;
vr.EntityHandling = EntityHandling.ExpandEntities;

xmlDocument.Load(vr);
har07
  • 88,338
  • 12
  • 84
  • 137
1

You should use XmlReader.Create(string, XmlReaderSettings) instead to create your reader instance.

From the MSDN reference:

Starting with the .NET Framework 2.0, we recommend that you use the System.Xml.XmlReader class instead.

The idea is to use the Create(...) factory method of the base class XmlReader instead of instantiating the derived class directly. Also see the factory method pattern for additional info.

The rest of your code remains unaffected since XmlValidatingReader takes an XmlReader in the constructor.

So you should end up with somehting like:

xmlDocument = new XmlDocument();

XmlReaderSettings settings = new XmlReaderSettings();
settings.MaxCharactersFromEntities = 0;
XmlReader reader = XmlReader.Create(fileInfo.FullName, settings);

var vr = new XmlValidatingReader(reader);
vr.ValidationType = ValidationType.None;
vr.EntityHandling = EntityHandling.ExpandEntities;

xmlDocument.Load(vr);
helb
  • 7,609
  • 8
  • 36
  • 58