什么是C#中的限制代码生成的XML模式? [英] What are the limits to code generation from XML Schema in C#?

查看:181
本文介绍了什么是C#中的限制代码生成的XML模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了关于与使用从XML架构生成类问题的几个问题 XSD.EXE ,与如何预处理模式(通常使用XSLT建议沿),以解决一些前代的棘手问题。我的问题是,是否有可能建立一个C#代码生成器,兼容100%的XML模式。与 XSD.EXE 的问题,仅仅是其实施的问题,还是他们指出,XML Schema和C#之间存在根本的矛盾?

I've seen several questions regarding problems with generating classes from XML Schema using xsd.exe, along with suggestions for how to pre-process the schema (often using XSLT) to resolve some of the trickier aspects prior to generation. My question is whether it's possible to construct a C# code generator that is 100% compliant with XML Schema. Are the problems with xsd.exe merely a question of its implementation, or do they point to a fundamental inconsistency between XML Schema and C#?

尤其后,我感兴趣的是如何在XML模式概念映射到C# - 什么是公认的映射,其中映射是值得商榷的,还有XML Schema构造天生联合国可映射并且是未充分利用的有C#构造? ?有,将用于映射提供的规则,这样,它可以被实现并测试了符合规范

In particular, I'm interested in how to map concepts in XML Schema to C# - what are the accepted mappings, which mappings are debatable, are there XML Schema constructs that are inherently un-mappable and are there C# constructs that are underutilised? Is there a compliance specification that would provide rules for mapping, such that it could be implemented and tested?

编辑:为了清楚起见,我完全意识到的XML 。架构不会为我提供完全实现C#的接口,我感兴趣的是它是否可以完全映射到一个C#类层次结构

For the sake of clarity I'm fully aware that XML Schema won't provide me with fully implemented C# interfaces, I'm interested in whether it can be fully mapped to a C# class hierarchy.

编辑2:我增加了一个小的奖金,因为我感兴趣的是得到更多的细节。

EDIT 2: I've added a small bounty, as I'm interested in getting a bit more detail.

编辑3:赏金仍然开放,但到目前为止,对stakx标题 - 一个很好的答案,但的主要的处理如何复制C#结构XML架构,而不是反过来。良好的输入虽然。

EDIT 3: Bounty still open, but so far heading toward stakx - a good answer but mainly dealing with how to replicate C# structures in XML Schema, rather than the other way round. Good input though.

推荐答案

有趣的问题。不久以前,我想知道同样的事情。

Interesting question. Not too long ago, I was wondering about exactly the same thing.

我会告诉我有多远了几个例子。我的演示将是不完整的(考虑到XML Schema规范是相当全面的),但它应该足以显示...

