Search code examples
c#xmlxsdxsd2code

Accessing Xml Element Attribute in a xsd class


I have a generated c# class from xsd. when I want to give value to the elements

result res = new result();
res.feed = "123132";

// This works res.data here I can not find id and value, how can I do for instance res.data.id="something"

I have an xml :

<result>
    <feed></feed>
    <status></status>
    <data>
        <id></id>
        <value></value> 
        <id></id> 
        <value></value> 
    </data>
</result>

that generates this xsd :

  <xs:element name="result">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="feed"/>
        <xs:element type="xs:string" name="status"/>
        <xs:element name="data">
          <xs:complexType>
            <xs:choice maxOccurs="unbounded" minOccurs="0">
              <xs:element type="xs:string" name="id"/>
              <xs:element type="xs:string" name="value"/>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

and with xsd.exe I have generated the class for it

public partial class result {

    private string feedField;

    private string statusField;

    private resultData dataField;

    /// <remarks/>
    public string feed {
        get {
            return this.feedField;
        }
        set {
            this.feedField = value;
        }
    }

    /// <remarks/>
    public string status {
        get {
            return this.statusField;
        }
        set {
            this.statusField = value;
        }
    }

    /// <remarks/>
    public resultData data {
        get {
            return this.dataField;
        }
        set {
            this.dataField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class resultData {

    private string[] itemsField;

    private ItemsChoiceType[] itemsElementNameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("id", typeof(string))]
    [System.Xml.Serialization.XmlElementAttribute("value", typeof(string))]
    [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
    public string[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public ItemsChoiceType[] ItemsElementName {
        get {
            return this.itemsElementNameField;
        }
        set {
            this.itemsElementNameField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema=false)]
public enum ItemsChoiceType {

    /// <remarks/>
    id,

    /// <remarks/>
    value,
}

Solution

  • It looks like you have to do the following

    res.data = new resultData();
    res.data.ItemsElementName = new [] 
    { 
        ItemsChoiceType.id,
        ItemsChoiceType.value,
        ItemsChoiceType.id,
        ItemsChoiceType.value,
    };
    res.data.Items = new [] 
    { 
        "id 1",
        "value 1",
        "id 2",
        "value 2",
    };
    

    Basically you have to set each array to contain the type and corresponding value. When you create the arrays they should have the same length and the type and value should be in the same position.

    However if you want id-value pairs you should rethink the structure of your XML as currently it will allow you to just put in ids or just values and does not guarantee order or that there will be the same number of each. Maybe something like the following is more like want you want.

    <result>
        <feed></feed>
        <status></status>
        <data>
            <dataItem>
                <id></id>
                <value></value> 
            </dataItem>
            <dataItem>
                <id></id> 
                <value></value> 
            </dataItem>
        </data>
    </result>