C# 需要帮助根据C中的XSD验证XML吗#
我正在编写一个WPF应用程序,它需要验证XML文件。我使用以下类根据一个或多个XSD文件验证XML:C# 需要帮助根据C中的XSD验证XML吗#,c#,xml,xsd,C#,Xml,Xsd,我正在编写一个WPF应用程序,它需要验证XML文件。我使用以下类根据一个或多个XSD文件验证XML: public class XSDValidator { public List<XmlSchema> Schemas { get; set; } public List<String> Errors { get; set; } public List<String> Warnings { get; set; } public
public class XSDValidator
{
public List<XmlSchema> Schemas { get; set; }
public List<String> Errors { get; set; }
public List<String> Warnings { get; set; }
public XSDValidator()
{
Schemas = new List<XmlSchema>();
}
/// <summary>
/// Add a schema to be used during the validation of the XML document
/// </summary>
/// <param name="schemaFileLocation">The file path for the XSD schema file to be added for validation</param>
/// <returns>True if the schema file was successfully loaded, else false (if false, view Errors/Warnings for reason why)</returns>
public bool AddSchema(string schemaFileLocation)
{
// Reset the Error/Warning collections
Errors = new List<string>();
Warnings = new List<string>();
XmlSchema schema;
if (!File.Exists(schemaFileLocation))
{
throw new FileNotFoundException("The specified XML file does not exist", schemaFileLocation);
}
using (var fs = new FileStream(schemaFileLocation, FileMode.Open))
{
schema = XmlSchema.Read(fs, ValidationEventHandler);
}
var isValid = !Errors.Any() && !Warnings.Any();
if (isValid)
{
Schemas.Add(schema);
}
return isValid;
}
/// <summary>
/// Perform the XSD validation against the specified XML document
/// </summary>
/// <param name="xmlLocation">The full file path of the file to be validated</param>
/// <returns>True if the XML file conforms to the schemas, else false</returns>
public bool IsValid(string xmlLocation)
{
if (!File.Exists(xmlLocation))
{
throw new FileNotFoundException("The specified XML file does not exist", xmlLocation);
}
using (var xmlStream = new FileStream(xmlLocation, FileMode.Open))
{
return IsValid(xmlStream);
}
}
/// <summary>
/// Perform the XSD validation against the supplied XML stream
/// </summary>
/// <param name="xmlStream">The XML stream to be validated</param>
/// <returns>True is the XML stream conforms to the schemas, else false</returns>
private bool IsValid(Stream xmlStream)
{
// Reset the Error/Warning collections
Errors = new List<string>();
Warnings = new List<string>();
var settings = new XmlReaderSettings
{
ValidationType = ValidationType.Schema
};
settings.ValidationEventHandler += ValidationEventHandler;
foreach (var xmlSchema in Schemas)
{
settings.Schemas.Add(xmlSchema);
}
var xmlFile = XmlReader.Create(xmlStream, settings);
while (xmlFile.Read()) { }
return !Errors.Any() && !Warnings.Any();
}
private void ValidationEventHandler(object sender, ValidationEventArgs e)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
Errors.Add(e.Message);
break;
case XmlSeverityType.Warning:
Warnings.Add(e.Message);
break;
}
}
}
在测试XSD验证时,我使用了4个XML文件:其中一个文件books.XML
,对应于硬编码的模式books.XSD
。另外三个是我从其他来源提取的随机XML文件,我已经验证了它们对books.xsd
无效。但是,在运行代码时,ValidatedXMLFiles
显示的值是4而不是1
我已经从
XSDValidator
类中验证了我所能想到的一切;我尝试手动将随机字符串添加到Errors
,在这种情况下,IsValid
返回false。我认为有趣的一件事是,当我尝试将架构文件名更改为不存在的内容时,抛出了一个targetingException
,而不是我所期望的FileNotFoundException
。我不知道这是否意味着什么,但这是我见过的唯一奇怪的行为。有人能提供帮助吗?您假设验证引擎将自动知道如何使用books.xsd模式来验证所有xml文件
这是不对的。xml需要告诉验证器它应该根据哪个XSD进行验证
要表明这一点,请在xml文档中设置xmlns属性
例如:
<MyXml xmlns="http://MySchemaNamespace">
...
</MyXml>
...
以及模式:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://MySchemaNamespace"
xmlns="http://MySchemaNamespace"
elementFormDefault="qualified">
...
</xs:schema>
...
否则,XML“有效”的唯一标准是它的格式良好 嗨,琼斯,我很有兴趣调查这件事。是否有可能获得文件,以便我可以尝试在我的计算机上复制它。我显然希望确保我的代码正常工作。
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://MySchemaNamespace"
xmlns="http://MySchemaNamespace"
elementFormDefault="qualified">
...
</xs:schema>