2009-08-04 23:26
很高興可以給你解答!
xml實際就是一個本地簡單的資料庫
我只做了一個簡單的。。但是道理是一樣的。
//xml檔案資訊
<abc>
<Field>100</Field>
<item>
<id>1</id>
<name>zhangsan</name>
<sex>男</sex>
</item>
<item>
<id>2</id>
<name>lisi</name>
<sex>男</sex>
</item>
</abc>
//實體類。
public class Information
{
private string id;
public string Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
public Information()
{
}
public Information(string id,string name,string sex)
{
this.Id = id;
this.Name = name;
this.Sex = sex;
}
}
//讀取xml裡面的檔案資訊
List<Information> list = new List<Information>();
//執行個體化xml
XmlDocument xml = new XmlDocument();
//讀取xml檔案
xml.Load(@"E:\C#\S2C#\DLCL\列印電腦\MyComputer\XulieHua\XML.xml"); //你的xml地址
string id = "";
string name = "";
string sex = "";
Information info = null;
//////////*******下面開始迴圈讀取xml檔案資訊********/
///////////////
foreach (XmlNode node in xml.ChildNodes)
{
if (node.Name == "abc")
{
foreach (XmlNode node1 in node.ChildNodes)
{
if (node1.Name == "item")
{
foreach (XmlNode node2 in node1.ChildNodes)
{
switch (node2.Name)
{
case "id":
id = node2.InnerText;
break;
case "name":
name = node2.InnerText;
break;
default:
sex = node2.InnerText;
break;
}
}
info = new Information(id, name, sex);
//將資訊儲存至集合
list.Add(info);
}
}
}
}
xml裡面的所有資訊就是在list集合裡面了。。簡單吧。。嘿嘿。。
當然你可以做多個表和多個欄位屬性咯。。