XML 通用操作
Xml格式:
<?xml version="1.0" encoding="utf-8"?>
<remotes>
<remote ip="ipval">nameAndPwd</remote>
</remotes>
?
通用讀寫刪類:
using System;
using System.Data;
using System.Web;
using System.Xml;
?
public class xml_oper
{
??? private XmlDocument xmlDoc;
??? public xml_oper() { }
??? /// <summary>
??? /// 加載xml文件
??? /// </summary>
??? /// <param name="path">xml文件的物理路徑 </param>
??? private void LoadXml(string path, string node_root)
??? {
??????? xmlDoc = new XmlDocument();
??????? //判斷xml文件是否存在
??????? if (!System.IO.File.Exists(path))
??????? {
??????????? //創建xml 聲明節點
??????????? XmlNode xmlnode = xmlDoc.CreateNode(System.Xml.XmlNodeType.XmlDeclaration, "", "");
??????????? //添加上述創建和 xml聲明節點
??????????? xmlDoc.AppendChild(xmlnode);
??????????? //創建xml dbGuest 元素(根節點)
??????????? XmlElement xmlelem = xmlDoc.CreateElement("", node_root, "");
??????????? xmlDoc.AppendChild(xmlelem);
??????????? try
??????????? {
??????????????? xmlDoc.Save(path);
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? throw ex;
??????????? }
??????????? xmlDoc.Load(path);
??????? }
??????? else
??????? {
??????????? //加載xml文件
??????????? xmlDoc.Load(path);
??????? }
??? }
?
??? /// <summary>
??? /// 添加xml子節點
??? /// </summary>
??? /// <param name="path">xml文件的物理路徑 </param>
??? /// <param name="node_root">根節點名稱 </param>
??? /// <param name="node_name">添加的子節點名稱 </param>
??? /// <param name="node_text">子節點文本 </param>
??? public void addElement(string path, string node_root, string node_name, string node_text, string att_name, string att_value)
??? {
??????? LoadXml(path, node_root);
?
??????? XmlNodeList nodeList = xmlDoc.SelectSingleNode(node_root).ChildNodes;//獲取bookstore節點的所有子節點
??????? //判斷是否有節點,有節點就遍歷所有子節點,看看有沒有重復節點,沒節點就添加一個新節點
??????? if (nodeList.Count > 0)
??????? {
??????????? foreach (XmlNode xn in nodeList)//遍歷所有子節點
??????????? {
??????????????? XmlElement xe = (XmlElement)xn;//將子節點類型轉換為XmlElement類型
??????????????? if (xe.GetAttribute(att_name) != att_value)
??????????????? {
??????????????????? XmlNode xmldocSelect = xmlDoc.SelectSingleNode(node_root);? //選中根節點
??????????????????? XmlElement son_node = xmlDoc.CreateElement(node_name);??? //添加子節點
??????????????????? son_node.SetAttribute(att_name, att_value);??? //設置屬性
??????????????????? son_node.InnerText = node_text;??? //添加節點文本
??????????????????? xmldocSelect.AppendChild(son_node);????? //添加子節點
??????????????????? xmlDoc.Save(path);????????? //保存xml文件
??????????????????? break;
??????????????? }
??????????? }
?
??????? }
??????? else
??????? {
??????????? XmlNode xmldocSelect = xmlDoc.SelectSingleNode(node_root);? //選中根節點
??????????? XmlElement son_node = xmlDoc.CreateElement(node_name);??? //添加子節點
??????????? son_node.SetAttribute(att_name, att_value);??? //設置屬性
??????????? son_node.InnerText = node_text;??? //添加節點文本
??????????? xmldocSelect.AppendChild(son_node);????? //添加子節點
??????????? xmlDoc.Save(path);????????? //保存xml文件
??????? }
??? }
?
??? /// <summary>
??? /// 修改節點的內容
??? /// </summary>
??? /// <param name="path">xml文件的物理路徑 </param>
??? /// <param name="node_root">根節點名稱 </param>
??? /// <param name="new_text">節點的新內容 </param>
??? /// <param name="att_name">節點的屬性名 </param>
??? /// <param name="att_value">節點的屬性值 </param>
??? public void UpdateElement(string path, string node_root, string new_text, string att_name, string att_value)
??? {
??????? LoadXml(path, node_root);
??????? XmlNodeList nodeList = xmlDoc.SelectSingleNode(node_root).ChildNodes;//獲取bookstore節點的所有子節點
??????? foreach (XmlNode xn in nodeList)//遍歷所有子節點
??????? {
??????????? XmlElement xe = (XmlElement)xn;//將子節點類型轉換為XmlElement類型
??????????? if (xe.GetAttribute(att_name) == att_value)
??????????? {
??????????????? xe.InnerText = new_text;??? //內容賦值
??????????????? xmlDoc.Save(path);//保存
??????????????? break;
??????????? }
??????? }
?
??? }
?
??? /// <summary>
??? /// 刪除節點
??? /// </summary>
??? /// <param name="path">xml文件的物理路徑 </param>
??? /// <param name="node_root">根節點名稱 </param>
??? /// <param name="att_name">節點的屬性名 </param>
??? /// <param name="att_value">節點的屬性值 </param>
??? public void deleteNode(string path, string node_root, string att_name, string att_value)
??? {
?
??????? LoadXml(path, node_root);
?
??????? XmlNodeList nodeList = xmlDoc.SelectSingleNode(node_root).ChildNodes;
??????? XmlNode root = xmlDoc.SelectSingleNode(node_root);
?
??????? foreach (XmlNode xn in nodeList)
??????? {
??????????? XmlElement xe = (XmlElement)xn;
?
??????????? if (xe.GetAttribute(att_name) == att_value)
??????????? {
??????????????? //xe.RemoveAttribute("name");//刪除name屬性
??????????????? xe.RemoveAll();//刪除該節點的全部內容
??????????????? root.RemoveChild(xe);
??????????????? xmlDoc.Save(path);//保存
??????????????? break;
??????????? }
?
??????? }
??? }
}
讀取方法:
// 讀取xml
XmlDocument doc = new XmlDocument();
doc.Load("DB.xml");
XmlNodeList nodes = doc.SelectSingleNode("remotes").ChildNodes;
foreach (XmlElement item in nodes)
{
??? string ipval = item.Attributes["ip"].Value; // ipval
??? string text = item.InnerText;?????????????? // nameAndPwd
}
【Stone 制作整理,引用請寫明出處謝謝合作,聯系QQ:1370569】
總結
- 上一篇: Android Call require
- 下一篇: Asp.Net回车键触发Button的O