I want to read a fairly large xml file. Its small enough to fit in memory, but still very big. When reading the XML it is validated against an XSD. This, however, does not prevent business errors from happening when using the read data for further manipulation in the system. When such business errors occur (after XSD validation) I want to be able to describe the line number and column number for the start and end position of an element from my xml. Also, in this context, it would be user friendly to show the input xml as it was read from the file.
Using the xsd.exe I’ve code generated all the data classes and I read the xml using
using (var reader = new StringReader(content))
{
var errors = new List<string>();
var settings = new XmlReaderSettings();
settings.Schemas.Add("urn:import-schema", "Import.xsd");
settings.ValidationEventHandler += (o, args) => errors.Add(args.Message);
settings.ValidationType = ValidationType.Schema;
using (XmlReader xr = XmlReader.Create(reader, settings))
{
var xs = new XmlSerializer(typeof(ImportRoot));
var result = (ImportRoot) xs.Deserialize(xr);
if (errors.Any())
throw new Exception(string.Join("\n\n", errors));
return result;
}
}
}
However, I can’t seem to find the meta-info that I’m looking for. I’ve checked the XDocument class as well. Here elements seems to have a Value property that is a string. But that is still not all the information I want to display.
Line number information is not read from a
StringReader. If you use aStreamReaderon aFileStream, you’ll be able to get the line number.This additional metadata you’re looking for is called the “Post Schema Validation Infoset”.