Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use the XDocument.Validate method to validate an XML document against a given schema and obtain a list of errors. Here's an example:

XDocument doc = XDocument.Load("example.xml");
XmlSchemaSet schema = new XmlSchemaSet();
schema.Add(null, "schema.xsd"); // replace "schema.xsd" with your schema file name

List<string> errors = new List<string>();
doc.Validate(schema, (sender, e) =>
{
    errors.Add(e.Message);
});

// print the list of errors
foreach (string error in errors)
{
    Console.WriteLine(error);
}

In this example, the XDocument.Load method reads an XML file into an XDocument object. The XmlSchemaSet object contains the schema against which the XML document is validated. The Validate method validates the document and adds any validation errors to the errors list. Finally, the list of errors is printed to the console.