xml被設計用來傳輸和存儲數據。(html被設計用來顯示數據)html
C#操做XML的基本操做:增刪改查node
demo示例代碼:程序員
<?xml version="1.0" encoding="utf-8"> <info> <userinfo> <name>花哥</name> <age>26</age> <email>921442206@qq.com</email> <website>51chiheng.com</website> </userinfo> </info>
增長:web
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("myfolder/userinfo.xml"));//加載xml文件
XmlNode root = xmld.SelectSingleNode("info");//查找info根節點
XmlElement xml0 = xmld.CreateElement("userinfo");
xml0.SetAttribute("realname", "花**");
XmlElement xml1 = xmld.CreateElement("age");//建立一個age節點
xml1.InnerText ="26";
xml0.AppendChild(xml1);//添加
XmlElement xml2 = xmld.CreateElement("email");//建立emial節點
xml2.InnerText ="921442206@qq.com";
xml0.AppendChild(xml2);//添加
XmlElement xml3 = xmld.CreateElement("website");//建立一個website節點
xml3.InnerText = "51chiheng.com";
xml0.AppendChild(xml3);//添加
root.AppendChild(xml0);
xmld.Save(Server.MapPath("myfolder/userinfo.xml"));
刪除操做:數據庫
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("myfolder/userinfo.xml"));
XmlNodeList list = xmld.SelectSingleNode("info").ChildNodes;
foreach (XmlNode var in list)
{
XmlElement xmle = (XmlElement)var;
if (xmle.GetAttribute("readname") == "花**")
{
xmle.RemoveAll();
xmld.Save(Server.MapPath("myfolder/userinfo.xml"));//刪除後記得必定要保存
Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('刪除成功!!');", true);
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('刪除失敗!!');", true);
}
}
修改操做:數組
XmlDocument xmld = new XmlDocument(); xmld.Load(Server.MapPath("App_Code/Message.xml"));
XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
foreach (XmlNode xmln in list)
{
XmlElement xmle = (XmlElement)xmln;//格式轉換
if ("花**".Equals(xmle.GetAttribute("readname")))
{
xmle.ChildNodes[0].InnerText = "111";
xmle.ChildNodes[1].InnerText = "222";
xmle.ChildNodes[2].InnerText = "333";
xmle.ChildNodes[3].InnerText = "444";
xmld.Save(Server.MapPath("myfolder/userinfo.xml"));
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改爲功!!!');location.href='index.aspx'",true);
}
}
查詢操做:架構
//下面的代碼開始是用頁面編碼的方式實現的
XmlDocument xmld = new XmlDocument();//構建xml文檔
xmld.Load(Server.MapPath("App_Code/Message.xml"));//加載
XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;//獲取子節點
if (list!=null)
{
foreach (XmlNode node in list)
{
XmlElement xmle = (XmlElement)node;//轉換
//todo....
}
}
最後分享一點乾貨,聰明的程序員會學會站在巨人的肩膀上去眺望遠方,而不是一直悶頭苦幹的造輪胎,感謝蘇飛大神!ide
/// <summary> /// 好東西分享: xml幫助類大全 /// 類說明:【感謝蘇飛大俠分享的好東西】 /// </summary> using System; using System.Data; using System.IO; using System.Xml; namespace DotNet.Utilities { public class XMLProcess { #region 構造函數 public XMLProcess() { } public XMLProcess(string strPath) { this._XMLPath = strPath; } #endregion #region 公有屬性 private string _XMLPath; public string XMLPath { get { return this._XMLPath; } } #endregion #region 私有方法 /// <summary> /// 導入XML文件 /// </summary> /// <param name="XMLPath">XML文件路徑</param> private XmlDocument XMLLoad() { string XMLFile = XMLPath; XmlDocument xmldoc = new XmlDocument(); try { string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLFile; if (File.Exists(filename)) xmldoc.Load(filename); } catch (Exception e) { } return xmldoc; } /// <summary> /// 導入XML文件 /// </summary> /// <param name="XMLPath">XML文件路徑</param> private static XmlDocument XMLLoad(string strPath) { XmlDocument xmldoc = new XmlDocument(); try { string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + strPath; if (File.Exists(filename)) xmldoc.Load(filename); } catch (Exception e) { } return xmldoc; } /// <summary> /// 返回完整路徑 /// </summary> /// <param name="strPath">Xml的路徑</param> private static string GetXmlFullPath(string strPath) { if (strPath.IndexOf(":") > 0) { return strPath; } else { return System.Web.HttpContext.Current.Server.MapPath(strPath); } } #endregion #region 讀取數據 /// <summary> /// 讀取指定節點的數據 /// </summary> /// <param name="node">節點</param> /// 使用示列: /// XMLProsess.Read("/Node", "") /// XMLProsess.Read("/Node/Element[@Attribute='Name']") public string Read(string node) { string value = ""; try { XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); value = xn.InnerText; } catch { } return value; } /// <summary> /// 讀取指定路徑和節點的串聯值 /// </summary> /// <param name="path">路徑</param> /// <param name="node">節點</param> /// <param name="attribute">屬性名,非空時返回該屬性值,不然返回串聯值</param> /// 使用示列: /// XMLProsess.Read(path, "/Node", "") /// XMLProsess.Read(path, "/Node/Element[@Attribute='Name']") public static string Read(string path, string node) { string value = ""; try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); value = xn.InnerText; } catch { } return value; } /// <summary> /// 讀取指定路徑和節點的屬性值 /// </summary> /// <param name="path">路徑</param> /// <param name="node">節點</param> /// <param name="attribute">屬性名,非空時返回該屬性值,不然返回串聯值</param> /// 使用示列: /// XMLProsess.Read(path, "/Node", "") /// XMLProsess.Read(path, "/Node/Element[@Attribute='Name']", "Attribute") public static string Read(string path, string node, string attribute) { string value = ""; try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value); } catch { } return value; } /// <summary> /// 獲取某一節點的全部孩子節點的值 /// </summary> /// <param name="node">要查詢的節點</param> public string[] ReadAllChildallValue(string node) { int i = 0; string[] str = { }; XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); XmlNodeList nodelist = xn.ChildNodes; //獲得該節點的子節點 if (nodelist.Count > 0) { str = new string[nodelist.Count]; foreach (XmlElement el in nodelist)//讀元素值 { str[i] = el.Value; i++; } } return str; } /// <summary> /// 獲取某一節點的全部孩子節點的值 /// </summary> /// <param name="node">要查詢的節點</param> public XmlNodeList ReadAllChild(string node) { XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); XmlNodeList nodelist = xn.ChildNodes; //獲得該節點的子節點 return nodelist; } /// <summary> /// 讀取XML返回經排序或篩選後的DataView /// </summary> /// <param name="strWhere">篩選條件,如:"name='kgdiwss'"</param> /// <param name="strSort"> 排序條件,如:"Id desc"</param> public DataView GetDataViewByXml(string strWhere, string strSort) { try { string XMLFile = this.XMLPath; string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLFile; DataSet ds = new DataSet(); ds.ReadXml(filename); DataView dv = new DataView(ds.Tables[0]); //建立DataView來完成排序或篩選操做 if (strSort != null) { dv.Sort = strSort; //對DataView中的記錄進行排序 } if (strWhere != null) { dv.RowFilter = strWhere; //對DataView中的記錄進行篩選,找到咱們想要的記錄 } return dv; } catch (Exception) { return null; } } /// <summary> /// 讀取XML返回DataSet /// </summary> /// <param name="strXmlPath">XML文件相對路徑</param> public DataSet GetDataSetByXml(string strXmlPath) { try { DataSet ds = new DataSet(); ds.ReadXml(GetXmlFullPath(strXmlPath)); if (ds.Tables.Count > 0) { return ds; } return null; } catch (Exception) { return null; } } #endregion #region 插入數據 /// <summary> /// 插入數據 /// </summary> /// <param name="path">路徑</param> /// <param name="node">節點</param> /// <param name="element">元素名,非空時插入新元素,不然在該元素中插入屬性</param> /// <param name="attribute">屬性名,非空時插入該元素屬性值,不然插入元素值</param> /// <param name="value">值</param> /// 使用示列: /// XMLProsess.Insert(path, "/Node", "Element", "", "Value") /// XMLProsess.Insert(path, "/Node", "Element", "Attribute", "Value") /// XMLProsess.Insert(path, "/Node", "", "Attribute", "Value") public static void Insert(string path, string node, string element, string attribute, string value) { try { XmlDocument doc = new XmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); XmlNode xn = doc.SelectSingleNode(node); if (element.Equals("")) { if (!attribute.Equals("")) { XmlElement xe = (XmlElement)xn; xe.SetAttribute(attribute, value); } } else { XmlElement xe = doc.CreateElement(element); if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); xn.AppendChild(xe); } doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// <summary> /// 插入數據 /// </summary> /// <param name="path">路徑</param> /// <param name="node">節點</param> /// <param name="element">元素名,非空時插入新元素,不然在該元素中插入屬性</param> /// <param name="strList">由XML屬性名和值組成的二維數組</param> public static void Insert(string path, string node, string element, string[][] strList) { try { XmlDocument doc = new XmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = doc.CreateElement(element); string strAttribute = ""; string strValue = ""; for (int i = 0; i < strList.Length; i++) { for (int j = 0; j < strList[i].Length; j++) { if (j == 0) strAttribute = strList[i][j]; else strValue = strList[i][j]; } if (strAttribute.Equals("")) xe.InnerText = strValue; else xe.SetAttribute(strAttribute, strValue); } xn.AppendChild(xe); doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// <summary> /// 插入一行數據 /// </summary> /// <param name="strXmlPath">XML文件相對路徑</param> /// <param name="Columns">要插入行的列名數組,如:string[] Columns = {"name","IsMarried"};</param> /// <param name="ColumnValue">要插入行每列的值數組,如:string[] ColumnValue={"XML大全","false"};</param> /// <returns>成功返回true,不然返回false</returns> public static bool WriteXmlByDataSet(string strXmlPath, string[] Columns, string[] ColumnValue) { try { //根據傳入的XML路徑獲得.XSD的路徑,兩個文件放在同一個目錄下 string strXsdPath = strXmlPath.Substring(0, strXmlPath.IndexOf(".")) + ".xsd"; DataSet ds = new DataSet(); ds.ReadXmlSchema(GetXmlFullPath(strXsdPath)); //讀XML架構,關係到列的數據類型 ds.ReadXml(GetXmlFullPath(strXmlPath)); DataTable dt = ds.Tables[0]; DataRow newRow = dt.NewRow(); //在原來的表格基礎上建立新行 for (int i = 0; i < Columns.Length; i++) //循環給一行中的各個列賦值 { newRow[Columns[i]] = ColumnValue[i]; } dt.Rows.Add(newRow); dt.AcceptChanges(); ds.AcceptChanges(); ds.WriteXml(GetXmlFullPath(strXmlPath)); return true; } catch (Exception) { return false; } } #endregion #region 修改數據 /// <summary> /// 修改指定節點的數據 /// </summary> /// <param name="node">節點</param> /// <param name="value">值</param> public void Update(string node, string value) { try { XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); xn.InnerText = value; doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLPath); } catch { } } /// <summary> /// 修改指定節點的數據 /// </summary> /// <param name="path">路徑</param> /// <param name="node">節點</param> /// <param name="value">值</param> /// 使用示列: /// XMLProsess.Insert(path, "/Node","Value") /// XMLProsess.Insert(path, "/Node","Value") public static void Update(string path, string node, string value) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); xn.InnerText = value; doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// <summary> /// 修改指定節點的屬性值(靜態) /// </summary> /// <param name="path">路徑</param> /// <param name="node">節點</param> /// <param name="attribute">屬性名,非空時修改該節點屬性值,不然修改節點值</param> /// <param name="value">值</param> /// 使用示列: /// XMLProsess.Insert(path, "/Node", "", "Value") /// XMLProsess.Insert(path, "/Node", "Attribute", "Value") public static void Update(string path, string node, string attribute, string value) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// <summary> /// 更改符合條件的一條記錄 /// </summary> /// <param name="strXmlPath">XML文件路徑</param> /// <param name="Columns">列名數組</param> /// <param name="ColumnValue">列值數組</param> /// <param name="strWhereColumnName">條件列名</param> /// <param name="strWhereColumnValue">條件列值</param> public static bool UpdateXmlRow(string strXmlPath, string[] Columns, string[] ColumnValue, string strWhereColumnName, string strWhereColumnValue) { try { string strXsdPath = strXmlPath.Substring(0, strXmlPath.IndexOf(".")) + ".xsd"; DataSet ds = new DataSet(); ds.ReadXmlSchema(GetXmlFullPath(strXsdPath));//讀XML架構,關係到列的數據類型 ds.ReadXml(GetXmlFullPath(strXmlPath)); //先判斷行數 if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { //若是當前記錄爲符合Where條件的記錄 if (ds.Tables[0].Rows[i][strWhereColumnName].ToString().Trim().Equals(strWhereColumnValue)) { //循環給找到行的各列賦新值 for (int j = 0; j < Columns.Length; j++) { ds.Tables[0].Rows[i][Columns[j]] = ColumnValue[j]; } ds.AcceptChanges(); //更新DataSet ds.WriteXml(GetXmlFullPath(strXmlPath));//從新寫入XML文件 return true; } } } return false; } catch (Exception) { return false; } } #endregion #region 刪除數據 /// <summary> /// 刪除節點值 /// </summary> /// <param name="path">路徑</param> /// <param name="node">節點</param> /// <param name="attribute">屬性名,非空時刪除該節點屬性值,不然刪除節點值</param> /// <param name="value">值</param> /// 使用示列: /// XMLProsess.Delete(path, "/Node", "") /// XMLProsess.Delete(path, "/Node", "Attribute") public static void Delete(string path, string node) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); xn.ParentNode.RemoveChild(xn); doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// <summary> /// 刪除數據 /// </summary> /// <param name="path">路徑</param> /// <param name="node">節點</param> /// <param name="attribute">屬性名,非空時刪除該節點屬性值,不然刪除節點值</param> /// <param name="value">值</param> /// 使用示列: /// XMLProsess.Delete(path, "/Node", "") /// XMLProsess.Delete(path, "/Node", "Attribute") public static void Delete(string path, string node, string attribute) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xn.ParentNode.RemoveChild(xn); else xe.RemoveAttribute(attribute); doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// <summary> /// 刪除全部行 /// </summary> /// <param name="strXmlPath">XML路徑</param> public static bool DeleteXmlAllRows(string strXmlPath) { try { DataSet ds = new DataSet(); ds.ReadXml(GetXmlFullPath(strXmlPath)); if (ds.Tables[0].Rows.Count > 0) { ds.Tables[0].Rows.Clear(); } ds.WriteXml(GetXmlFullPath(strXmlPath)); return true; } catch (Exception) { return false; } } /// <summary> /// 經過刪除DataSet中指定索引行,重寫XML以實現刪除指定行 /// </summary> /// <param name="iDeleteRow">要刪除的行在DataSet中的Index值</param> public static bool DeleteXmlRowByIndex(string strXmlPath, int iDeleteRow) { try { DataSet ds = new DataSet(); ds.ReadXml(GetXmlFullPath(strXmlPath)); if (ds.Tables[0].Rows.Count > 0) { ds.Tables[0].Rows[iDeleteRow].Delete(); } ds.WriteXml(GetXmlFullPath(strXmlPath)); return true; } catch (Exception) { return false; } } /// <summary> /// 刪除指定列中指定值的行 /// </summary> /// <param name="strXmlPath">XML相對路徑</param> /// <param name="strColumn">列名</param> /// <param name="ColumnValue">指定值</param> public static bool DeleteXmlRows(string strXmlPath, string strColumn, string[] ColumnValue) { try { DataSet ds = new DataSet(); ds.ReadXml(GetXmlFullPath(strXmlPath)); if (ds.Tables[0].Rows.Count > 0) { //判斷行多仍是刪除的值多,多的for循環放在裏面 if (ColumnValue.Length > ds.Tables[0].Rows.Count) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { for (int j = 0; j < ColumnValue.Length; j++) { if (ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j])) { ds.Tables[0].Rows[i].Delete(); } } } } else { for (int j = 0; j < ColumnValue.Length; j++) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if (ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j])) { ds.Tables[0].Rows[i].Delete(); } } } } ds.WriteXml(GetXmlFullPath(strXmlPath)); } return true; } catch (Exception) { return false; } } #endregion } }