C# 如何使用C根据HR-XML验证XML文件#
我正在寻找一些关于验证HR-XML的建议和指导 我编写了come代码来生成一个XML文件,该文件“应”正确格式化为HR-XML,但我想在将其写入磁盘之前使用代码对其进行验证 下面是我的验证方法和验证错误事件处理程序的代码示例C# 如何使用C根据HR-XML验证XML文件#,c#,xml,validation,C#,Xml,Validation,我正在寻找一些关于验证HR-XML的建议和指导 我编写了come代码来生成一个XML文件,该文件“应”正确格式化为HR-XML,但我想在将其写入磁盘之前使用代码对其进行验证 下面是我的验证方法和验证错误事件处理程序的代码示例 /// <summary> /// Validate the populated XML /// </summary> /// <remarks> /// The schema folder needs to be "HR-XML-3_0
/// <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_openapplications_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;
}
//
///验证填充的XML
///
///
///模式文件夹需要是“HR-XML-3\u 0”,其中包含“org\u HR-XML”和“org\u openapplications\u platform”文件夹
///
///要验证的XML的HR-XML XSD文件的根路径
///如果xml有效,则为true,否则为false
公共bool验证(字符串模式)
{
尝试
{
//初始化有效标志
this.m_FormatValid=false;
此.m_ValidationErrors.Clear();
//检查是否有任何输出
if(this.m_Root.HasElements==true)
{
//验证根节点是否已正确填充
XDocument doc=新XDocument(m_Root);
XmlSchemaSet schemase=新的XmlSchemaSet();
//将指定文件夹中的架构添加到架构集
schemas.Add(XmlSchema.Read(新的StreamReader(Path.Combine(schemaPath,@“org_hr-xml\3_0\Developer\BODs\RespondHRMasterData.xsd”)),null);
Add(XmlSchema.Read(新的StreamReader(Path.Combine(schemaPath,@“org\u openapplications\u platform\1\u 1\Common\OAGi\Components\Meta.xsd”)),null);
//在验证之前,将valid标志设置为true,如果无效,则让事件处理程序将其设置为false
this.m_FormatValid=true;
文档验证(模式、HRXML验证错误);
}
其他的
{
Log.WriteLine(Category.Info,“无需验证的HR-XML数据”);
}
}
捕获(例外情况除外)
{
this.m_FormatValid=false;
Log.WriteLine(Category.Warning,“验证HR-XML数据时检测到错误”,例如);
}
返回此.m_格式有效;
}
///
///XML验证错误的事件处理程序
///
无效HRXML_验证_错误(对象源,ValidationEventArgs参数)
{
//不必担心验证的严重性,因为它们应该总是错误
//如果未指定任何架构,则该警告似乎只会被触发,但此处不应如此
//将消息输出到验证列表
这个.m_ValidationErrors.Add(args.Message);
//将有效标志设置为false
m_FormatValid=false;
}
我将用于响应HRMasrterData请求的BOD添加到模式集中,但这产生了一个异常,因为在RespondHRMasterData.xsd文件中引用了导入的模式。错误是
未定义的complexType'http://www.openapplications.org/oagis/9:BusinessObjectDocumentType'用作复杂类型扩展的基础。
将第二个文件添加到架构集中解决了第一个异常,并给出了这个异常。
类型'http://www.openapplications.org/oagis/9:NormalizedStringType'未声明,或不是简单类型。
我不知道要做什么,就是在创建的文件中出现“实际”错误之前添加所有HR-XML模式文件(除非我真的必须这样做)
我是走对了路还是有更好的方法
谢谢。我发现验证HR-XML的最佳方法是将节点xsi:schemaLocation添加到生成的XML中,该XML指向所需的BOD
还可以创建一个XMLSpy项目,其中引用了所有模式文件。如果这样做,则不需要模式位置。我创建的xml将在使用项目的xml-SPY中进行验证。如果我将xsi:schemaLocation属性添加到根节点,并将路径添加到RespondHRMasterData.xsd,它也会正确验证。