xmlserializer 验证
我正在使用 XmlSerializer 反序列化 Xml 档案。但我发现生成的类 xsd.exe 仅提供读取 xml 的功能,但没有验证。例如,如果文档中缺少一个节点,则生成的类的属性字段将为 null,而不是像我预期的那样抛出验证异常。我怎样才能做到这一点?谢谢!
I'm using XmlSerializer to deserialize Xml achives. But I found the class xsd.exe generated only offers capability to read the xml, but no validation. For example, if one node is missing in a document, the attribute field of the generated class will be null, rather than throws a validation exception as I expected. How can I achieve that? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下代码应在反序列化时针对架构进行验证。类似的代码可用于在序列化时针对模式进行验证。
The following code should validate against a schema while deserializing. Similar code can be used to validate against a schema while serializing.
以下代码将以编程方式根据架构文件手动加载和验证 XML,从而允许您处理任何 导致错误和/或警告。
现在显然您希望 xsd.exe 生成的类在加载时自动执行此操作(上述方法需要对 XML 文件进行第二次处理),但预加载验证将允许您以编程方式检测格式错误的输入文件。
The following code will manually load and validate your XML against a schema file programmatically, allowing you to deal with any resulting errors and/or warnings.
Now obviously you desire to have the classes generated by xsd.exe to do this automatically and while loading (the above approach would require a second handling of the XML file), but a pre-load validate would allow you to programmatically detect a malformed input file.