任何方式来覆盖的方式元素由 xsd.exe 绑定

发布于 2024-08-28 20:14:54 字数 1920 浏览 2 评论 0原文

我的架构中有以下元素:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="optimizeModelBase">
    <xs:attribute name="name" type="xs:string"/>
  </xs:complexType>

  <xs:complexType name="riskModel">
    <xs:complexContent>
      <xs:extension base="optimizeModelBase">
        <xs:attribute name="type" type="xs:string" use="required"/>        
      </xs:extension>      
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="fullCovariance">
    <xs:complexContent>
      <xs:extension base="optimizeModelBase">
        <xs:attribute name="fromDate" type="xs:date" use="required"/>
        <xs:attribute name="toDate" type="xs:date" use="required"/>
        <xs:attribute name="windowSize" type="xs:int" use="required"/>
      </xs:extension>
    </xs:complexContent>    
  </xs:complexType>

在我的主架构主体中,我使用一个元素来指定 1-of 情况:

<xs:choice id="RiskModelParameter">
  <xs:element name="RiskModel" type="riskModel"/>
  <xs:element name="FullCovariance" type="fullCovariance"/>
</xs:choice>

当我运行 xsd.exe 时,生成的代码是:

    [System.Xml.Serialization.XmlElementAttribute("FullCovariance",
    typeof(fullCovariance))]
    [System.Xml.Serialization.XmlElementAttribute("RiskModel", 
    typeof(riskModel))]
    public optimizeModelBase Item 
    {
        get 
        {
           return this.itemField;
        } 
        set 
        {
            this.itemField = value;
        }
    }

问题是元素的 ID 标记被忽略,并且 xsd.exe 任意命名属性“Item”。我必须承认,这不是一个大问题,但它开始让我烦恼。让这个额外烦人的是,如果我在同一级别有其他元素,xsd.exe 会将它们绑定为“Item1”、“Item2”等。

有谁知道是否可以拥有 xsd .exe 将我的选择元素命名为“Item”,并且能够放入我自己的属性名称?

I have the following elements in my schema:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="optimizeModelBase">
    <xs:attribute name="name" type="xs:string"/>
  </xs:complexType>

  <xs:complexType name="riskModel">
    <xs:complexContent>
      <xs:extension base="optimizeModelBase">
        <xs:attribute name="type" type="xs:string" use="required"/>        
      </xs:extension>      
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="fullCovariance">
    <xs:complexContent>
      <xs:extension base="optimizeModelBase">
        <xs:attribute name="fromDate" type="xs:date" use="required"/>
        <xs:attribute name="toDate" type="xs:date" use="required"/>
        <xs:attribute name="windowSize" type="xs:int" use="required"/>
      </xs:extension>
    </xs:complexContent>    
  </xs:complexType>

In my main schema body, I use a element to specify a 1-of situation:

<xs:choice id="RiskModelParameter">
  <xs:element name="RiskModel" type="riskModel"/>
  <xs:element name="FullCovariance" type="fullCovariance"/>
</xs:choice>

When I run xsd.exe, the resulting code is:

    [System.Xml.Serialization.XmlElementAttribute("FullCovariance",
    typeof(fullCovariance))]
    [System.Xml.Serialization.XmlElementAttribute("RiskModel", 
    typeof(riskModel))]
    public optimizeModelBase Item 
    {
        get 
        {
           return this.itemField;
        } 
        set 
        {
            this.itemField = value;
        }
    }

The issue is that the element's ID tag is being ignored, and xsd.exe is arbitrarily naming the property "Item". I have to admit, it's not a big issue, but it's starting to annoy me. What makes this extra annoying is that if I have additional elements at the same level, xsd.exe binds them as "Item1", "Item2", etc.

Does anyone know if it's possible to not have xsd.exe name my choice elements as "Item", and instead be able to put in my own property names?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忘东忘西忘不掉你 2024-09-04 20:14:54

我到处搜索,但似乎没有解决我的问题。根据链接:

http://msdn.microsoft。 com/en-us/library/sa6z5baz(VS.80).aspx
似乎选择元素的任意命名是不可覆盖的。希望这些信息对其他人有帮助......!

I've searched high and low, but it seems like there is no solution for my problem. According to the link:

http://msdn.microsoft.com/en-us/library/sa6z5baz(VS.80).aspx
It seems like the arbitrary naming of the choice element is not overrideable. Hopefully this information is helpful to others out there...!

夜深人未静 2024-09-04 20:14:54

今天刚刚遇到这个问题。

我确实有一个解决方法,因此您可以使用组而不是选择来规避此问题。

使用上面的 xsd 作为基础:

重写:

<xs:choice id="RiskModelParameter">
  <xs:element name="RiskModel" type="riskModel"/>
  <xs:element name="FullCovariance" type="fullCovariance"/>
