How to validate XML file against HR-XML using C#
I'm looking for some advice and guidance for validating HR-XML.
I've written come code to produce an XML file that "should" be correctly formatted as HR-XML but I want to validate it using code before writing it to disk.
Below is a code sample of my validation method and the validation error event handler
/// <summary>
/// Validate the populated XML
/// </summary>
/// <remarks>
/// The schema folder needs to be "HR-XML-3_0" which contains the "org_hr-xml" and "org_openap开发者_StackOverflowplications_platform" folders
/// </remarks>
/// <param name="schemaPath">The root path for the HR-XML XSD files for the xml to be validated against</param>
/// <returns>true if the xml is valid, else false</returns>
public bool Validate(string schemaPath)
{
try
{
// Initalise the valid flag
this.m_FormatValid = false;
this.m_ValidationErrors.Clear();
// Check if there is anything to output
if (this.m_Root.HasElements == true)
{
// Validate that the root node has been populated correctly
XDocument doc = new XDocument(m_Root);
XmlSchemaSet schemas = new XmlSchemaSet();
// Add the schemas in the specified folder to the schema set
schemas.Add(XmlSchema.Read(new StreamReader(Path.Combine(schemaPath, @"org_hr-xml\3_0\Developer\BODs\RespondHRMasterData.xsd")), null));
schemas.Add(XmlSchema.Read(new StreamReader(Path.Combine(schemaPath, @"org_openapplications_platform\1_1\Common\OAGi\Components\Meta.xsd")), null));
// Set the valid flag to true prior to validation and let the event handler set it to false if it's not valid
this.m_FormatValid = true;
doc.Validate(schemas, HRXML_Validation_Error);
}
else
{
Log.WriteLine(Category.Info, "No HR-XML data to validate");
}
}
catch (Exception ex)
{
this.m_FormatValid = false;
Log.WriteLine(Category.Warning, "An error was detected whilst validating HR-XML data", ex);
}
return this.m_FormatValid;
}
/// <summary>
/// Event handler for XML validation errors
/// </summary>
void HRXML_Validation_Error(Object source, ValidationEventArgs args)
{
// There is no need to worry about the severity of the validation as they should always be errors
// The warning appears only to be triggered is no schema has been specified which shouyldn't be the case here
// Output the message to the validation list
this.m_ValidationErrors.Add( args.Message );
//Set the Valid flag to false
m_FormatValid = false;
}
I added the BOD for responding to HRMasrterData requests into the schema set but this generated an exception because of the imported schema referenced in the RespondHRMasterData.xsd file. The error was Undefined complexType 'http://www.openapplications.org/oagis/9:BusinessObjectDocumentType' is used as a base for complex type extension.
Adding the second file to the schema set resolved the first exception and gave this one. Type 'http://www.openapplications.org/oagis/9:NormalizedStringType' is not declared, or is not a simple type.
What I don't what to do is add all the HR-XML schema files (unless I really have to) before I get to and "Actual" errors in the created file.
Am I on the right track or is there a better way of doing this?
Thanks.
The best way I found to validate the HR-XML was to add the node xsi:schemaLocation to the generated xml which points to the required BOD.
It's also possible to create an XML Spy project which has all the schema files referenced. If this is done, the schema location is not required.
精彩评论