简体   繁体   中英

C# validate XML file using XSD file

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 ?

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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM