简体   繁体   English

针对整个架构验证单个 XML 节点

[英]Validating single XML node against entire schema

I am developing a serializer which should be compliant with an already existent, big and well defined schema.我正在开发一个序列化程序,它应该符合一个已经存在的、大的、定义明确的模式。 In order to unit test my serialization and attributes (which define how things are serialized) I want to use unit testing.为了对我的序列化和属性(定义事物的序列化方式)进行单元测试,我想使用单元测试。

The problem is that I want to unit test element by element.问题是我想逐个元素地进行单元测试。

For example: This Schema has an element called ide , this element has another elements within it.例如:这个 Schema 有一个名为ide的元素,这个元素里面有另一个元素。 On my project I have an object which represents this ide element.在我的项目中,我有一个代表这个ide元素的对象。

In my test method I create an instance of this object and generate a xml on which this ide element is the root.在我的测试方法中,我创建了这个对象的一个​​实例并生成了一个 xml,这个ide元素是根。 I want to be able to validate this single xml node using the entire xsd .我希望能够使用整个xsd验证这个单个 xml 节点。

How would I go about doing this?我该怎么做呢?

The only thing that came to mind and seems too much trouble is to modify the schema on the fly, so it has only the elements that interest me on the given moment.唯一想到并且似乎很麻烦的是动态修改模式,因此它只有在给定时刻我感兴趣的元素。 I tested and verified that this would work, but would be bad for maintenance.我测试并验证了这可以工作,但不利于维护。

What is the correct way to achieve this?实现这一目标的正确方法是什么?

I was finally able to answer the question.我终于可以回答这个问题了。 You have to use XElement.Validate你必须使用XElement.Validate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM