这是我第一次尝试使用 XSD 验证 XML。
要验证的 XML 文件:
<?xml version="1.0" encoding="utf-8" ?>
<config xmlns="Schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="config.xsd">
<levelVariant>
<filePath>SampleVariant</filePath>
</levelVariant>
<levelVariant>
<filePath>LegendaryMode</filePath>
</levelVariant>
<levelVariant>
<filePath>AmazingMode</filePath>
</levelVariant>
</config>
XSD,位于与要验证的 XML 文件相关的“Schemas/config.xsd”中:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="config">
<xs:complexType>
<xs:sequence>
<xs:element name="levelVariant">
<xs:complexType>
<xs:sequence>
<xs:element name="filePath" type="xs:anyURI">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
现在,我只想准确地验证当前显示的 XML 文件。一旦我更好地理解了这一点,我就会扩展更多。对于像当前存在的 XML 文件这样简单的东西,我真的需要这么多行吗?
C#中的验证码:
public void SetURI(string uri)
{
XElement toValidate = XElement.Load(Path.Combine(PATH_TO_DATA_DIR, uri) + ".xml");
// begin confusion
// exception here
string schemaURI = toValidate.Attributes("xmlns").First().ToString()
+ toValidate.Attributes("xsi:noNamespaceSchemaLocation").First().ToString();
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(null, schemaURI);
XDocument toValidateDoc = new XDocument(toValidate);
toValidateDoc.Validate(schemas, null);
// end confusion
root = toValidate;
}
运行上面的代码给出了这个异常:
The ':' character, hexadecimal value 0x3A, cannot be included in a name.
任何照明将不胜感激。
最佳答案
而不是使用 XDocument.Validate扩展方法,我会使用 XmlReader可以配置为通过 XmlReaderSettings 处理内联模式.您可以执行类似以下代码的操作。
public void VerifyXmlFile(string path)
{
// configure the xmlreader validation to use inline schema.
XmlReaderSettings config = new XmlReaderSettings();
config.ValidationType = ValidationType.Schema;
config.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
config.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
config.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
config.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
// Get the XmlReader object with the configured settings.
XmlReader reader = XmlReader.Create(path, config);
// Parsing the file will cause the validation to occur.
while (reader.Read()) ;
}
private void ValidationCallBack(object sender, ValidationEventArgs vea)
{
if (vea.Severity == XmlSeverityType.Warning)
Console.WriteLine(
"\tWarning: Matching schema not found. No validation occurred. {0}",
vea.Message);
else
Console.WriteLine("\tValidation error: {0}", vea.Message);
}
上面的代码假设有以下 using 语句。
using System.Xml;
using System.Xml.Schema;
为了简单起见,我没有返回 boolean
或验证错误的集合,您可以轻松修改它来实现。
注意:我修改了您的 config.xml 和 config.xsd 以验证它们。这些是我所做的更改。
配置.xsd:
<xs:element maxOccurs="unbounded" name="levelVariant">
配置文件:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="config.xsd">
关于c# - 开始使用 .NET 进行 XSD 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2513024/