简体   繁体   English

如何从.NET中的XmlSchemaObject按顺序获取可能的XML元素的平面列表

[英]How to get a flat list of possible XML Elements in order from a XmlSchemaObject in .NET

I have a XML Schema from a XmlSchemaSetProvider and need a list of all possible XML Elements (names) that may be in the given type in the correct order. 我有一个来自XmlSchemaSetProvider的XML Schema,并且需要以正确的顺序列出给定类型的所有可能的XML元素(名称)的列表。

The schema uses XML Schema extensions, complex types, sequences... 模式使用XML模式扩展,复杂类型,序列...

Is there some good way to accomplish this? 有一些好的方法可以做到这一点吗?

Cause: I have a custom XML serializer that depended on property order returned when reflecting the .NET type but that (documented as undefined) order behavior changed with MSBuild 14. Now i have to correct the order without putting around 1000 XmlElement.Order on all class properties. 原因:我有一个自定义XML序列化程序,该序列化程序依赖于反映.NET类型时返回的属性顺序,但是该顺序行为(记录为未定义)已随MSBuild 14更改。现在,我必须更正顺序,而不必在所有位置上放置1000 XmlElement.Order类属性。 So the idea is to detect/correct the order from XSD information and use it to fix the XML. 因此,其想法是从XSD信息中检测/更正顺序,并使用它来修复XML。

In the general case, what you are asking for is not possible. 在一般情况下,您所要求的是不可能的。 If the type can contain N different elements, then it is possible to design a schema in which some subset of the elements can appear in a different sequence depending on the context. 如果类型可以包含N个不同的元素,则可以设计一个架构,其中元素的某些子集可以根据上下文以不同的顺序出现。 So there is no such thing as a single defined sequence for all of the elements in the schema. 因此,对于架构中的所有元素都没有一个定义好的序列。 The particular schemas that you have in mind may not suffer from this problem (although you should assume the worst until you can prove that they do not). 您所想到的特定架构可能不会遭受此问题的影响(尽管您应该假设最坏的架构,直到可以证明它们没有)。 If you believe that a solution is possible for this restricted set of schemas then you will need to design a custom solution, and I would advise you to design it pretty conservatively. 如果您认为可以为这种受限的模式集提供解决方案,那么您将需要设计一个自定义解决方案,我建议您谨慎设计。 In particular, you should include assertions that check for scenarios that break your only-one-possible-sequence rule. 特别是,您应包括断言,以检查是否违反了唯一可能的规则。

Having said all of that, I would be interested to understand the business requirement behind your request. 综上所述,我很想了解您的请求背后的业务需求。 It might be possible to solve this by some other ( easier ) means. 可能可以通过其他(更简单)的方法解决此问题。

In the end I chose a different approach based on class layout instead of the XML Schema. 最后,我选择了一种基于类布局而不是XML Schema的方法。 I added a "[PropertyLineNumber]" on all properties with one search & replace and use it via reflection to get a reliable order. 我在所有属性上添加了一个[[PropertyLineNumber]”,并进行了一次搜索和替换,并通过反射使用它来获得可靠的订单。 The order goes by "inheritance chain then line number". 顺序按“继承链然后行号”进行。 Here the attribute (using C# 6) I used to make line number information available via reflection for our serializer. 在这里,该属性(使用C#6)用于使行号信息通过反射可用于序列化程序。

[AttributeUsage(AttributeTargets.Property)]
public sealed class PropertyLineNumberAttribute : Attribute
{
    public int LineNumber { get; }

    public PropertyLineNumberAttribute([CallerLineNumber] int lineNumber = 0)
    {
        LineNumber = lineNumber;
    }
}

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

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