On a day to day we might face a scenario where we end up having an XML validated against XSD.
I too faced the same scenario where some of our Tier 2 and BA's wants to test some of their files form customers are valid or not. we have a system which validates and rejects the files which don't match with a proper XSD, but Tier 2 wants to do it before hand some times instead of flowing it through the system.
Below is the method which we can use with in our Windows or web application where we can have user input XML and XSD and have them validated against and show all the errors if they don't match.
I too faced the same scenario where some of our Tier 2 and BA's wants to test some of their files form customers are valid or not. we have a system which validates and rejects the files which don't match with a proper XSD, but Tier 2 wants to do it before hand some times instead of flowing it through the system.
Below is the method which we can use with in our Windows or web application where we can have user input XML and XSD and have them validated against and show all the errors if they don't match.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
namespace XmltoXpathnamespace
{
public class XmltoXpath
{
private List _listOfErrorMessages = null;
public string _errormessagesconcatinated = string.Empty;
public bool IsXMLValidAgainstXSD(string xml_To_Validate, string xsd_To_Validate_Against)
{
bool validXML = true;
try
{
_listOfErrorMessages = new List();
XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();
xmlSchemaSet.Add(string.Empty, XmlReader.Create(new StringReader(xsd_To_Validate_Against)));
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.IgnoreComments = true;
xmlReaderSettings.IgnoreWhitespace = true;
xmlReaderSettings.ValidationType = ValidationType.Schema;
xmlReaderSettings.Schemas.Add(xmlSchemaSet);
xmlReaderSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
ValidationEventHandler validationEventHandler = (sender, args) =>
{
validXML = false;
ErrMsg errMsg = new ErrMsg();
errMsg.parentName = ((XmlReader)sender).Name.ToString();
errMsg.entityIDCode = ((XmlReader)sender).GetAttribute("EntityIDCode") == null ? string.Empty : ((XmlReader)sender).GetAttribute("EntityIDCode").ToString();
PropertyInfo propInfo = args.Exception.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "Args");
errMsg.attributeName = propInfo.GetValue(args.Exception, null) == null ? string.Empty : ((string[])propInfo.GetValue(args.Exception, null))[0];
errMsg.errorMessage = args.Exception.Message.Replace("'", string.Empty);
errMsg.lineNumber = args.Exception.LineNumber.ToString();
_listOfErrorMessages.Add(errMsg);
_errormessagesconcatinated += string.Format("ErrorMessage: {0} Line number: {1} \r\n", errMsg.errorMessage, errMsg.lineNumber);
};
xmlReaderSettings.ValidationEventHandler += validationEventHandler;
using (XmlReader xmlReader = XmlReader.Create(new StringReader(xml_To_Validate), xmlReaderSettings))
{
while (xmlReader.Read()) ;
}
xmlReaderSettings.ValidationEventHandler -= validationEventHandler;
}
catch (Exception ex)
{
throw new Exception(string.Format("ERROR: Exception caught in IsXMLValidAgainstSingleXSD: {0}", ex.Message));
}
return validXML;
}
private class ErrMsg
{
public string parentName { get; set; }
public string attributeName { get; set; }
public string errorMessage { get; set; }
public string entityIDCode { get; set; }
public string lineNumber { get; set; }
}
}
}
No comments:
Post a Comment