Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to validate an XML with an XSD using LINQ
    text
    copied!<p>EDIT2: the problem seems to be my xsd. it validates pretty much every XML. i can't post the XSD on here though. why would every XML be valid for an XSD?</p> <p>EDIT: found a similar excample in the answer on <a href="https://stackoverflow.com/questions/6522319/how-to-validate-xml-against-xsd-in-c-windows-forms">here</a>. Same issue, it finds no errors no matter what xml and xsd i compare. even if i use a random diffrent xsd it keeps saying it is all fine.</p> <p>I found a lot of examples doing it without LINQ, but how would you do it with LINQ?</p> <p>I used Google to find an example but it seems to skip the validation most of the time validating every XML. (it once went into it, rejecting the file but I haven't been able to reproduce it.)</p> <p>Are there better ways of doing it or is there a reason why it would skip over the validation?</p> <pre><code>public String ValidateXml2(String xml, String xsd) { String Message = String.Empty; var ms = new MemoryStream(Encoding.Default.GetBytes(xml)); // Create the XML document to validate against. XDocument xDoc = XDocument.Load(ms, LoadOptions.PreserveWhitespace); XmlSchemaSet schema = new XmlSchemaSet(); bool isError = new bool(); // Defaults to false. int countError = 1; // Counts the number of errors have generated. Stream xsdMemoryStream = new MemoryStream(Encoding.Default.GetBytes(xsd)); // Add the schema file you want to validate against. schema.Add(XmlSchema.Read (xsdMemoryStream, new ValidationEventHandler((sender, args) =&gt; { Message = args.Exception.Message; }) )); // Call validate and use a LAMBDA Expression as extended method! // Don't you love .NET 3.5 and LINQ... xDoc.Validate(schema, (sender, e) =&gt; { switch (e.Severity) { case XmlSeverityType.Error: Console.WriteLine("Error {0} generated.", countError); break; case XmlSeverityType.Warning: Console.WriteLine("Warning {0} generated.", countError); break; } Console.WriteLine(sender.GetType().Name); Console.WriteLine("\r\n{0}\r\nType {1}\r\n", e.Message, e.Severity.ToString()); Console.WriteLine("-".PadRight(110, '-')); countError++; isError = true; // If error fires, flag it to handle once call is complete. } , true); // True tells the validate call to populate the post-schema-validation // which you will need later, if you want to dive a littel deeper... if (isError == true) // Error has been flagged. Lets see the errors generated. Console.WriteLine("You my friend have {0} error(s), now what?", countError); else Console.WriteLine("You rock! No errors..."); Console.Write("\r\n\r\nPress Enter to End"); Console.ReadKey(); return Message; } </code></pre> <p><a href="http://nitefrog.com/2008/06/xsd-schema-validation-using-linq-and-c/" rel="nofollow noreferrer">Credits and original example</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload