使用XSD验证XmlDocument
发布时间:2020-12-16 23:04:39 所属栏目:百科 来源:网络整理
导读:我正在开发一个系统,它将通过webservice接收 XML(XmlDocument).我不会在硬盘上有这个XML(XmlDocument).它将在内存上进行管理. 我有一个文件XSD来验证我从WebService收到的XML(XmlDocument).我试图做一个例子来验证这个Xml. 我的XML: ?xml version="1.0"?not
我正在开发一个系统,它将通过webservice接收
XML(XmlDocument).我不会在硬盘上有这个XML(XmlDocument).它将在内存上进行管理.
我有一个文件XSD来验证我从WebService收到的XML(XmlDocument).我试图做一个例子来验证这个Xml. 我的XML: <?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> 我也有我的XSD: <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> 正如我们所看到的,我将身体字段设为int,只是为了模拟错误. 好吧,为了尝试获取错误,我有以下代码: //event handler to manage the errors private static void verifyErrors(object sender,ValidationEventArgs args) { if (args.Severity == XmlSeverityType.Warning) MessageBox.Show(args.Message); } 点击按钮,我有: private void button1_Click(object sender,EventArgs e) { try { // my XmlDocument (in this case I will load from hardisk) XmlDocument xml = new XmlDocument(); // load the XSD schema.. is this right? xml.Schemas.Add("http://www.w3schools.com","meuEsquema.xsd"); // Load my XML from hardisk xml.Load("meusDados.xml"); // event handler to manage the errors from XmlDocument object ValidationEventHandler veh = new ValidationEventHandler(verificaErros); // try to validate my XML.. and the event handler verifyError will show the error xml.Validate(veh); } catch { // do nothing.. just to test } } 问题是我将body字段更改为int,但是该字段中有一个字符串值,我没有收到错误. 解决方法
问题是XML命名空间.
在您的XSD中,您将targetNamespace =和xmlns =定义为“http://www.w3schools.com”: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> 但是 – 您的XML文档不包含任何XML名称空间. <?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> 基本上,XSD根本没有验证这个XML. 您需要从XSD中删除这些命名空间: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> 或者,将XSD中定义的默认XML命名空间(没有前缀)添加到XML: <?xml version="1.0"?> <note xmlns="http://www.w3schools.com"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> 如果您的XSD中有XML命名空间,那么它们也必须存在于XML中 – 反之亦然. 一旦你做了一个或另一个解决方案,你应该得到一个验证错误,如: Validation error: The 'body' element is invalid - The value 'Don't forget me this weekend!' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:int' - The string 'Don't forget me this weekend!' is not a valid Int32 value. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |