Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing an array element within an array element using c#
    primarykey
    data
    text
    <p>My XML file looks something like this:</p> <pre><code>&lt;MyXml&gt; &lt;Version&gt; 9.3.2 &lt;/Version&gt; &lt;Resources&gt; &lt;Sets&gt; &lt;ItemCollection&gt; &lt;Item&gt; &lt;Name&gt; Name &lt;/Name&gt; &lt;Age&gt; 66 &lt;/Age&gt; &lt;/Item&gt; &lt;/ItemCollection&gt; &lt;/Sets&gt; &lt;/Resources&gt; </code></pre> <p>I'm trying to get to the items within the ItemCollection, but so far, not luck at all; this is what my code looks like:</p> <pre><code>Stream reader = new FileStream(fileLocation, FileMode.Open); XmlSerializer s = new XmlSerializer(typeof(MyClass)); var items = s.Deserialize(reader) as MyClass; </code></pre> <p>And my objects look like:</p> <pre><code>[Serializable] [XmlRoot("MyXml")] public class MyClass { [XmlElement("Version")] public string Version { get; set; } [XmlElement("Resources")] public List&lt;Resources&gt; Resources{ get; set; } } [Serializable] public class Resources { [XmlElement("Sets")] public List&lt;Sets&gt; Sets { get; set; } } [Serializable] public class Sets { [XmlArray(ElementName = "ItemCollection")] [XmlArrayItem("Item")] public List&lt;Item&gt; Items { get; set; } } [Serializable] public class Item { [XmlElement("Name")] public string Name{ get; set; } [XmlElement("Age")] public string Age { get; set; } } </code></pre> <p>I can get the version just fine, and the hierarchy looks fine, but Name and Age from the Item object are always null. I've tried XmlElement instead of the XmlArray, but that doesn't work either.</p> <p>Any help on how to achieve this will be much appreciated!!! </p> <p><strong>EDIT:</strong> The example I gave was a simplification of the XML that I receive: it is actually a call to the location REST service from the BING API; the XML I get looks like the one in this URL:</p> <p><a href="http://msdn.microsoft.com/en-us/library/ff701710.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ff701710.aspx</a></p> <p>and what I'm trying to put in my structures are the information within the Location element. </p> <p>My real objects look like this:</p> <pre><code>[Serializable] [XmlRoot("Response")] public class LocationService { [XmlElement("StatusCode")] public string Code{ get; set; } [XmlElement("ResourceSets")] public List&lt;ResourceSets&gt; ResourceSets{ get; set; } } [Serializable] public class ResourceSets { [XmlElement("ResourceSet")] public List&lt;ResourceSet&gt; ResourceSet { get; set; } } [Serializable] public class ResourceSet { [XmlArray(ElementName = "Resources")] [XmlArrayItem("Location")] public List&lt;Location&gt; Locations { get; set; } } [Serializable] public class Location { [XmlElement("Latitude")] public string Latitude{ get; set; } [XmlElement("Longitude")] public string Longitude{ get; set; } } </code></pre> <p>Hopefully this will clarify even further what I'm trying to achieve here.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload