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 Answer
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;
}
}
3 Comments
Reticulated Spline
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.
kodi1911
Thank you! That is what I was looking for couple hours
Graffito
@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.