2

I've the following XML

<getAvailability>
  <result>
    <arrival_date>2013-05-05</arrival_date>
    <block>
    <block_id>80884788</block_id>
    </block>
    <id>230802</id>
   </result>
   <result>
    <arrival_date>2013-05-05</arrival_date>
    <block>
    <block_id>419097</block_id>
    </block>
    <id>98121</id>
  </result>
</getAvailability>

How do I go about deserialising it and getting it into classes in C#? Many thanks. In addition here is my code;

XmlSerializer serializer = new XmlSerializer(typeof(getAvailability));
getAvailability output;

using (StringReader reader = new StringReader(xmlSource))
{
    output = (getAvailability)serializer.Deserialize(reader);
}

public class Result
{
    [XmlElement("block_id")]
    public string sBlockID { get; set; }

    [XmlElement("arrival_date")]
    public DateTime dArrivalDate { get; set; }

    [XmlElement("id")]
    public int iID{ get; set; }
}

[XmlRoot("getAvailability")]
public class getAvailability
{
     [XmlArray("result")]
     [XmlArrayItem("block", typeof(Result))]
     public Result[] Result { get; set; }
}

Let me know if you need any further information.

neildt
  • 5,101
  • 10
  • 56
  • 107
  • 3
    It appears it has been asked here with a good answer http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document – Mike Weber May 03 '13 at 16:55
  • I followed the example at http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document, but for some reason my program is only reading in the last element of the XML file – neildt May 03 '13 at 17:17
  • Can you post a small sample program that is misbehaving? – Matt Johnson May 03 '13 at 17:45
  • Just updated my original post with the code – neildt May 03 '13 at 18:28

1 Answers1

2

The easiest way would be to structure your result class as shown in the code sample below so as to easily map it to the XML sample you provided. I have added a few comments that provide information about some methods.

using System;
using System.IO;
using System.Xml.Serialization;

namespace Test
{
    public class Program
    {
        public static void Main(string[] args)
        {

            Result result1 = new Result
                                 {
                                     arrival_date = new DateTime(2013, 05, 05),
                                     block = new Result.Block { block_id = 80884788 },
                                     id = 230802
                                 };
            Result result2 = new Result
                                 {
                                     arrival_date = new DateTime(2013, 05, 05),
                                     block = new Result.Block { block_id = 419097 },
                                     id = 98121
                                 };
            Results results = new Results { result = new Result[2] };
            results.result[0] = result1;
            results.result[1] = result2;

            WriteSettingsAsXml("D:\\test.xml", typeof(Results), results, true);

            Results gA = (Results)ReadSettingsFromXml("D:\\test.xml", typeof(Results));
        }

        // This `Result` class below maps to a single result in the XML you provided. This class is used in the `Results` class to obtain the needed XML structure.   

        public class Result
        {
            public class Block
            {
                public Int32 block_id { get; set; }
            }

            public DateTime arrival_date { get; set; }
            public Block block { get; set; }
            public Int32 id { get; set; }
        }

        [Serializable()]
        [XmlRootAttribute("getAvailability", Namespace = "", DataType = "", IsNullable = false)]
        public class Results
        {
            [XmlElement("result")]
            public Result[] result { get; set; }
        }

        /// Library methods that saves/reads any passed/retrieved object into/from a xml file at specified location

        public static void WriteSettingsAsXml(string destinationPath, Type objectType, object objectValue, bool hideNamespaces)
        {
            XmlSerializer serializer = new XmlSerializer(objectType);
            using (TextWriter writer = new StreamWriter(destinationPath))
            {
                if (hideNamespaces)
                {
                    XmlSerializerNamespaces hiddenNamespaces = new XmlSerializerNamespaces();
                    hiddenNamespaces.Add("", "");
                    serializer.Serialize(writer, objectValue, hiddenNamespaces);
                }
                else
                    serializer.Serialize(writer, objectValue);
                writer.Close();
            }
        }
        public static object ReadSettingsFromXml(string xmlFilePath, Type objectType)
        {
            XmlSerializer serializer = new XmlSerializer(objectType);
            using (FileStream fileStream = new FileStream(xmlFilePath, FileMode.Open))
            {
                return serializer.Deserialize(fileStream);
            }
        }
    }
}
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
  • I've used the example below which gave two elements ' 2013-05-05 80884788 230802 2013-05-05 419097 98121 ' But for some reason when I try a much bigger dataset from the live source it only returns the first node? Any ideas what might be causing this – neildt May 11 '13 at 12:19