Ganesh Kumar
Ganesh Kumar

Reputation: 108

How to deserialize a xml file

<countries>
    <country code="AF" iso="4">Afghanistan</country>
    <country code="AL" iso="8">Albania</country>
    <country code="DZ" iso="12">Algeria</country>
    <country code="AS" iso="16">American Samoa</country>
    <country code="AD" iso="20">Andorra</country>
    <country code="AO" iso="24">Angola</country>
    <country code="AI" iso="660">Anguilla</country>
    <country code="AQ" iso="10">Antarctica</country>
    <country code="AG" iso="28">Antigua And Barbuda</country>
    <country code="AR" iso="32">Argentina</country>
    <country code="AM" iso="51">Armenia</country>
    <country code="AW" iso="533">Aruba</country>
    <country code="AU" iso="36">Australia</country>
    <country code="AT" iso="40">Austria</country>
    <country code="AZ" iso="31">Azerbaijan</country>
    <country code="BS" iso="44">Bahamas</country>
    <country code="BH" iso="48">Bahrain</country>
    <country code="BD" iso="50">Bangladesh</country>
    <country code="BB" iso="52">Barbados</country>
    <country code="BY" iso="112">Belarus</country>
    <country code="BE" iso="56">Belgium</country>
    <country code="BZ" iso="84">Belize</country>
    <country code="BJ" iso="204">Benin</country>
    <country code="BM" iso="60">Bermuda</country>
    <country code="BT" iso="64">Bhutan</country>
    <country code="BO" iso="68">Bolivia</country>
    <country code="BA" iso="70">Bosnia And Herzegovina</country>
    <country code="BW" iso="72">Botswana</country>
    <country code="BV" iso="74">Bouvet Island</country>
    <country code="BR" iso="76">Brazil</country>
    <country code="IO" iso="86">British Indian Ocean Territory</country>
    <country code="BN" iso="96">Brunei Darussalam</country>
    <country code="BG" iso="100">Bulgaria</country>
    <country code="BF" iso="854">Burkina Faso</country>
    <country code="BI" iso="108">Burundi</country>
</countries>

Someone please guide me on how to design my class to deserialize this .

Here is my current class design

public class Country
{
    public string country { get; set; }
    public string code { get; set; }
    public int iso { get; set; }
}

But this doesn't seem to work. Please someone guide me on this.

Upvotes: 0

Views: 130

Answers (4)

Antonio Bakula
Antonio Bakula

Reputation: 20693

There is no Serializable attribute for Windows 8 apps, you have to use DataContractAttribute and DataMemberAttribute and decorate your model class, something like this:

[DataContractAttribute]
public class Country
{
  [DataMemberAttribute]
  public string country { get; set; }
  [DataMemberAttribute]
  public string code { get; set; }
  [DataMemberAttribute]
  public int iso { get; set; }
}

And then you can serialize this class, here is the example for Json

public static string SerializeToJson(object instance)
{
  using (MemoryStream _Stream = new MemoryStream())
  {
    var _Serializer = new DataContractJsonSerializer(instance.GetType());
    _Serializer.WriteObject(_Stream, instance);
    _Stream.Position = 0;
    using (StreamReader _Reader = new StreamReader(_Stream))
    { return _Reader.ReadToEnd(); }
  }
}

Please see this answer for examples for XML:

https://stackoverflow.com/a/17055641/351383

Upvotes: 2

RogerN
RogerN

Reputation: 3821

Contrary to the other answers so far, you do not need to use the Serializable attribute when working with Xml serialization. But you do need to adorn your properties with code attributes describing what part of the Xml file the values will be taken from.

Since you haven't included the Xml document declaration in your question, I'm not sure if the countries collection is the root node of your document or not. But let's assume that your entire Xml document actually looks like this:

<?xml version="1.0" encoding="utf-8" ?> 
<countries>
  <country code="AF" iso="4">Afghanistan</country>
  <country code="AL" iso="8">Albania</country>
  <country code="DZ" iso="12">Algeria</country>
</countries>

You need to apply code attributes to your classes which describe how the above Xml maps to your properties and objects. These attributes are defined in System.Xml. This is how the attributes might look in your case:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
...
[XmlRoot("countries",  Namespace="")]
public class countriesDocument
{
    [XmlElement("country")]
    public country[] countries { get; set; }
}

public class country
{
    [XmlText]
    public string name { get; set; }

    [XmlAttribute]
    public string code { get; set; }

    [XmlAttribute]
    public int iso { get; set; }
}

Then you can deserialize the document with something like the following code:

var serializer = new XmlSerializer(typeof(countriesDocument));
countriesDocument document;
using (var reader = File.OpenText("countries.xml"))
{
    document = (countriesDocument)serializer.Deserialize(reader);
}

Upvotes: 3

Ricardo Silva
Ricardo Silva

Reputation: 1384

First of all, as said by Oscar. You must add the decorator Serializable to your class.

[Serializable]
public class Country
{
   public string country { get; set; }
   public string code { get; set; }
   public int iso { get; set; }
}

Then you must follow the steps above to deserialize your xml into objects:

private Country[] DeserializeCountries(string xmlPath)
{
    // Create an instance of the XmlSerializer specifying type and namespace.
    XmlSerializer serializer = new
    XmlSerializer(typeof(Country));

    // A FileStream is needed to read the XML document.
    FileStream fs = new FileStream(xmlPath, FileMode.Open);
    XmlReader reader = XmlReader.Create(fs);

    // Use the Deserialize method to restore the object's state.
    Country[] countries = (Country[])serializer.Deserialize(reader);
    fs.Close();

    return countries;
}

The code above was adapted from Microsoft Documentation

Upvotes: -1

Oscar
Oscar

Reputation: 13990

The first step to serialize-deserialize a class is to mark it with the Serializable attribute

[Serializable]
public class Country
{
   public string country { get; set; }
   public string code { get; set; }
   public int iso { get; set; }
}

Upvotes: -1

Related Questions