using System; using System.Xml; using System.Collections; namespace 예제_3._5_XmlReader_개체로_요소_읽기 { class Book { internal string Title { get; private set; } internal int Price { get; private set; } internal static Book MakeBook(XmlReader xr) { string title = string.Empty; int price = 0; xr.ReadToDescendant("title"); title = xr.ReadElementString("title"); xr.ReadToNextSibling("price"); xr.ReadStartElement("price"); price = int.Parse(xr.Value); return new Book(title, price); } Book(string title, int price) { Title = title; Price = price; } public override string ToString() { return Title; } } class Program { static void Main(string[] args) { ArrayList ar = new ArrayList(); XmlReader reader = XmlReader.Create("data.xml"); while (reader.Read()) { if (reader.IsStartElement("book")) { Book book = Book.MakeBook(reader); if (book != null) { ar.Add(book); } } } Console.WriteLine("도서 개수:{0}", ar.Count); foreach (Book book in ar) { Console.WriteLine("도서명:{0} 가격:{1}", book.Title, book.Price); } } } }