【发布时间】:2009-11-10 03:14:28
【问题描述】:
我正在使用 XmlSerializer 反序列化 Xml 档案。但我发现生成的类 xsd.exe 仅提供读取 xml 的功能,但没有验证。例如,如果文档中缺少一个节点,则生成的类的属性字段将为空,而不是像我预期的那样抛出验证异常。我怎样才能做到这一点?谢谢!
【问题讨论】:
标签: c# xml-serialization xsd xsd.exe
我正在使用 XmlSerializer 反序列化 Xml 档案。但我发现生成的类 xsd.exe 仅提供读取 xml 的功能,但没有验证。例如,如果文档中缺少一个节点,则生成的类的属性字段将为空,而不是像我预期的那样抛出验证异常。我怎样才能做到这一点?谢谢!
【问题讨论】:
标签: c# xml-serialization xsd xsd.exe
以下代码应在反序列化时针对架构进行验证。类似的代码可用于在序列化时针对模式进行验证。
private static Response DeserializeAndValidate(string tempFileName)
{
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(LoadSchema());
Exception firstException = null;
var settings = new XmlReaderSettings
{
Schemas = schemas,
ValidationType = ValidationType.Schema,
ValidationFlags =
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ReportValidationWarnings
};
settings.ValidationEventHandler +=
delegate(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
{
Console.WriteLine(args.Message);
}
else
{
if (firstException == null)
{
firstException = args.Exception;
}
Console.WriteLine(args.Exception.ToString());
}
};
Response result;
using (var input = new StreamReader(tempFileName))
{
using (XmlReader reader = XmlReader.Create(input, settings))
{
XmlSerializer ser = new XmlSerializer(typeof (Response));
result = (Response) ser.Deserialize(reader);
}
}
if (firstException != null)
{
throw firstException;
}
return result;
}
【讨论】:
以下代码将以编程方式手动加载并根据模式文件验证您的 XML,允许您处理任何resulting errors and/or warnings。
//Read in the schema document
using (XmlReader schemaReader = XmlReader.Create("schema.xsd"))
{
XmlSchemaSet schemaSet = new XmlSchemaSet();
//add the schema to the schema set
schemaSet.Add(XmlSchema.Read(schemaReader,
new ValidationEventHandler(
delegate(Object sender, ValidationEventArgs e)
{
}
)));
//Load and validate against the programmatic schema set
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Schemas = schemaSet;
xmlDocument.Load("something.xml");
xmlDocument.Validate(new ValidationEventHandler(
delegate(Object sender, ValidationEventArgs e)
{
//Report or respond to the error/warning
}
));
}
现在显然您希望 xsd.exe 生成的类在加载时自动执行此操作(上述方法需要对 XML 文件进行第二次处理),但预加载验证将允许您以编程方式检测格式错误的输入文件。
【讨论】: