使用XmlDatabase向xml中添加數(shù)據(jù)的方法

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

public static int AddXmlData(string path,string tableName,params XmlParamter[] param)
{
//創(chuàng)建XmlDocument類(lèi)的實(shí)例
XmlDocument xmldoc = new XmlDocument();
//調(diào)用XmlDocument類(lèi)中的Load()方法加載XML文件
xmldoc.Load(path);
///選擇根節(jié)點(diǎn)
XmlNode node = xmldoc.SelectSingleNode("/" + tableName + "s");
if(node == null) return -1;

///創(chuàng)建新記錄的ID值
int newID;
if (node.LastChild == null)
{
newID = 1;
}
else
{
newID = DataTypeConvert.ConvertToInt(node.LastChild.Attributes["ID"].Value) + 1;
}
if(newID < 1) return -1;
///創(chuàng)建一個(gè)新節(jié)點(diǎn)
XmlNode newNode = xmldoc.CreateNode(XmlNodeType.Element, tableName, null);
if(newNode == null)return -1;
///添加ID的值
newNode.Attributes.Append(CreateNodeAttribute(xmldoc, "ID", newID.ToString()));
///添加新節(jié)點(diǎn)的屬性
foreach(XmlParamter p in param)
{
newNode.Attributes.Append(CreateNodeAttribute(xmldoc, p.Name, p.Value));
}
///將新節(jié)點(diǎn)追加到根節(jié)點(diǎn)中
node.AppendChild(newNode);
//保存XML文檔
xmldoc.Save(path);
return newID;

}

文章標(biāo)題:使用XmlDatabase向xml中添加數(shù)據(jù)的方法
轉(zhuǎn)載注明:http://www.muchs.cn/news7/40007.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站導(dǎo)航、動(dòng)態(tài)網(wǎng)站、定制網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、虛擬主機(jī)

廣告

聲明:本網(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)

小程序開(kāi)發(fā)