1

I have a XML file - its generated from parser here and I have a XSD file . The key is to validate the XML file (from specific path) using the XSD file (from specific path) and return flag if it's validated or not. Most of the code I saw was without using a XSD file to validate. Is there a possible way to validate XML file using XSD file?

1
  • Ok, how to do it without a targetNamespace? Commented Jul 3, 2015 at 21:51

1 Answer 1

3

some code:

XmlDocument doc = new XmlDocument() ;
doc.load(xmlFileName) ;
doc.Schemas.Add("",xsdFileName);
doc.Schemas.Compile();
TheSchemaErrors   = new List<string>() ;
TheSchemaWarnings = new List<string>() ;
doc.Validate(Xml_ValidationEventHandler);
if (TheSchemaErrors  .Count>0) { // display errors  }
if (TheSchemaWarnings.Count>0) { // display warnings  }
...
private List<string> TheSchemaErrors ;
private List<string> TheSchemaWarnings ;

private void Xml_ValidationEventHandler(object sender,ValidationEventArgs e)
{
  switch (e.Severity)
  {
    case XmlSeverityType.Error   : TheSchemaErrors  .Add(e.Message) ; break;
    case XmlSeverityType.Warning : TheSchemaWarnings.Add(e.Message) ; break;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is not a good answer as you just did a code dump without any explanation of what's happening. The least you can do is comment the code and mention relevant classes.
Thank you! That is what I was looking for couple hours
@Reticulated Spline: Your remark is generally true. But In this case, code is self-explanatory : I extracted some code of one of my applications and refactored it to be readable. If a clarification was required on any part of this code, a lot of people in the forum could respond.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.