C#模式验证返回假成功
我一直在做这个项目,它应该允许用户指向一个XML文件,并根据XML模式验证该文件。 到目前为止没有什么“复杂”的 我用适当的XML测试了它——验证成功了。 向XML添加了一个节点-验证失败 一切看起来都很完美,直到我给它提供了一个完全不相关的xml文件。 我的临时文件夹中有一个,是很久以前从计划任务管理器中提取的。 ... 验证成功 我仍然无法理解“为什么”。 该模式有一个targetnamespace,它不在“随机”xml中。 …结构一点也不相似。 有人能解释一下验证过程实际上是如何进行的,以及为什么它在这种情况下成功了吗C#模式验证返回假成功,c#,xml,validation,C#,Xml,Validation,我一直在做这个项目,它应该允许用户指向一个XML文件,并根据XML模式验证该文件。 到目前为止没有什么“复杂”的 我用适当的XML测试了它——验证成功了。 向XML添加了一个节点-验证失败 一切看起来都很完美,直到我给它提供了一个完全不相关的xml文件。 我的临时文件夹中有一个,是很久以前从计划任务管理器中提取的。 ... 验证成功 我仍然无法理解“为什么”。 该模式有一个targetnamespace,它不在“随机”xml中。 …结构一点也不相似。 有人能解释一下验证过程实际上是如何进行的,以
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
namespace XMLtoXSDValidation
{
class clsXMLManage
{
string filepath = AppDomain.CurrentDomain.BaseDirectory;
public bool ValidateSchema(string path)
{
try
{
string schemaPath = Path.Combine(filepath, "XMLSchema1.xsd");
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("XMLtoXSDValidation", schemaPath);
settings.ValidationType = ValidationType.Schema;
XmlReader reader = XmlReader.Create(path, settings);
XmlDocument document = new XmlDocument();
document.Load(reader);
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
document.Validate(eventHandler);
return true;
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
return false;
}
}
static void ValidationEventHandler(object sender, ValidationEventArgs e)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
Console.WriteLine("Error: {0}", e.Message);
break;
case XmlSeverityType.Warning:
Console.WriteLine("Warning {0}", e.Message);
break;
}
}
}}
以下是模式:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="XMLtoXSDValidation" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstCategory">
<xs:complexType>
<xs:sequence>
<xs:element name="One">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="UserName"/>
<xs:element type="xs:string" name="Password"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Two">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="UserName"/>
<xs:element type="xs:string" name="Password"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SecondCategory">
<xs:complexType>
<xs:sequence>
<xs:element name="One">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="UserName"/>
<xs:element type="xs:string" name="Password"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Two">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="UserName"/>
<xs:element type="xs:string" name="Password"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="XMLtoXSDValidation">
<FirstCategory>
<One>
<UserName>a</UserName>
<Password></Password>
</One>
<Two>
<UserName>b</UserName>
<Password></Password>
</Two>
</FirstCategory>
<SecondCategory>
<One>
<UserName>a</UserName>
<Password></Password>
</One>
<Two>
<UserName>b</UserName>
<Password></Password>
</Two>
</SecondCategory>
</root>
真正对应于模式的XML:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="XMLtoXSDValidation" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstCategory">
<xs:complexType>
<xs:sequence>
<xs:element name="One">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="UserName"/>
<xs:element type="xs:string" name="Password"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Two">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="UserName"/>
<xs:element type="xs:string" name="Password"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SecondCategory">
<xs:complexType>
<xs:sequence>
<xs:element name="One">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="UserName"/>
<xs:element type="xs:string" name="Password"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Two">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="UserName"/>
<xs:element type="xs:string" name="Password"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="XMLtoXSDValidation">
<FirstCategory>
<One>
<UserName>a</UserName>
<Password></Password>
</One>
<Two>
<UserName>b</UserName>
<Password></Password>
</Two>
</FirstCategory>
<SecondCategory>
<One>
<UserName>a</UserName>
<Password></Password>
</One>
<Two>
<UserName>b</UserName>
<Password></Password>
</Two>
</SecondCategory>
</root>
A.
B
A.
B
以下是声称能够成功验证的XML:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Author>V-PC\V</Author>
</RegistrationInfo>
<Triggers>
<TimeTrigger>
<StartBoundary>2014-03-16T17:27:02</StartBoundary>
<Enabled>true</Enabled>
</TimeTrigger>
<LogonTrigger>
<Enabled>true</Enabled>
</LogonTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
<RestartOnFailure>
<Interval>PT5M</Interval>
<Count>3</Count>
</RestartOnFailure>
</Settings>
<Actions Context="Author">
<Exec>
</Exec>
</Actions>
</Task>
V-PC\V
2014-03-16T17:27:02
真的
真的
S-1-5-18
高可用性
停止存在
假的
假的
真的
真的
假的
假的
假的
真的
真的
假的
假的
假的
P3D
7.
PT5M
3.
关键在于这一部分:
该模式有一个targetnamespace,它不在“随机”XML中
本质上,您的模式与XML文件无关,因此验证器无法判断您的文件是否有效
如果将ReportValidationWarnings
添加到,则会收到一条警告:
警告:找不到匹配的架构。未进行任何验证。找不到元素“root”的架构信息
这确实有效。。。但这对我来说毫无意义。这不应该是一个警告。不应该警告您,XML根本就不接近模式。这应该是一个错误。几年后,我再次@BlueBunny,我们仍然发现没有验证失败,没有名称空间似乎绕过了验证过程。这很奇怪,但上面的建议至少提供了一种机制来确保应用程序意识到提交的XML不符合预期