1

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,
}
lol
  • 93
  • 3
  • 11

1 Answers1

2

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>
juharr
  • 31,741
  • 4
  • 58
  • 93
  • res.data.id = "jhgjh" give me the error can not implicitely convert 'string' to 'ItemsChoiceType[]' – lol Jan 26 '16 at 15:13
  • @lol Yeah, I've updated to reflect how the `resultData` class actually works. – juharr Jan 26 '16 at 15:15
  • in fact when I put it in try catch gives me the error : Object reference not set to an instance of an object. – lol Jan 26 '16 at 15:18
  • @lol Make sure to create a `resultData` first. I've updated my answer. – juharr Jan 26 '16 at 15:27
  • I tried to do this : res.data = new resultData(); res.data.ItemsElementName = new[] { ItemsChoiceType.id }; res.data.Items = new[] { "something" }; res.data.ItemsElementName = new[] { ItemsChoiceType.value }; res.data.Items = new[] { "sth" }; res.data.ItemsElementName = new[] { ItemsChoiceType.id }; res.data.Items = new[] { "xxxx" }; res.data.ItemsElementName = new[] { ItemsChoiceType.value }; res.data.Items = new[] { "yyyyyy" }; – lol Jan 26 '16 at 15:36
  • after XmlSerializer xmlSerializer = new XmlSerializer(typeof(result)); using (StringWriter textWriter = new StringWriter()) { xmlSerializer.Serialize(textWriter, res); xmlString = textWriter.ToString(); } – lol Jan 26 '16 at 15:36
  • There is Only the last value! true 123132 yyyyyy – lol Jan 26 '16 at 15:37
  • I'll update to show you how to add multiple ids and values by putting them all into one array that you assign to the properties. – juharr Jan 26 '16 at 15:48
  • but what if I need to have all these id and values under data? – lol Jan 26 '16 at 16:08
  • as in xsd also we have – lol Jan 26 '16 at 16:11