PersonListXMLWriter – Person 클래스에 XML Serialization을 사용하는 대신
XmlDocument 클래스를 사용하여 xml file 열기와 저장하기9520712777.zip
class PersonListManager
{
private List<Person> pList = null;
public string FileName
{
get;
set;
}
public PersonListManager(ref List<Person> pList)
{
this.pList = pList;
}
public void Load()
{
if (pList == null) return;
if (File.Exists(FileName))
{
pList.Clear();
// create filestream and xmlDoc loading
FileStream stream = File.Open(FileName, FileMode.OpenOrCreate, FileAccess.Read);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(stream);
XmlNode nodePersonList;
nodePersonList = xmlDoc.FirstChild;
foreach (XmlNode nodePerson in nodePersonList)
{
Person p = new Person();
p.Name = nodePerson.FirstChild.InnerText;
p.ID = Int32.Parse(nodePerson.FirstChild.NextSibling.InnerText);
p.Phone = nodePerson.FirstChild.NextSibling.NextSibling.InnerText;
p.Address = nodePerson.LastChild.InnerText;
pList.Add(p);
}
stream.Close();
pList.Sort(Person.IDComparison);
}
}
public void Save()
{
if (pList == null) return;
File.Delete(FileName); // delete the saved file
FileStream stream = File.Open(FileName, FileMode.OpenOrCreate);
// xmlDoc writing
XmlDocument xmlDoc = new XmlDocument();
XmlNode nodePersonList = xmlDoc.CreateNode(“element”, “PersonList”, “”);
foreach (Person p in pList)
{
XmlNode nodePerson = xmlDoc.CreateNode(“element”, “Person”, “”);
XmlNode nodeName = xmlDoc.CreateNode(“element”, “Name”, “”);
nodeName.InnerText = Convert.ToString(p.Name);
XmlNode nodeID = xmlDoc.CreateNode(“element”, “ID”, “”);
nodeID.InnerText = Convert.ToString(p.ID);
XmlNode nodePhone = xmlDoc.CreateNode(“element”, “Phone”, “”);
nodePhone.InnerText = Convert.ToString(p.Phone);
XmlNode nodeAddress = xmlDoc.CreateNode(“element”, “Address”, “”);
nodeAddress.InnerText = Convert.ToString(p.Address);
nodePerson.AppendChild(nodeName);
nodePerson.AppendChild(nodeID);
nodePerson.AppendChild(nodePhone);
nodePerson.AppendChild(nodeAddress);
nodePersonList.AppendChild(nodePerson);
}
xmlDoc.AppendChild(nodePersonList);
xmlDoc.Save(stream);
stream.Close();
}
public void Add(Person p)
{
pList.Add(p);
pList.Sort(Person.IDComparison);
Save();
}
}
public class Person : IComparable<Person>, IEquatable<Person>
{
// member field
protected string name;
protected int id;
protected string phone;
protected string address;
// delegate
public static Comparison<Person> NameComparison = delegate(Person p, Person q)
{
return p.Name.CompareTo(q.Name);
};
public static Comparison<Person> IDComparison = delegate(Person p, Person q)
{
return p.ID.CompareTo(q.ID);
};
public static Comparison<Person> PhoneComparison = delegate(Person p, Person q)
{
return p.Phone.CompareTo(q.Phone);
};
public static Comparison<Person> AddressComparison = delegate(Person p, Person q)
{
return p.Address.CompareTo(q.Address);
};
// property
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int ID
{
get
{
return id;
}
set
{
id = value;
}
}
public string Phone
{
get
{
return phone;
}
set
{
phone = value;
}
}
public string Address
{
get
{
return address;
}
set
{
address = value;
}
}
// constructor
public Person() : this(“”, 0, “”, “”)
{
}
public Person(string name, int id, string phone, string address)
{
this.name = name;
this.id = id;
this.phone = phone;
this.address = address;
}
public Person(Person p)
{
this.name = p.name;
this.id = p.id;
this.phone = p.phone;
this.address = p.address;
}
// method
public virtual void Print()
{
Console.WriteLine(“이름: {0} ID: {1} 전화번호: {2} 주소: {3}”, this.name, this.id, this.phone, this.address);
}
// needed for sort
#region IComparable<Person> Members
public int CompareTo(Person other)
{
return Name.CompareTo(other.Name);
}
#endregion
// needed for search
#region IEquatable<Person> Members
public bool Equals(Person other)
{
return Name.Equals(other.Name) && ID.Equals(other.ID) && Phone.Equals(other.Phone) && Address.Equals(other.Address);
}
#endregion
// System.Object.ToString method override
public override string ToString()
{
return string.Format(“{0},{1},{2},{3}”, Name, ID, Phone, Address); // csv file format으로 출력
}
// needed for ListView
#region ListViewItem
public ListViewItem ToListViewItem()
{
ListViewItem item = new ListViewItem(this.name);
item.SubItems.Add(new ListViewItem.ListViewSubItem(item, this.id.ToString()));
item.SubItems.Add(new ListViewItem.ListViewSubItem(item, this.phone));
item.SubItems.Add(new ListViewItem.ListViewSubItem(item, this.address));
return item;
}
#endregion
}
-<PersonList>
-<Person>
<Name>Park </Name>
<ID>1207</ID>
<Phone> 010-</Phone>
<Address>단국대학교</Address>
</Person>
-<Person>
<Name>박똑똑</Name>
<ID>5205</ID>
<Phone>10</Phone>
<Address>단국대</Address>
</Person>
-<Person>
<Name>아무개</Name>
<ID>1202</ID>
<Phone>041-550-3418</Phone>
<Address>충남 천안시</Address>
</Person>
-<Person>
<Name>홍군</Name>
<ID>1204</ID>
<Phone> 041-550-3490</Phone>
<Address>충남 천안시</Address>
</Person>
-<Person>
<Name>김씨</Name>
<ID>5204</ID>
<Phone>11</Phone>
<Address>단국대</Address>
</Person>
</PersonList>