</xs:choice>

到:

<xs:group name="RiskModelGroup">
    <xs:sequence>
        <xs:element name="RiskModel" type="riskModel"/>
        <xs:element name="FullCovariance" type="fullCovariance"/>
    </xs:sequence>
</xs:group>

引用元素中的组:

<xs:element name="Foo">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="SomeFieldId" type="xs:int" />
      <xs:group ref="RiskModelGroup" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
    </xs:complexType>
</xs:element>

运行 xsd.exe,结果如下所示

public partial class Foo {

    private int someFieldIdField;

    private riskModel riskModelField;

    private fullCovariance fullCovarianceField;

    /// <remarks/>
    public int SomeFieldId {
        get {
            return this.someFieldIdField;
        }
        set {
            this.someFieldIdField = value;
        }
    }

    /// <remarks/>
    public riskModel RiskModel {
        get {
            return this.riskModelField;
        }
        set {
            this.riskModelField = value;
        }
    }

    /// <remarks/>
    public fullCovariance FullCovariance {
        get {
            return this.fullCovarianceField;
        }
        set {
            this.fullCovarianceField = value;
        }
    }
}

现在您有属性名称 RiskModel 和 FullCovariance

Foo f = new Foo()
f.RiskModel.name

f.FullCovariance.fromDate

如果您需要 RiskModels 和 FullCovariance 对象的多个项目,您可以在序列中添加一个具有 RiskModelGroup 的新元素。

祝你好运!

Just had this exact problem today.

I do have a workaround so you can circumvent this problem using a group instead of choice.

using the above xsd as the basis:

Rewrite:

<xs:choice id="RiskModelParameter">
  <xs:element name="RiskModel" type="riskModel"/>
  <xs:element name="FullCovariance" type="fullCovariance"/>
</xs:choice>

To:

<xs:group name="RiskModelGroup">
    <xs:sequence>
        <xs:element name="RiskModel" type="riskModel"/>
        <xs:element name="FullCovariance" type="fullCovariance"/>
    </xs:sequence>
</xs:group>

Reference the group in your element:

<xs:element name="Foo">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="SomeFieldId" type="xs:int" />
      <xs:group ref="RiskModelGroup" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
    </xs:complexType>
</xs:element>

Run xsd.exe and the result is as follows

public partial class Foo {

    private int someFieldIdField;

    private riskModel riskModelField;

    private fullCovariance fullCovarianceField;

    /// <remarks/>
    public int SomeFieldId {
        get {
            return this.someFieldIdField;
        }
        set {
            this.someFieldIdField = value;
        }
    }

    /// <remarks/>
    public riskModel RiskModel {
        get {
            return this.riskModelField;
        }
        set {
            this.riskModelField = value;
        }
    }

    /// <remarks/>
    public fullCovariance FullCovariance {
        get {
            return this.fullCovarianceField;
        }
        set {
            this.fullCovarianceField = value;
        }
    }
}

Now you have property names RiskModel and FullCovariance

Foo f = new Foo()
f.RiskModel.name

or

f.FullCovariance.fromDate

If you require multiple Items of RiskModels and FullCovariance objects you could add a new element having the RiskModelGroup within a sequence.

Good luck!

我最亲爱的 2024-09-04 20:14:54

对于那些仍然想知道的人:只需使用 XSDObjectGenerator(由 Microsoft 制造)。它通过为类中的每个选项添加一个对象来管理 XsdChoice。这样,您就不必寻找用于 Item 属性的正确元素。
例如:

<xs:complexType name="AccountSchemeName1Choice">
    <xs:sequence>
        <xs:choice>
            <xs:element name="Cd" type="ExternalAccountIdentification1Code"/>
            <xs:element name="Prtry" type="Max35Text"/>
        </xs:choice>
    </xs:sequence>
</xs:complexType>

成为

[XmlType(TypeName = "AccountSchemeName1Choice", Namespace = Declarations.SchemaVersion), Serializable]
public class AccountSchemeName1Choice : INotifyPropertyChanged
{

    [XmlElement(ElementName = "Cd", IsNullable = false, Form = XmlSchemaForm.Qualified, DataType = "string", Namespace = Declarations.SchemaVersion)]
    public string __Cd;

    [XmlIgnore]
    public string Cd
    {
        get { return __Cd; }
        set { __Cd = value; RaisePropertyChanged("Cd"); }
    }

    [XmlElement(ElementName = "Prtry", IsNullable = false, Form = XmlSchemaForm.Qualified, DataType = "string", Namespace = Declarations.SchemaVersion)]
    public string __Prtry;

    [XmlIgnore]
    public string Prtry
    {
        get { return __Prtry; }
        set { __Prtry = value; RaisePropertyChanged("Prtry"); }
    }

