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

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

public static int AddXmlData(string path,string tableName,params XmlParamter[] param)
{
//創(chuàng)建XmlDocument類的實(shí)例
XmlDocument xmldoc = new XmlDocument();
//調(diào)用XmlDocument類中的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;

}

文章題目:使用XmlDatabase向xml中添加數(shù)據(jù)的方法
文章出自:http://www.muchs.cn/news/40007.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、靜態(tài)網(wǎng)站商城網(wǎng)站、ChatGPT移動網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站

廣告

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

成都網(wǎng)站建設(shè)