Rabu, 14 November 2007

Validating XML with DTD or XSD

public void ValidateXMLagainstDTD(Stream thestream)


{


XmlTextReader reader = new XmlTextReader(thestream);


XmlValidatingReader validator = new XmlValidatingReader(reader);


validator.ValidationType = ValidationType.DTD;


validator.ValidationEventHandler += new ValidationEventHandler(DTDValidationEventHandler);


while (validator.Read())


{


//add something logical here


}


validator.Close();


if (Isvalid)


Console.WriteLine("Document is valid");


else


Console.WriteLine("Document is Invalid");


Console.ReadLine();


}


public void DTDValidationEventHandler(object sender,


ValidationEventArgs args)


{


this.Isvalid = false;


Console.WriteLine("Validation event\n" + args.Message);


}

Tidak ada komentar: