XmlDocument.Validate 忽略不带 [XmlIgnore] 的属性
我有一个对象,它具有许多 xsd 文件中不存在的属性。在执行 XmlDocument.Validate 时,有没有一种方法可以告诉它忽略 xsd 中不存在的属性,而只是确保 xsd 所需的属性存在于 xml 文档中?
我可以通过在我的类中添加 [XmlIgnore] 属性来解决此问题,但我宁愿按照约定来完成此操作,而不是在整个对象模型中显式添加属性。
I have a object that has a number of properties that aren't present in the xsd file. When doing XmlDocument.Validate is there a way I can tell it to ignore properties that are not present in the xsd and instead just ensure that the properties required by xsd are present in the xml document?
I can get around this by adding [XmlIgnore] attributes all over my class but I would rather accomplish this by convention rather then explicitly add attributes all throughout my object model.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑有。就我个人而言,我会创建一个单独的 DTO,因为听起来您正在尝试让一个对象服务于两项工作。另一种选择是使用
XmlSerializer
构造函数,它允许您在运行时指定属性,但这比[XmlIgnore]
工作量要大得多。因此,如果您只想让它工作:
[XmlIgnore]
。如果您希望它是“纯粹的”,请创建第二个 DTO 模型并在它们之间进行转换。I doubt there is. Personally I would create a separate DTO, as it sounds like you're trying to make one object serve two jobs. Another option would be to use the
XmlSerializer
ctor that allows you to specify attribs at runtime, but this is much more work than[XmlIgnore]
.So if you just want it to work:
[XmlIgnore]
. If you want it to be "pure", create a second DTO model and translate between them.