- using System;
- using System.IO;
- using System.Xml.Serialization;
- class Program
- {
- static void Main(string[] args)
- {
- Product Item1;
- FileStream fStream = new FileStream("product.xml", FileMode.Open);
- XmlSerializer xmlSerializer = new XmlSerializer(typeof(Product));
- Item1 = (Product)xmlSerializer.Deserialize(fStream);
- Console.WriteLine("Item1: Name:" + Item1.Name);
- Console.WriteLine("Item1: Price:" + Item1.Price);
- fStream.Close();
- 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 Read XmlSerializer
This snippet shows how to read a serialized XML file using the XmlSerializer Class.
No comments:
Post a Comment