【发布时间】:2016-05-18 11:11:50
【问题描述】:
我一直在从事这个项目,该项目应该允许用户指向一个 XML 文件,并且该文件将根据 XML 模式进行验证。 到目前为止,没有什么“复杂”的。
我根据正确的 XML 对其进行了测试 - 验证成功。 向 XML 添加了一个节点 - 验证失败。
一切看起来都很完美,直到我给它提供了一个完全不相关的 xml 文件。 我在我的临时文件夹中折叠的一个,很久以前从计划的任务管理器中提取的。 ...并且验证成功。
我仍然无法理解“为什么”。 该模式有一个目标命名空间,它不存在于“随机”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:
<?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:
<?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>
【问题讨论】:
标签: c# xml validation