Skip to content

Instantly share code, notes, and snippets.

@ivankahl
Created September 24, 2015 13:22
Show Gist options
  • Save ivankahl/ae09b7ae7c9867f8bc53 to your computer and use it in GitHub Desktop.
Save ivankahl/ae09b7ae7c9867f8bc53 to your computer and use it in GitHub Desktop.
Serialize and Deserialize XML data in C#
<?xml version="1.0"?>
<toyStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Toys R Us">
<toys>
<toy>
<name>Teddy</name>
<price>100</price>
<suitableAge>7</suitableAge>
</toy>
<toy>
<name>Hot Wheels</name>
<price>70</price>
<suitableAge>6</suitableAge>
</toy>
<toy>
<name>Mechano</name>
<price>1000</price>
<suitableAge>14</suitableAge>
</toy>
<toy>
<name>Lego</name>
<price>500</price>
<suitableAge>12</suitableAge>
</toy>
<toy>
<name>Barbie</name>
<price>150</price>
<suitableAge>10</suitableAge>
</toy>
</toys>
</toyStore>
using System;
using System.IO;
using System.Xml.Serialization;
namespace XMLSerilizationDeserialization
{
class Program
{
static void Main(string[] args)
{
string data = File.ReadAllText("C:\\Users\\Ivan\\data.xml");
XmlSerializer x = new XmlSerializer(typeof(ToyStore));
ToyStore toyStore = (ToyStore)x.Deserialize(new StringReader(data));
Console.WriteLine("Name: {0}", toyStore.Name);
foreach(Toy toy in toyStore.Toys)
{
Console.WriteLine(toy.Name);
Console.WriteLine(new string('=', toy.Name.Length));
Console.WriteLine("Price: R{0}", toy.Price);
Console.WriteLine("Suitable Age: {0}", toy.SuitableAge);
Console.WriteLine();
}
Console.ReadKey();
}
}
}
using System;
using System.IO;
using System.Xml.Serialization;
namespace XMLSerilizationDeserialization
{
class Program
{
static void Main(string[] args)
{
ToyStore toyStore = new ToyStore("Toys R Us");
toyStore.Toys.Add(new Toy("Teddy", 100, 7));
toyStore.Toys.Add(new Toy("Hot Wheels", 70, 6));
toyStore.Toys.Add(new Toy("Mechano", 1000, 14));
toyStore.Toys.Add(new Toy("Lego", 500, 12));
toyStore.Toys.Add(new Toy("Barbie", 150, 10));
XmlSerializer x = new XmlSerializer(typeof(ToyStore));
FileStream fs = new FileStream("C:\\Users\\Ivan\\data.xml", FileMode.Create);
x.Serialize(fs, toyStore);
fs.Close();
}
}
}
using System.Xml;
using System.Xml.Serialization;
namespace XMLSerilizationDeserialization
{
public class Toy
{
private string _name;
private int _price;
private int _suitableAge;
public Toy(string name, int price, int suitableAge)
{
_name = name;
_price = price;
_suitableAge = suitableAge;
}
public Toy()
{
_name = "";
_price = -1;
_suitableAge = -1;
}
[XmlElement("name")]
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
[XmlElement("price")]
public int Price
{
get
{
return _price;
}
set
{
_price = value;
}
}
[XmlElement("suitableAge")]
public int SuitableAge
{
get
{
return _suitableAge;
}
set
{
_suitableAge = value;
}
}
}
}
using System.Xml;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace XMLSerilizationDeserialization
{
[XmlRoot("toyStore")]
public class ToyStore
{
private string _name;
private List<Toy> _toys;
public ToyStore(string name)
{
_name = name;
_toys = new List<Toy> { };
}
public ToyStore()
{
_name = "";
_toys = new List<Toy> { };
}
[XmlAttribute("name")]
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
[XmlArray("toys")]
[XmlArrayItem("toy")]
public List<Toy> Toys
{
get
{
return _toys;
}
set
{
_toys = value;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment