C#中怎樣讀取xml檔案中節點的值

來源:互聯網
上載者:User
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集合裡面了。。簡單吧。。嘿嘿。。
當然你可以做多個表和多個欄位屬性咯。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.