Any way to override how the <selection> element is bound by xsd.exe

My circuit has the following elements:

<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 circuit structure, I use an element to indicate the 1st situation:

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

When I ran xsd.exe, the resulting code:

    [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 problem is that the element ID tag is ignored, and xsd.exe arbitrarily calls the "Item" property. I must admit, this is not a big problem, but it starts to annoy me. What is annoying is that if I have additional elements on the same level, xsd.exe binds them as "Item1", "Item2", etc.

Does anyone know if xsd.exe can name my selection items as "Item" and you can specify my own property names instead?

+3
5

- xsd.exe NRefactory.

0

XSD.exe. , , XSD.exe ID . - MS, XSD.exe . , SchemaImporterExtension, , , , , / . , , XSD.exe ID, , XmlSchemaChoice, . , - , , , , , XSD MS . XSD.exe , " " XML-. -, WCF XSD.exe, , ? WCF svcutil XML-; svcutil IIRC. - , MS , XSD ( , ? -.

0

.

, , , .

xsd :

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>

:

<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 .

!

0

For those who still want to know: just use the XSDObjectGenerator (made by Microsoft). It manages XsdChoice by adding one object for each selection to your class. Thus, you do not need to search for the correct element to use the Item property. For instance:

<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>

has become

[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));
    }
}
0
source

Source: https://habr.com/ru/post/1741295/


All Articles