    public AccountSchemeName1Choice()
    {
    }

    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

For those who still want to know : Just use XSDObjectGenerator (made by Microsoft). It manage XsdChoice by adding one object for each choice to your class. That way, you don't have to look for the right element to use for the Item property.
For example:

<xs:complexType name="AccountSchemeName1Choice">
    <xs:sequence>
        <xs:choice>
            <xs:element name="Cd" type="ExternalAccountIdentification1Code"/>
            <xs:element name="Prtry" type="Max35Text"/>
        </xs:choice>
    </xs:sequence>
</xs:complexType>

became

[XmlType(TypeName = "AccountSchemeName1Choice", Namespace = Declarations.SchemaVersion), Serializable]
public class AccountSchemeName1Choice : INotifyPropertyChanged
{

    [XmlElement(ElementName = "Cd", IsNullable = false, Form = XmlSchemaForm.Qualified, DataType = "string", Namespace = Declarations.SchemaVersion)]
    public string __Cd;

    [XmlIgnore]
    public string Cd
    {
        get { return __Cd; }
        set { __Cd = value; RaisePropertyChanged("Cd"); }
    }

    [XmlElement(ElementName = "Prtry", IsNullable = false, Form = XmlSchemaForm.Qualified, DataType = "string", Namespace = Declarations.SchemaVersion)]
    public string __Prtry;

    [XmlIgnore]
    public string Prtry
    {
        get { return __Prtry; }
        set { __Prtry = value; RaisePropertyChanged("Prtry"); }
    }

    public AccountSchemeName1Choice()
    {
    }

    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
半暖夏伤 2024-09-04 20:14:54

我曾经实际解析过一个自动生成的 xsd.exe 文件并使用 NRefactory 对其进行了更改。

I once actually parsed an autogenerated xsd.exe file and changed it using NRefactory.

空名 2024-09-04 20:14:54

我自己在处理 XSD.exe 时就遇到了这个限制。就 XSD.exe 如何解释所有元素类型的 ID 属性而言,这似乎是相当统一的做法。我很想听听 MS 开发团队中的某个人对 XSD.exe 以这种方式工作的原因的合理解释。

有趣的是,我一直在开发一个 SchemaImporterExtension,除其他外,它会利用选择元素的 ID 属性来精确实现您所描述的内容,这是一种自定义选择映射对象成员的字段/属性名称的方法。

不幸的是,XSD.exe 不仅似乎不支持 ID 属性绑定,而且甚至没有显示 ID 包含在反映解析架构文档中的选择元素的 XmlSchemaChoice 对象中。

也许我遗漏了一些东西,但如果这确实是预期的行为而不是我的错误,那么根据我的估计,这是一个非常荒谬的遗漏,并且它说明了 MS 中反映了多少中性的 XSD 表示形式模式自动生成工具。

XSD.exe 遵循我所说的 XML 架构标准的“精益且平均”的解释。显然现在 WCF 消除了 XSD.exe,你猜怎么着? WCF 的 svcutil 工具可以识别甚至更小的 XML 架构足迹; svcutil IIRC 甚至不支持选择元素绑定。

在某些时候,我什至不确定 MS 自动生成工具将带来什么价值,因为您将不得不使用琐碎的 XSD 结构(我的意思是,没有选择元素支持?真的吗?)封装您的业务对象建模。

I myself just encountered this limitation in my dealings with XSD.exe. It seems that this is a fairly uniform practice with respect to how XSD.exe interprets the ID attribute for all element types. I'd love to hear a rationalization from someone on the MS development team for why XSD.exe works in this manner.

What's interesting is that I have been working on a SchemaImporterExtension that would, among other things, leverage the ID attribute of choice elements to achieve precisely what you're describing, a means of customizing the field/property names of choice mapped object members.

Unfortunately, not only does it seem that XSD.exe doesn't support ID attribute binding, but it doesn't even appear that ID is included in the XmlSchemaChoice object that reflects the choice element from the parsed schema document.

Perhaps I'm missing something, but if this is indeed the intended behavior and not an error on my part, then it's a pretty ridiculous omission in my estimation, and it speaks to just how much of a neutered XSD representation is reflected in the MS schema auto-generation tools.

XSD.exe adheres to what I'll term as a "lean and mean" interpretation of the XML schema standard. Apparently now WCF obviates XSD.exe, and guess what? WCF's svcutil tool recognizes an even smaller footprint of XML schema; choice element binding is even't supported in svcutil IIRC.

At some point I'm not even sure what value the MS auto-generation tools are going to bring to the table, because you're going to be reduced to using trivial XSD structures (I mean, no choice element support? Really?) to encapsulate your business object modeling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文