XSD,泛型和C#类 [英] XSD, Generics and C# Classes

查看:92
本文介绍了XSD,泛型和C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have following simple example:

  <xs:element name="Search" type="SearchObject"/>

  <xs:complexType name="SearchObject">
    <xs:choice>
      <xs:element name="Simple" type="SimpleSearch"/>
      <xs:element name="Extended" type="ExtendedSearch"/>
    </xs:choice>
  </xs:complexType>

  <xs:complexType name="SimpleSearch">
    <xs:sequence>
      <xs:element name="FirstName" type="xs:string"/>
      <xs:element name="LastName" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ExtendedSearch">
    <xs:sequence>
      <xs:element name="FirstName" type="xs:string"/>
      <xs:element name="LastName" type="xs:string"/>
      <xs:element name="Age" type="xs:int"/>
      <xs:element name="Address" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

I use Visual Studio Shell like this:

xsd XMLSchema.xsd /c

Basically /c stands for generating C# classes out of XMLSchema.xsd.

然后这些类看起来像这样:

The classes then look something like this one:

[System.Xml.Serialization.XmlRootAttribute("Search", Namespace="http://tempuri.org/XMLSchema.xsd", IsNullable=false)]
public partial class SearchObject {

    private object itemField;

    [System.Xml.Serialization.XmlElementAttribute("Extended", typeof(ExtendedSearch))]
    [System.Xml.Serialization.XmlElementAttribute("Simple", typeof(SimpleSearch))]
    public object Item {
        get {
            return this.itemField;
        }
        set {
            this.itemField = value;
        }
    }
}

My first question is why is the property "Item" not called "Search" as I have set inside xsd file on that element?

My second question is why is property Item of type object? I have set a choice inside my xsd file and I would like the c# code to look more like this:

My second question is why is property Item of type object? I have set a choice inside my xsd file and I would like the c# code to look more like this:

public partial class SearchObject<T> where T : SimpleSearch, where T : ExtendedSearch
{
    public T Search
    {
       get ...
       set ...
    }
}

I would like to have somehow an generic class that allows only the types which I have specified inside the choice block in xsd file which are in my case SimpleSearch and ExtendedSearch.

Is that even possible and if yes how do I get it right?

Is that even possible and if yes how do I get it right?

推荐答案

While I'm not sure about the first, the second is easy to answer: Your items or search objects are simply two classes. XSD does not introduce a common base class for these, thus the only common base is Object.
While I'm not sure about the first, the second is easy to answer: Your items or search objects are simply two classes. XSD does not introduce a common base class for these, thus the only common base is Object.


这篇关于XSD,泛型和C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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