XML reading with correct error handling (line numbers, original text, etc.)
source link: https://www.codesd.com/item/xml-reading-with-correct-error-handling-line-numbers-original-text-etc.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
XML reading with correct error handling (line numbers, original text, etc.)
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 a StreamReader
on a FileStream
, you'll be able to get the line number.
This additional metadata you're looking for is called the "Post Schema Validation Infoset".
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK