- using System;
- using System.IO;
- using System.Xml.Serialization;
- class Program
- {
- static void Main(string[] args)
- {
- Product Item1 = new Product();
- Item1.Name = "Mp3 Player";
- Item1.Price = 33.99;
- FileStream fStream = new FileStream("product.xml", FileMode.Create);
- XmlSerializer xmlSerializer = new XmlSerializer(typeof(Product));
- xmlSerializer.Serialize(fStream, Item1);
- fStream.Close();
- Console.WriteLine("Item1 has been serialized");
- Console.ReadKey();
- }
- }
- [System.Xml.Serialization.XmlRootAttribute()]
- public class Product
- {
- private string name;
- private double price;
- [XmlElement("Name")]
- public string Name
- {
- set { this.name = value; }
- get { return name; }
- }
- [XmlElement("Price")]
- public double Price
- {
- set { this.price = value; }
- get { return price; }
- }
- }
C# - Simple Write XmlSerializer
This snippet shows how to serialize a class into XML using the XmlSerializer Class.
No comments:
Post a Comment