使用XmlDatabase讀取XML文件中的數(shù)據(jù)

2016-04-23    分類: 網(wǎng)站建設(shè)

public static DataTable GetData(string path,string tableName)
{
//創(chuàng)建XmlDocument類的實(shí)例
XmlDocument xmldoc = new XmlDocument();
//調(diào)用XmlDocument類中的Load()方法加載XML文件
xmldoc.Load(path);
//創(chuàng)建DataTable類型的變量dt
DataTable dt = new DataTable();
///獲取根節(jié)點(diǎn)
XmlNode rootNode = xmldoc.SelectSingleNode("/" + tableName + "s");
//判斷節(jié)點(diǎn)及其子節(jié)點(diǎn)是否為空,為空將返空值
if(rootNode == null) return null;
if(rootNode.ChildNodes.Count <= 0) return null;
///創(chuàng)建保存記錄的數(shù)據(jù)列
foreach(XmlAttribute attr in rootNode.ChildNodes[0].Attributes)
{
dt.Columns.Add(new DataColumn(attr.Name,typeof(string)));
}
///創(chuàng)新獲取數(shù)據(jù)節(jié)點(diǎn)的XPath
string xmlPath = "/" + tableName + "s/" + tableName;
//獲取XML節(jié)點(diǎn)下的所有節(jié)點(diǎn)
XmlNodeList nodeList = xmldoc.SelectNodes(xmlPath);
//遍歷所有節(jié)點(diǎn)
foreach(XmlNode node in nodeList)
{
//創(chuàng)建數(shù)據(jù)表行,并在其中添加數(shù)據(jù)
DataRow row = dt.NewRow();
foreach(DataColumn column in dt.Columns)
{ ///讀取每一個(gè)屬性
row[column.ColumnName] = node.Attributes[column.ColumnName].Value;
}
//將數(shù)據(jù)表行添加到數(shù)據(jù)表中
dt.Rows.Add(row);
}
//返回DataTable對(duì)象dt
return dt;

}

網(wǎng)站欄目:使用XmlDatabase讀取XML文件中的數(shù)據(jù)
標(biāo)題網(wǎng)址:http://www.muchs.cn/news6/40006.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google企業(yè)網(wǎng)站制作、網(wǎng)站導(dǎo)航、云服務(wù)器、網(wǎng)站收錄、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)