C#Xml文件解析,序列化,反序列化

public class ComputerManger
{
public Dictionary> Mycomputer { get; set; }

//解析xml文件
public void LoadFromXml(string XMLpath)
{
Dictionary tempDictionry = null;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(XMLpath);
XmlNode xmlnode = xmldoc.DocumentElement;
string brand=null;
foreach (XmlNode node in xmlnode.ChildNodes)
{
brand = node.Attributes["name"].Value;
tempDictionry = new Dictionary();
foreach (XmlNode nodebrand in node.ChildNodes)
{
if (nodebrand.Attributes["kind"].Value == "pc")
{
DeskTop desktop = new DeskTop();
desktop.CPU = nodebrand["cpu"].InnerText;
desktop.Brand = brand;
desktop.Memory = nodebrand["memory"].InnerText;
desktop.Monitor = nodebrand["monitor"].InnerText;
https://www.360docs.net/doc/bc973277.html, = nodebrand["name"].InnerText;
desktop.HostType=nodebrand["hosttype"].InnerText;
desktop.HardDisk = nodebrand["harddisk"].InnerText;
tempDictionry.Add(https://www.360docs.net/doc/bc973277.html,,desktop);
}
else
{
NoteBook notebook = new NoteBook();
notebook.CPU = nodebrand["cpu"].InnerText;
notebook.Brand = brand;
notebook.Memory = nodebrand["memory"].InnerText;
notebook.Monitor = nodebrand["monitor"].InnerText;
https://www.360docs.net/doc/bc973277.html, = nodebrand["name"].InnerText;
notebook.Battery = nodebrand["battery"].InnerText;
notebook.HardDisk = nodebrand["harddisk"].InnerText;
tempDictionry.Add(https://www.360docs.net/doc/bc973277.html,,notebook);
}


}
}
Mycomputer = new Dictionary>();
this.Mycomputer.Add(brand,tempDictionry);

}
//序列化
public void SaveSerialize()
{
FileStream filestrem = new FileStream(@"Save.bin",FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(filestrem,this.Mycomputer);
filestrem.Close();

}
//反序列化
public void LoadSweialize()
{
FileStream filestrem = new FileStream(@"Save.bin", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
this.Mycomputer = (Dictionary>)bf.Deserialize(filestrem);

filestrem.Close();
}

相关文档
最新文档