开发者

How to valiadate a xml document with Multiple xml-schemas

开发者 https://www.devze.com 2022-12-28 04:50 出处:网络
xmlns:m=\"http://www.MangoDSP.com/mav/wsdl\" as localfile:\"ma.wsdl\" xmlns:m0=\"http://w开发者_开发知识库ww.MangoDSP.com/schema\"as localfile:\"MaTypes.xsd\"

xmlns:m="http://www.MangoDSP.com/mav/wsdl" as localfile:"ma.wsdl" xmlns:m0="http://w开发者_开发知识库ww.MangoDSP.com/schema" as localfile:"MaTypes.xsd"

how can i validate it.


I haven't done this in a while (years, really) and once again I'm posting from my Linux laptop, so please forgive the fuzziness.

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 );
}

You'll also want to do some error handling and resource cleanup.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号