Question

My XML file looks something like this:

<MyXml>
<Version> 9.3.2 </Version>
<Resources>      
  <Sets>
    <ItemCollection>
       <Item>
           <Name> Name </Name>
           <Age> 66 </Age>
       </Item>
     </ItemCollection>
   </Sets>
</Resources>

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:

Stream reader = new FileStream(fileLocation, FileMode.Open);
XmlSerializer s = new XmlSerializer(typeof(MyClass));
var items = s.Deserialize(reader) as MyClass;

And my objects look like:

[Serializable]
[XmlRoot("MyXml")]
public class MyClass
{
   [XmlElement("Version")]
   public string Version { get; set; }

   [XmlElement("Resources")]
   public List<Resources> Resources{ get; set; }
}

[Serializable]
public class Resources
{       
   [XmlElement("Sets")]
   public List<Sets> Sets { get; set; }
}

[Serializable]
public class Sets
{
    [XmlArray(ElementName = "ItemCollection")]
    [XmlArrayItem("Item")]
    public List<Item> Items { get; set; }
}

[Serializable]
public class Item
{
    [XmlElement("Name")]
    public string Name{ get; set; }

    [XmlElement("Age")]
    public string Age { get; set; }
}

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.

Any help on how to achieve this will be much appreciated!!!

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

http://msdn.microsoft.com/en-us/library/ff701710.aspx

and what I'm trying to put in my structures are the information within the Location element.

My real objects look like this:

[Serializable]
[XmlRoot("Response")]
public class LocationService
{
   [XmlElement("StatusCode")]
   public string Code{ get; set; }

   [XmlElement("ResourceSets")]
   public List<ResourceSets> ResourceSets{ get; set; }
}

[Serializable]
public class ResourceSets
{       
   [XmlElement("ResourceSet")]
   public List<ResourceSet> ResourceSet { get; set; }
}

[Serializable]
public class ResourceSet
{
    [XmlArray(ElementName = "Resources")]
    [XmlArrayItem("Location")]
    public List<Location> Locations { get; set; }
}

[Serializable]
public class Location
{
    [XmlElement("Latitude")]
    public string Latitude{ get; set; }

    [XmlElement("Longitude")]
    public string Longitude{ get; set; }
}

Hopefully this will clarify even further what I'm trying to achieve here.

Thanks!

Was it helpful?

Solution

Maybe your input file is incorrect because I believe your code works.

Here's a quick console app I wrote to test. Name and Age are both deserialized correctly.

Note: I assume your actual xml is not missing the closing tag, which is missing from your example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
  [Serializable]
  [XmlRoot("MyXml")]
  public class MyClass
  {
    [XmlElement("Version")]
    public string Version { get; set; }

    [XmlElement("Resources")]
    public List<Resources> Resources { get; set; }
  }

  [Serializable]
  public class Resources
  {
    [XmlElement("Sets")]
    public List<Sets> Sets { get; set; }
  }

  [Serializable]
  public class Sets
  {
    [XmlArray(ElementName = "ItemCollection")]
    [XmlArrayItem("Item")]
    public List<Item> Items { get; set; }
  }

  [Serializable]
  public class Item
  {
    [XmlElement("Name")]
    public string Name { get; set; }

    [XmlElement("Age")]
    public string Age { get; set; }
  }

  class Program
  {
    static void Main(string[] args)
    {
      string xml = 
      @"<MyXml>
         <Version> 9.3.2 </Version>
         <Resources>      
           <Sets>
             <ItemCollection>
               <Item>
                 <Name> Name </Name>
                 <Age> 66 </Age>
               </Item>
             </ItemCollection>
           </Sets>
         </Resources>
       </MyXml>";

      MemoryStream str = new MemoryStream( UTF8Encoding.UTF8.GetBytes( xml ) );

      XmlSerializer s = new XmlSerializer(typeof(MyClass));
      var items = s.Deserialize( str ) as MyClass;

      Console.Write( "Done" );

    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top