I am running into a situation where my IIS App Pool actually crashes when I try to get the outer XML of an XML input. Here is the code that I inherited :
var tempNamespace = xmlToValidate.OwnerDocument.CreateAttribute("xmlns");
tempNamespace.Value = xsdFileName;
xmlToValidate.Attributes.Append(tempNamespace);
//Have to create fragment. Otherwise XmlNodeReader does not work correctly with validation.
var fragment = xmlToValidate.OwnerDocument.CreateDocumentFragment();
fragment.InnerXml = xmlToValidate.OuterXml;
// Set the validation settings.
var readerSettings = new XmlReaderSettings();
readerSettings.ValidationType = ValidationType.Schema;
readerSettings.Schemas = schemaSet;
readerSettings.ValidationEventHandler += new ValidationEventHandler(this.XsdValidationEvent);
// Create the XmlReader object and validate the provided XML blob.
var nodeReader = new XmlNodeReader(fragment);
var validatingReader = XmlReader.Create(nodeReader, readerSettings);
while (validatingReader.Read());
I have been able to determine that the crash is happening on the "get" of the "xmlToValidate.OuterXml" property whenever there is some invalid XML input with a ton of really deep, invalid, nested nodes. I need XSD to validate this without crashing because thsi is a high security app so I really do need to use XSD here. I tried to skip this part about copying from the XmlNode to the XmlFragment as referred to in the comments, but if I just do the validation on the xmlToValidate node itself then it never finds any XSD errors.