【发布时间】:2017-05-30 21:01:43
【问题描述】:
我正在尝试使用其架构读取 xml 文件。
我的 xml 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<PersonList
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="D:\MySchema.xsd">
现在我正在读取这样的 xml 文件:
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
using (XmlReader reader = XmlReader.Create("MyXmlFile.xml", settings))
{
while(reader.Read())
//.....
}
private static void ValidationCallBack(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
Console.WriteLine("\tWarning: Matching schema not found. No validation occurred." + args.Message);
else
Console.WriteLine("\tValidation error: " + args.Message);
}
问题是当没有找到模式'D:\MySchema.xsd'时,它仍在读取xml,所以noNamespaceSchemaLocation没用......
所以我在我的代码中设置了 Schema 路径,如下所示:
settings.Schemas.Add(null, "D:\\MySchema.xsd");
现在它正在使用模式读取 xml 文件,但我在这里设置的是模式路径硬编码... 我想从 xml 文件中获取模式路径 (noNamespaceSchemaLocation),并根据 xml 文件中的模式将模式添加到设置中。通过这样做,我还可以检查 Schema 是否存在。
明确一点:如何从 xml 文件中获取 noNamespaceSchemaLocation?
【问题讨论】: