-1

I have an auto generated class from an xml like the following:

public partial class XmlClass {

private decimal num1;

private ClassA[] classField;

/// <remarks/>
public decimal num1 {
    get; 
    set; 
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("classA")]
public ClassA[] classA {
    get{...};
    set{...}; 

}

}

public partial class ClassA {

private object[] itemsField;

private string typeField;

[System.Xml.Serialization.XmlElementAttribute("commands", typeof(classACommands))]

[System.Xml.Serialization.XmlElementAttribute("minVersion", typeof(string))] 
public object[] Items {
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
    }
}

[System.Xml.Serialization.XmlAttributeAttribute()]
public string type {
    get {
        return this.typeField;
    }
    set {
        this.typeField = value;
    }
}

}

ClassA has the string and commands class as Objects in the Object[]. I can see everything is deserialized perfectly and get them by: (commands)myXmlClass.classA.ElementAt(i).Items[3], where i is from the index of the ClassA array. But how can I get or set them without using '3'? It might be different in different ClassA elements.

  • If you have an array you need an index to access its elements...why would you want not to use the index? – MiMo Sep 05 '12 at 12:44
  • ...`myXmlClass.classA.ElementAt(i)` is of type `ClassA`, why the cast to `commands`? Your code seems also to be missing the declaration of a property after `[System.Xml.Serialization.XmlElementAttribute("commands", typeof(classACommands))]` – MiMo Sep 05 '12 at 12:50
  • I actually have more objects under ClassA and not all ClassA elements actually have the same number of objects. So the index for the same object might be different in different ClassA elements. For example for the first ClassA in the array. command obj might be indexed at 3 but in the third it could be 4. I do have the xmlElementAttribute declaration in my code and it was commands)myXmlClass.classA.ElementAt(i).Items[3] instead of commands)myXmlClass.classA.ElementAt(i)... Thanks. – NewDTinStackoverflow Sep 05 '12 at 16:16

1 Answers1

0

Not sure I totally understand your question, but you can use a foreach loop:

foreach(ClassA a in myXmlClass.classA) {
  Console.WriteLine(a.num1.ToString());
}
David Hoerster
  • 28,421
  • 8
  • 67
  • 102
  • I don't have problem with getting a ClassA from the ClassA array, classA. I am trying to get the objects of the object item, commands class or minVersion string here, of a ClassA item in the classA. The first XMLclass includes a ClassA array. Each ClassA item in that array includes an object array with different objects from xmlElements. – NewDTinStackoverflow Sep 05 '12 at 00:31