简体   繁体   English

如何比较XML模式中的节点深度?

[英]How can I compare depth of nodes in an XML-schema?

I am working with a xml schema file that contains complex types with nested choice and sequence tags. 我正在使用一个XML模式文件,该文件包含带有嵌套选择和序列标签的复杂类型。 Those can be nested inside each other in any way. 它们可以以任何方式彼此嵌套。 What I need to do is to execute different methods depending on the first sequence or choice tag. 我需要做的是根据第一个序列或选择标记执行不同的方法。 I have no trouble finding a sequence or a choice tag via the .Descendants()-method. 我毫不费力地通过.Descendants()方法找到序列或选择标签。 But in this case I only get the tag I check for first. 但是在这种情况下,我只会得到我首先检查的标签。

I was thinking to determine the depths of the first sequence and choice tag after a given node and compare them. 我正在考虑确定给定节点之后的第一个序列和选择标签的深度,并进行比较。 This way I could determine the highest tag. 这样我可以确定最高标签。 How can I do this? 我怎样才能做到这一点? I can't find a way to determine the depth of a node. 我找不到确定节点深度的方法。 Or is there another way to figure out whether a node contains a sequence higher than a choice or the other way around? 还是有另一种方法可以弄清楚一个节点是否包含比选择更高的序列或相反的选择?

You could write an extension method that gives you the depth of an element: 您可以编写一个扩展方法,为您提供元素的深度:

public static class ExtensionMethods {
    public static int GetDepth(this XElement element) {
        if (element.Parent == null)
            return 0;
        return element.Parent.GetDepth() + 1;
    }
}

Using this, you can sort the selected elements by their depth and take the one you're searching for. 使用此功能,您可以按所选元素的深度对它们进行排序,然后选择要搜索的元素。

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

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