ValidationEventHandler

ValidationEventHandler

本文介绍了使用C#通过XSD架构验证XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在验证无法访问互联网的服务器上的XML文件和 C#中的validadation不起作用。我验证的模式 的命名空间如下:xmlns:ds =" http://www.w3.org/2000/09/xmldsig#" xmlns:xenc =" http://www.w3.org/2001/04/xmlenc#" ;. 我在磁盘上本地都有这些模式,但确实如此不行。如果 我尝试使用可上网的电脑,那么它的工作正常。 有人知道该怎么做。我验证的功能看起来像 这个: private bool ValidXml(字符串XmlFile,字符串XSDLoc,字符串tns) { //。 。 。 string targetNamespaceURI = tns; //在这里放置目标命名空间URI 。 // XmlValidatingReader vr = new XmlValidatingReader(new XmlTextReader(new StringReader( doc))); XmlValidatingReader vr = new XmlValidatingReader(new XmlTextReader(XmlFile)); vr.XmlResolver = new XmlUrlResolver() ; MessageBox.Show(vr.NamespaceURI); vr.Schemas.Add(targetNamespaceURI,XSDLoc); vr.ValidationEventHandler + = new ValidationEventHandler (ValidationHandler); vr.ValidationType = ValidationType.Schema; _ValidationErrorsCount = 0; _ValidationErrorsMsgs ="" ;; while(vr.Read()); vr.Close(); if(_ValidationErrorsCount> ; 0) 返回false; else 返回true; } 感谢您的帮助...I am validating XML files on a server which has no internet access andthe validadation in C# does not work. Schema with which I am validatinghas namespaces like: xmlns:ds="http://www.w3.org/2000/09/xmldsig#"xmlns:xenc="http://www.w3.org/2001/04/xmlenc#".I have these schemas all locally on the disk, but it does not work. IfI try on a computer with internet access then it works just fine. Doessomeone know what to do. Function with which I validate looks likethis:private bool ValidXml(string XmlFile, string XSDLoc, string tns){// . . .string targetNamespaceURI = tns; // put the target namespace URIof your schema here.//XmlValidatingReader vr = new XmlValidatingReader( newXmlTextReader( new StringReader( doc ) ) );XmlValidatingReader vr = new XmlValidatingReader( newXmlTextReader(XmlFile) );vr.XmlResolver = new XmlUrlResolver();MessageBox.Show(vr.NamespaceURI);vr.Schemas.Add( targetNamespaceURI, XSDLoc);vr.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);vr.ValidationType = ValidationType.Schema;_ValidationErrorsCount = 0;_ValidationErrorsMsgs = "";while(vr.Read());vr.Close();if (_ValidationErrorsCount > 0)return false;elsereturn true;}Thanks for any help ...推荐答案 代码失败时XSDLoc的价值究竟是什么? - Martin Honnen --- MVP XML http:/ /JavaScript.FAQTs.com/ 你是否会抛出一个中止.NET代码的.NET异常?或者 您的验证事件处理程序报告验证错误消息? - Martin Honnen - - MVP XML http://JavaScript.FAQTs.com/ 这篇关于使用C#通过XSD架构验证XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 19:22