如何使用多个xml-schema验证xml文档

时间:2010-04-28 02:57:57

标签: c# xml xsd validation

xmlns:m =“http://www.MangoDSP.com/mav/wsdl”as localfile:“ma.wsdl” xmlns:m0 =“http://www.MangoDSP.com/schema”作为localfile:“MaTypes.xsd”

我如何验证它。

1 个答案:

答案 0 :(得分:1)

我有一段时间没有这样做(多年,真的),我再次从我的Linux笔记本电脑上发帖,所以请原谅模糊。

private bool isValid;
private ArrayList exceptionList;

public bool Validate()
{
    isValid = true;
    exceptionList = new ArrayList();

    XmlTextReader reader;
    XmlSchema schema;
    XmlSchemaCollection schemas = new XmlSchemaCollection();

    reader = new XmlTextReader( "schema file 1" );
    schema = XmlSchema.Read( reader, new ValidationEventHandler( ValidationError ) );
    schemas.Add( schema );

    reader = new XmlTextReader( "schema file 2" );
    schema = XmlSchema.Read( reader, new ValidationEventHandler( ValidationError ) );
    schemas.Add( schema );

    reader = new XmlTextReader( "validate this file" );

    XmlValidatingReader validatingReader;

    validatingReader = new XmlValidatingReader( reader );
    validatingReader.ValidationEventHandler += new ValidationEventHandler( ValidationError );
    validatingReader.Schemas.Add( schemas );

    isValid = true;
    exceptionList = new ArrayList();

    while ( validatingReader.Read() );

    return isValid;
}

private void ValidationError( object sender, ValidationEventArgs e )
{
    isValid = false;
    exceptionList.Add( e.Exception );
}

您还需要进行一些错误处理和资源清理。