EDN Admin
Well-known member
Hi every body,
While validating my .xml with an .xsd, i added these 2 methods to access the xml node that contains the error :
public void ValidateXml(string xmlFilePath, string xsdFilePath)
{
if (string.IsNullOrEmpty(xmlFilePath)) { throw new ArgumentNullException("xmlFilePath"); }
if (string.IsNullOrEmpty(xsdFilePath)) { throw new ArgumentNullException("xsdFilePath"); }
if (!File.Exists(xmlFilePath))
{
throw new ArgumentException(string.Format("File [{0}] not found.", xmlFilePath));
}
if (!File.Exists(xsdFilePath))
{
throw new ArgumentException(string.Format("File [{0}] not found.", xsdFilePath));
}
var schemas = new XmlSchemaSet();
// Use the target namespace specified in the XSD file.
schemas.Add(null, xsdFilePath);
var readerSettings = new XmlReaderSettings();
// Validate the xml file to an XSD file not an DTD or XDR.
readerSettings.ValidationType = ValidationType.Schema;
// Include schemas referenced by the given xsd file (recursively) in the validation proces.
readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
// Warnings will fire the ValidationEventHandler function.
readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
readerSettings.Schemas.Add(schemas);
// The function [ValidationEventHandler] will be used to handle all validation errors / warnings.
readerSettings.ValidationEventHandler += ValidationEventHandler;
using (var xmlReader = XmlReader.Create(xmlFilePath, readerSettings))
{
while (xmlReader.Read()) { } // Validate XML file.
}
}
public void ValidationEventHandler(object sender, ValidationEventArgs args)
{
//MessageBox.Show(args.Severity.ToString() + " *** " + args.Exception.ToString() + " $$$ " + args.Exception.LineNumber.ToString() + " ### " + args.Exception.LinePosition.ToString());
XmlSchemaValidationException ex = (XmlSchemaValidationException) args.Exception;
XmlNode node = (XmlNode)ex.SourceObject;
}
My problem is that : ex.SourceObject is always NULL.
Any help will be much appreciated.
Thank you.
Nacer CREPUQ BI Developer
View the full article
While validating my .xml with an .xsd, i added these 2 methods to access the xml node that contains the error :
public void ValidateXml(string xmlFilePath, string xsdFilePath)
{
if (string.IsNullOrEmpty(xmlFilePath)) { throw new ArgumentNullException("xmlFilePath"); }
if (string.IsNullOrEmpty(xsdFilePath)) { throw new ArgumentNullException("xsdFilePath"); }
if (!File.Exists(xmlFilePath))
{
throw new ArgumentException(string.Format("File [{0}] not found.", xmlFilePath));
}
if (!File.Exists(xsdFilePath))
{
throw new ArgumentException(string.Format("File [{0}] not found.", xsdFilePath));
}
var schemas = new XmlSchemaSet();
// Use the target namespace specified in the XSD file.
schemas.Add(null, xsdFilePath);
var readerSettings = new XmlReaderSettings();
// Validate the xml file to an XSD file not an DTD or XDR.
readerSettings.ValidationType = ValidationType.Schema;
// Include schemas referenced by the given xsd file (recursively) in the validation proces.
readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
// Warnings will fire the ValidationEventHandler function.
readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
readerSettings.Schemas.Add(schemas);
// The function [ValidationEventHandler] will be used to handle all validation errors / warnings.
readerSettings.ValidationEventHandler += ValidationEventHandler;
using (var xmlReader = XmlReader.Create(xmlFilePath, readerSettings))
{
while (xmlReader.Read()) { } // Validate XML file.
}
}
public void ValidationEventHandler(object sender, ValidationEventArgs args)
{
//MessageBox.Show(args.Severity.ToString() + " *** " + args.Exception.ToString() + " $$$ " + args.Exception.LineNumber.ToString() + " ### " + args.Exception.LinePosition.ToString());
XmlSchemaValidationException ex = (XmlSchemaValidationException) args.Exception;
XmlNode node = (XmlNode)ex.SourceObject;
}
My problem is that : ex.SourceObject is always NULL.
Any help will be much appreciated.
Thank you.
Nacer CREPUQ BI Developer
View the full article