The most efficient way to validate XML on XSD

I get a string variable with XML in it and have an XSD file. I have to check the XML in the string on the XSD file and find out that there are several ways (XmlDocument, XmlReader, ...?).

After validation, I just have to store the XML, so I do not need it in XDocument or XmlDocument.

How to go if I need the fastest performance?

+7
c # xml xsd xml-validation
source share
4 answers

Others have already mentioned the XmlReader class for performing validation, and I will not dwell on this in detail.

Your question does not indicate much context. Will you repeat this check multiple xml documents or only once? I am reading a script in which you simply check a lot of XML documents (from a third-party system?) And save them for future use.

My contribution to search performance will be to use a compiled XmlSchemaSet , which will be thread safe, so multiple threads can reuse it without having to re-analyze the xsd document.

 var xmlSchema = XmlSchema.Read(stream, null); var xmlSchemaSet = new XmlSchemaSet(); xmlSchemaSet.Add(xmlSchema); xmlSchemaSet.Compile(); CachedSchemas.Add(name, xmlSchemaSet); 
+9
source share

I would go to XmlReader with XmlReaderSettings, because there is no need to load the full XML into memory. This will be more efficient for large XML files.

+3
source share

I think the fastest way is to use XmlReader, which checks the document while it is reading. This allows you to check the document in one go: http://msdn.microsoft.com/en-us/library/hdf992b8.aspx

+2
source share

Use an XmlReader configured to perform validation, with the source being TextReader .

You can manually specify the XSD that XmlReader will use if you do not want to rely on the ads in the input document ( XmlReaderSettings.Schemas )

Start (just accepts the declarations of the XSD instance in the input document):

 var settings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Document, ValidationType = ValidationType.Schema, ValidationFlags = XmlSchemaValidationFlags.ProcessSchemaLocation | XmlSchemaValidationFlags.ProcessInlineSchema, }; int warnings = 0; int errors = 0; settings.ValidationEventHandler += (obj, ea) => { if (args.Severity == XmlSeverityType.Warning) { ++warnings; } else { ++errors; } }; XmlReader xvr = XmlReader.Create(new StringReader(inputDocInString), settings); try { while (xvr.Read()) { // do nothing } if (0 != errors) { Console.WriteLine("\nFailed to load XML, {0} error(s) and {1} warning(s).", errors, warnings); } else if (0 != warnings) { Console.WriteLine("\nLoaded XML with {0} warning(s).", warnings); } else { System.Console.WriteLine("Loaded XML OK"); } Console.WriteLine("\nSchemas loaded durring validation:"); ListSchemas(xvr.Schemas, 1); } catch (System.Xml.Schema.XmlSchemaException e) { System.Console.Error.WriteLine("Failed to read XML: {0}", e.Message); } catch (System.Xml.XmlException e) { System.Console.Error.WriteLine("XML Error: {0}", e.Message); } catch (System.IO.IOException e) { System.Console.Error.WriteLine("IO error: {0}", e.Message); } 
0
source share

All Articles