我有一个没有DOCTYPE声明的XML文件,我想在阅读时使用外部DTD进行验证.
Dim x_set As Xml.XmlReaderSettings = New Xml.XmlReaderSettings() x_set.XmlResolver = Nothing x_set.CheckCharacters = False x_set.ProhibitDtd = False x = XmlTextReader.Create(sChemin, x_set)
如何设置外部DTD的路径?你如何验证?
我以前成功使用过以下功能,应该很容易适应.然而,这依赖于创建一个XmlDocument作为magnifico提到.这可以通过以下方式实现:
XmlDocument doc = new XmlDocument(); doc.Load( filename ); doc.InsertBefore( doc.CreateDocumentType( "doc_type_name", null, DtdFilePath, null ), doc.DocumentElement ); ////// Class to test a document against DTD /// /// XML The document to validate private static bool ValidateDoc( XmlDocument doc ) { bool isXmlValid = true; StringBuilder xmlValMsg = new StringBuilder(); StringWriter sw = new StringWriter(); doc.Save( sw ); doc.Save( TestFilename ); XmlReaderSettings settings = new XmlReaderSettings(); settings.ProhibitDtd = false; settings.ValidationType = ValidationType.DTD; settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler( delegate( object sender, ValidationEventArgs args ) { isXmlValid = false; xmlValMsg.AppendLine( args.Message ); } ); XmlReader validator = XmlReader.Create( new StringReader( sw.ToString() ), settings ); while( validator.Read() ) { } validator.Close(); string message = xmlValMsg.ToString(); return isXmlValid; }