Dan*_*Dan 4 xml validation xmldocument
我正在开发一个系统,它将通过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>
Run Code Online (Sandbox Code Playgroud)
我也有我的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>
Run Code Online (Sandbox Code Playgroud)
正如我们所看到的,我将身体字段设为int,只是为了模拟错误.
好吧,为了尝试获取错误,我有以下代码:
//event handler to manage the errors
private static void verifyErrors(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
MessageBox.Show(args.Message);
}
Run Code Online (Sandbox Code Playgroud)
点击按钮,我有:
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
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我将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">
Run Code Online (Sandbox Code Playgroud)
但是-你的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>
Run Code Online (Sandbox Code Playgroud)
所以基本上,这XSD是没有验证这个XML 在所有.
您需要从XSD中删除这些命名空间:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
Run Code Online (Sandbox Code Playgroud)
或者,将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>
Run Code Online (Sandbox Code Playgroud)
如果您的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.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10339 次 |
最近记录: |