1.10.2. fejezet, XMLReader, XMLWriter és XMLSerializer

namespace XMLProject
{
    public class Address
    {
        [XmlElement("Country")]
        public string Country { get; set; }
        [XmlElement("PostalCode")]
        public int PostalCode { get; set; }
        [XmlElement("State")]
        public int State { get; set; }
        [XmlElement("City")]
        public string City { get; set; }
        [XmlElement("Street")]
        public string Street { get; set; }
    }
 
    public class Clerk
    {
        [XmlAttribute("ID")]
        public int ID { get; set; }
        [XmlElement("FirstName")]
        public string FirstName { get; set; }
        [XmlElement("LastName")]
        public string LastName { get; set; }
        [XmlElement("Email")]
        public string Email { get; set; }
        [XmlElement("PhoneNumber")]
        public string PhoneNumber { get; set; }
        [XmlElement("Address")]
        public Address Address { get; set; }
    }
 
    class Program
    {
 
        private static List<Clerk> GetCustomerList()
        {
            FileStream fstream = new FileStream("c:\\temp\\clerks.xml", FileMode.Open);
            XmlSerializer ser = new XmlSerializer(typeof(List<Clerk>));
            List<Clerk> result = (List<Clerk>)ser.Deserialize(fstream);
            fstream.Close();
            return result;
        }
 
        private static void generateCustomerSample()
        {
            List<Clerk> result = new List<Clerk>();
            Clerk cust1 = new Clerk();
            cust1.ID = 1;
            cust1.FirstName = "Andor";
            cust1.LastName = "Pázmány";
            Address addr = new Address();
            addr.City = "Budapest";
            addr.Country = "Hungary";
            addr.State = 36;
            cust1.Address = addr;
            result.Add(cust1);
 
            Clerk cust2 = new Clerk();
            cust2.ID = 2;
            cust2.FirstName = "Dezső";
            cust2.LastName = "Nánási";
            Address addr2 = new Address();
            addr2.City = "Budapest";
            addr2.Country = "Hungary";
            addr2.State = 36;
            cust2.Address = addr;
            result.Add(cust2);
 
            FileStream fstream = new FileStream(@"c:\temp\clerk.xml", FileMode.Create);
            XmlSerializer ser = new XmlSerializer(typeof(List<Clerk>));
            ser.Serialize(fstream, result);
            fstream.Close();
        }
 
 
        public static void Main(string[] args)
        {
            generateCustomerSample();
            XmlReader reader = XmlReader.Create(@"c:\temp\clerk.xml");
            while (reader.MoveToNextAttribute() || reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        Console.WriteLine("<{0}>", reader.Name);
                        break;
                    case XmlNodeType.EndElement:
                        Console.WriteLine("</{0}>", reader.Name);
                        break;
                    case XmlNodeType.Attribute:
                        Console.WriteLine("{0} = {1}", reader.Name, reader.Value);
                        break;
                    case XmlNodeType.Text:
                        Console.WriteLine(reader.Value);
                        break;
                    default:
                        break;
                };
            }
            reader.Close();
            Console.ReadKey();            
 
            XmlTextWriter writer = new XmlTextWriter(@"c:\temp\persons.xml", Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteComment(DateTime.Now.ToString());
            writer.WriteStartElement("PersonsList");
            writer.WriteStartElement("Person");
            writer.WriteAttributeString("id", "1");
            writer.WriteElementString("Name", "Reiter Istvan");
            writer.WriteElementString("Age", "26");
            writer.WriteEndElement();
            writer.Close();
        }        
    }
}