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

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

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

}

網(wǎng)頁題目:使用XmlDatabase讀取XML文件中的數(shù)據(jù)
轉(zhuǎn)載源于:http://www.bm7419.com/news/40006.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、App設(shè)計、面包屑導(dǎo)航網(wǎng)站營銷、營銷型網(wǎng)站建設(shè)、網(wǎng)站制作

廣告

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

成都網(wǎng)頁設(shè)計公司