I will show a couple examples of how far I got. My demonstration will not be complete (considering that the XML Schema specification is fairly comprehensive), but it should suffice to show...


  • 您的可以的也优于 XSD.EXE (如果你愿意遵守一定的模式,当你写你的XML架构);和

  • XML Schema允许键入不能在C#中表示声明。这不应该作为一个大惊喜,考虑到XML和C#是非常不同的语言具有完全不同的目的。

  • that you can do better than xsd.exe (if you're willing to adhere to certain patterns when you write your XML Schema); and
  • that XML Schema allows type declarations that cannot be expressed in C#. This should not come as a big surprise, considering that XML and C# are very different languages with quite different purposes.

C#接口,可与复杂类型的XML Schema定义。例如:

C# interfaces can be defined in XML Schema with complex types. For example:

<xsd:complexType name="IFoo" abstract="true">
  <xsd:attribute name="Bar" type="xsd:string" use="required" />
  <xsd:attribute name="Baz" type="xsd:int" use="optional" />
</xsd:complexType>



对应相当好:

corresponds fairly well to:

interface IFoo
{
    string Bar { get; set; }
    int?   Baz { get; set; }
}



这里的模式是抽象的,名为(非匿名)复杂类型。基本的XML Schema相当于在C#中的接口

The pattern here is that abstract and named (non-anonymous) complex types are basically the XML Schema equivalent of interfaces in C#.

请注意一些问题的映射:

Note some problems with the mapping:


  • C#访问修饰符,如公共内部不能在XML模式呈现等。

您有没有表达一个C#领域和XML模式的属性之间的区别的方式。

You have no way of expressing the difference between a C# field and a property in XML Schema.

您不能在XML模式定义方法。

You cannot define methods in XML Schema.

您还没有表达一个C#的区别方式结构。 (有根本的XML模式,它大致对应于.NET值类型类型,但他们更比复杂类型的XML模式的限制。)

You also have no way of expressing the difference between a C# struct and class. (There's simply types in XML Schema, which roughly correspond to .NET value types; but they're much more restricted in XML Schema than complex types.)

用法用量=可选的可用于映射可空类型。在XML Schema中,你可以定义一个字符串属性为可选。跨越到C#,翻译了一些损失发生:由于字符串是引用类型,它不能被声明为可为空(因为它已经被默认为空的)

The usage of usage="optional" can be used to map nullable types. In XML Schema, you could define a string attribute as optional. Crossing over to C#, some loss in translation occurs: Since string is a reference type, it cannot be declared as nullable (since it's already nullable by default).

XML模式也允许 =使用禁止。这又是什么,也不会在C#来表示,或者至少在一个不错的方式(据我所知)。

XML Schema also allows usage="prohibited". This is again something that cannot be expressed in C#, or at least in a nice fashion (AFAIK).

从我的实验,似乎 XSD.EXE 将的从不的生成抽象复杂类型C#接口;它会留在抽象类上课吧。 (我猜,这是为了保持翻译的逻辑相当简单的。)

From my experiments, it appears that xsd.exe will never generate C# interfaces from abstract complex types; it will stay with abstract classes instead. (I'm guessing that this is to keep the translation logic reasonably simple.)

抽象类可以非常相似做接口:

Abstract classes can be done very similarly to interfaces:

<xsd:element name="FooBase" abstract="true">
  <xsd:complexType>
    ...
  </xsd:complexType>
</xsd:element>



在这里,你定义的抽象元素属性设置为真正,并嵌入在里面一个匿名复杂类型。

Here, you define an element with the abstract attribute set to true, and embed an anonymous complex type inside it.

这对应于以下类型声明在C#中:

This corresponds to the following type declaration in C#:

abstract class FooBase { ... }



声明类



同上,但省略了抽象=真正的

<xsd:complexType name="IFoo" abstract="true">
  ...
</xsd:complexType>

<xsd:element name="Foo" type="IFoo" />

这映射到:

interface IFoo { ... }

class Foo : IFoo { ... }

也就是说,您同时定义了一个名为的,抽象的复杂类型(接口),并与该类型命名元素。

That is, you define both a named, abstract complex type (the interface), and a named element with that type.


  • 请注意,上面的C#代码段包含 ... 的两倍,而XML模式片段只有一个 ... 。怎么会呢?

  • Note that the C# code snippet above contains ... twice, while the XML Schema snippet has only one .... How come?

由于无法定义的方法(代码),因为你也不能指定访问修饰符,你并不需要落实的复杂类型的元素XML架构。该复杂类型的执行就等同于原来的声明。如果复杂类型定义了一些属性,这些只是被映射到一个C#接口实现自动属性。

Because you cannot define methods (code), and because you also cannot specify access modifiers, you don't need to "implement" a complex type with the element in XML Schema. The "implementation" of the complex type would be identical to the original declaration. If the complex type defines some attributes, these simply get mapped to auto-properties in a C# interface implementation.

类和接口继承的XML模式,可以通过扩展类型和元素替换组的组合定义b $ b

Class and interface inheritance in XML Schema can be defined through a combination of type extensions and element substitution groups:

<xsd:element name="Base" type="base" />
<xsd:element name="Derived" substitutionGroup="Base" type="derived" />
                       <!-- ^^^^^^^^^^^^^^^^^^^^^^^^ -->

<xsd:complexType name="base">
  <xsd:attribute name="Foo" type="xsd:boolean" use="required" />
</xsd:complexType>

<xsd:complexType name="derived">
  <xsd:complexContent>
    <xsd:extension base="base">  <!-- !!! -->
      <xsd:attribute name="Bar" type="xsd:string" use="required" />
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>

这映射到:

class Base
{
    bool Foo { get; set; }
}

class Derived : Base
{
    string Bar { get; set; }
}

请注意:


  • 我们会再次使用命名的复杂类型。但是这一次,他们没有定义的抽象=真正的,因为我们没有定义任何C#接口类型。

  • We're again using named complex types. But this time, they're not defined abstract="true", since we're not defining any C# interface type.

请注意引用:元素派生基本的取代基团;与此同时,复合型导出是复杂类型的延伸。 导出的类型导出基本已键入

Note the references: Element Derived is in Base's substitution group; at the same time, complex type derived is an extension of complex type base. Derived has type derived, Base has type base.

命名复杂类型不是抽象的在C#中没有直接对应的。他们不是类,因为它们不能被实例化(以XML,的元素的,不是的类型的,有大致相同的功能,在F#或对象实例化值构造在C#) ;也不是他们真正的接口,因为它们不声明为抽象的。

Named complex types that are not abstract have no direct counterpart in C#. They're not classes, since they cannot be instantiated (in XML, elements, not types, have roughly the same function as value constructors in F# or object instantiation in C#); neither are they truly interfaces, since they are not declared abstract.


  • 显示怎么一会宣布,在XML Schema中,一个C#类类型实现的若干个的。接口

显示在XML架构映射多么复杂的内容到C#(我的第一个猜它有在C#中没有对应可言,至少不是在一般情况)。

Showing how complex content in XML Schema maps to C# (my first guess it that there's no correspondence in C# at all; at least not in the general case).

枚举秒。 (他们是在XML模式由通过枚举,顺便说一句限制简单类型实现的。)

enums. (They are realised in XML Schema by restricting a simple type via enumeration, btw.)

<一类code>常量字段(这将可能映射到一个固定值的属性)。

const fields in a class (these would possibly map to attributes with a fixed value).

如何映射的xsd:选择的xsd:序列到C#;如何正确映射的IEnumerable< T> 的ICollection< T> 的IList< T> 的IDictionary< TKEY的,TValue> XML模式

How to map xsd:choice, xsd:sequence to C#; How to correctly map IEnumerable<T>, ICollection<T>, IList<T>, IDictionary<TKey, TValue> to XML Schema?

XML模式简单类型,这听起来像他们的.NET值类型对应的概念;但更受限制,并有不同的目的。

XML Schema simple types, which sound like they're the corresponding concept of .NET value types; but are far more restricted and have a different purpose.

有就是我还没有表现出很多很多的事情,但此时,你也许可以看到后面我的例子的基本模式。

There's many many more things that I haven't shown, but by now you can probably see the basic patterns behind my examples.

要正确地做到这一切,一会要系统地去通过XML Schema规范,看看各概念提到有映射的的为C#。 (有可能是没有单一的最好的解决办法,但有几个备选方案)。但我明确的意思,只显示了几个有趣的例子。我希望这是仍有足够的信息!

To do all this correctly, one would have to systematically go through the XML Schema specification and see how each concept mentioned there maps best to C#. (There's perhaps no single best solution, but several alternatives.) But I explicitly meant to show only a couple of interesting examples. I hope that was still informative enough!

这篇关于什么是C#中的限制代码生成的XML模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