C#操作XML文件實例匯總
更新時間:2014年08月20日 17:30:46 投稿:shichen2014
這篇文章主要介紹了C#操作xml文件實例,包括了對XML文件節(jié)點的查找、遍歷、刪除、添加等。是C#程序設(shè)計中非常重要的技巧,需要的朋友可以參考下
針對XML文件的操作是C#程序設(shè)計中非常常見的功能。本文即以實例展示了C#操作XML文件的幾個常見的示例。具體如下:
1.返回節(jié)點下標(biāo)
public static XmlDocument getDoc(String path)//加載xml文檔
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
return doc;
}
/// <summary>
/// 返回找到的節(jié)點下標(biāo)
/// </summary>
/// <param name="path">xml文件路徑</param>
/// <param name="bname">書名</param>
/// <returns></returns>
public static int getPosition(String path,string node, String bname)
{
int i;
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;
for (i = 0; i < nodeList.Count; i++)
{
if (nodeList[i].ChildNodes[0].InnerText == bname)
{
return i;
}
}
return -1;
}
2.遍歷數(shù)據(jù)
/// <summary>
/// 遍歷數(shù)據(jù)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnRead_Click(object sender, EventArgs e)
{
XmlDocument doc = getDoc("books.xml");//加載xml文檔,books文件存放于
foreach (XmlElement root in doc.DocumentElement.ChildNodes)//遍歷根的子節(jié)點
{
Response.Write("<script>document.write('<br/>');</script>");
Response.Write("<script>document.write('" + root.Name + "');</script>");//節(jié)點名root.Name book
foreach (XmlElement item in root.ChildNodes)
{
Response.Write("<script>document.write('" + item.Name + "'+':'+'" + item.InnerText + "');</script>");//輸出節(jié)點名和文本節(jié)點的值
Response.Write("<script>document.write('<br/>');</script>");
}
}
}
3.查找
/// <summary>
/// 查找
/// </summary>
/// <param name="path">文件路徑</param>
/// <param name="node">節(jié)點</param>
/// <param name="bname">查找關(guān)鍵字</param>
/// <returns>XmlNode</returns>
public static XmlNode Find(string path,string node,string bname)
{
XmlDocument doc = new XmlDocument();
doc.Load(path);//加載xml文檔
XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;
int i = getPosition(path, node, bname);//
if (i >= 0)
return nodeList[i];
else
return null;
}
4.刪除節(jié)點
/// <summary>
/// 刪除元素、屬性
/// </summary>
/// <param name="path">文件</param>
/// <param name="node">指定節(jié)點的父節(jié)點</param>
/// <param name="attribute">為空時刪除節(jié)點,否則刪除屬性</param>
/// <param name="bname"></param>
public static void Delete(string path,string node,string attribute,string bname)
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode root = doc.SelectSingleNode(node);
XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;
int i = getPosition(path, node, bname);//返回指定的節(jié)點下標(biāo)
if (i >= 0)
{
if (attribute.Equals(""))
{
root.RemoveChild(nodeList[i]);
}
else
{
XmlElement xn = (XmlElement)nodeList[i];
xn.RemoveAttribute(attribute);
}
}
doc.Save(path);
}
5.添加
/// <summary>
/// 添加元素值
/// </summary>
/// <param name="path"></param>
/// <param name="node"></param>
/// <param name="element"></param>
/// <param name="value"></param>
/// <param name="i">插入的下標(biāo),如果為負(fù),默認(rèn)從最后一個節(jié)點插入</param>
/// <returns></returns>
public static bool Add(string path,string node,string element,string value,int i)
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList nodeList = doc.SelectNodes(node);
XmlNode newNode = doc.SelectSingleNode(node).LastChild;
if (i < 0 || i > nodeList.Count-1)//如果小于0或者大于節(jié)點長度,默認(rèn)從最后一個節(jié)點添加
{
XmlElement newElement = doc.CreateElement(element);//創(chuàng)建元素
newElement.InnerText = value;//賦值
newNode.AppendChild(newElement);
}
else
{
XmlElement newElement = doc.CreateElement(element);
newElement.InnerText = value;
nodeList[i - 1].AppendChild(newElement);
}
doc.Save(path);
return true;
}
希望本文所述方法對大家的C#程序設(shè)計能有所幫助。
相關(guān)文章
C#實現(xiàn)日期格式轉(zhuǎn)換的公共方法類實例
這篇文章主要介紹了C#實現(xiàn)日期格式轉(zhuǎn)換的公共方法類,結(jié)合完整實例形式分析了C#針對各種常見日期格式的轉(zhuǎn)換方法,涉及C#字符串、日期、時間相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
淺談Visual Studio 2019 Vue項目的目錄結(jié)構(gòu)
這篇文章主要介紹了Visual Studio 2019 Vue項目 目錄結(jié)構(gòu),本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
在C#中基于Semantic?Kernel的檢索增強生成(RAG)實踐記錄
SemanticKernel是一個用于集成和操作大語言模型的應(yīng)用程序框架,支持C#、Python和Java等多種編程語言,通過SemanticKernel,開發(fā)者可以輕松構(gòu)建基于最新AI技術(shù)的應(yīng)用程序2024-10-10
c# winform 關(guān)閉窗體時同時結(jié)束線程實現(xiàn)思路
th.IsBackground = true解決線程問題,意思就是把線程設(shè)置為后臺線程,感興趣的朋友可以多了解下,如何有什么妙招還請多多指導(dǎo)哈2013-02-02

