1、 認識xml
可擴展標記語言,一種用于標記電子文檔使其具有結果性的標記語言,它可以用來標記數據、定義數據類型,是一種允許用戶對自己的標記語言進行定義的源語言。
2、 和超文本標記語言區別
2.1 html不一定需要成對出現,xml則一定需要成對出現。
2.2 html 不區分大小寫,但是xml區分。
3、對xml文檔增刪改查
1 //聲明一個XmlDocument空對象 2 PRotected XmlDocument XmlDoc = new XmlDocument(); 3 /// <summary> 4 /// 構造函數,導入xml文件 5 /// </summary> 6 /// <param name="path"></param> 7 public XmlHelper(string path) 8 { 9 try10 {11 XmlDoc.Load(path);12 }13 catch (Exception ex)14 {15 throw ex;16 }17 }18 /// <summary>19 /// 保存文件20 /// </summary>21 /// <param name="path"></param>22 public void SaveXml(string path)23 {24 try25 {26 XmlDoc.Save(path);27 }28 catch (System.Exception ex)29 {30 throw ex;31 }32 }
1 /// <summary> 2 /// 獲取節點的子節點的內容 3 /// </summary> 4 /// <param name="path"></param> 5 /// <param name="rootNode"></param> 6 /// <param name="attributeName"></param> 7 /// <returns></returns> 8 public string GetNodeChildAttribute(string path, string rootNode, string attributeName) 9 {10 XmlNode xn = XmlDoc.SelectSingleNode(rootNode);11 StringBuilder sb = new StringBuilder();12 XmlNodeList xnl = xn.ChildNodes;13 14 foreach (XmlNode xnf in xnl)15 {16 XmlElement xe = (XmlElement)xnf;17 XmlNodeList xnf1 = xe.ChildNodes;18 foreach (XmlNode xn2 in xnf1)19 {20 if (xn2.Name == attributeName)21 {22 sb.Append(xn2.InnerText);//顯示子節點點文本23 }24 }25 }26 return sb.ToString();27 } 1 /// <summary> 2 /// 獲取節點的屬性值 3 /// </summary> 4 /// <param name="path">xml路徑</param> 5 /// <param name="rootNode">根節點名稱</param> 6 /// <param name="attributeName">屬性名稱</param> 7 /// <returns></returns> 8 public string GetNodeAttribute(string path, string rootNode, string attributeName) 9 {10 try11 {12 XmlNode xn = XmlDoc.SelectSingleNode(rootNode);13 XmlNodeList xnl = xn.ChildNodes;14 StringBuilder sb = new StringBuilder();15 foreach (XmlNode xnf in xnl)16 {17 XmlElement xe = (XmlElement)xnf;18 sb.Append(xe.GetAttribute(attributeName));19 }20 return sb.ToString();21 }22 catch (Exception)23 {24 25 throw;26 }27 } 1 /// <summary> 2 /// 刪除節點/節點屬性 3 /// </summary> 4 /// <param name="path">xml文件地址</param> 5 /// <param name="rootNode">根節點名稱</param> 6 /// <param name="delNode">要刪除的節點</param> 7 /// <param name="attributeName">節點屬性</param> 8 /// <param name="attributeValue">屬性值</param> 9 public void DeleteNode(string path, string rootNode, string attributeName, string attributeValue)10 {11 try12 {13 XmlNodeList xnl = XmlDoc.SelectSingleNode(rootNode).ChildNodes;14 foreach (XmlNode xn in xnl)15 {16 XmlElement xe = (XmlElement)xn;17 if (xe.GetAttribute(attributeName) == attributeValue)18 {19 //xe.RemoveAttribute(attributeName);//刪除屬性20 xe.RemoveAll();//刪除該節點的全部內容21 }22 }23 SaveXml(path);24 }25 catch (Exception)26 {27 28 throw;29 }30 } 1 /// <summary> 2 /// 修改節點的子節點內容 3 /// </summary> 4 /// <param name="path">xml文件路徑</param> 5 /// <param name="rootNode">根節點名稱</param> 6 /// <param name="attributeName">節點的子節點名稱</param> 7 /// <param name="attributeOldValue">節點的子節點原始內容</param> 8 /// <param name="attributeNewValue">節點的子節點新內容</param> 9 public void UpdateChildNodeAttribute(string path, string rootNode, string attributeName, string attributeOldValue, string attributeNewValue)10 {11 try12 {13 XmlNodeList nodeList = XmlDoc.SelectSingleNode(rootNode).ChildNodes;//獲取根節點的所有子節點14 foreach (XmlNode xn in nodeList)//遍歷所有子節點15 {16 XmlElement xe = (XmlElement)xn;//將子節點類型轉換為XmlElement類型17 if (string.IsNullOrEmpty(attributeName) || string.IsNullOrEmpty(attributeOldValue))18 {19 return;20 }21 else22 {23 XmlNodeList nls = xe.ChildNodes;24 if (nls != null && nls.Count > 0)25 {26 foreach (XmlNode xn1 in nls)//遍歷27 {28 XmlElement xe2 = (XmlElement)xn1;//轉換類型29 if (xe2.InnerText == attributeOldValue)//如果找到30 {31 xe2.InnerText = attributeNewValue;//則修改32 break;//找到退出來就可以了33 }34 }35 }36 }37 }38 SaveXml(path);39 }40 catch (Exception)41 {42 43 throw;44 }45 } 1 /// <summary> 2 /// 修改節點屬性值操作 3 /// </summary> 4 /// <param name="path">xml文件路徑</param> 5 /// <param name="rootNode">根節點名稱,如:bookstore</param> 6 /// <param name="attributeName">節點屬性名</param> 7 /// <param name="attributeOldValue">節點屬性原來值</param> 8 /// <param name="attributeNewValue">節點屬性修改后的值</param> 9 public void UpdateNodeAttribute(string path, string rootNode, string attributeName, string attributeOldValue, string attributeNewValue)10 {11 try12 {13 XmlNodeList nodeList = XmlDoc.SelectSingleNode(rootNode).ChildNodes;//獲取根節點的所有子節點14 foreach (XmlNode xn in nodeList)//遍歷所有子節點15 {16 XmlElement xe = (XmlElement)xn;//將子節點類型專程xmlelement類型17 if (string.IsNullOrEmpty(attributeName) || string.IsNullOrEmpty(attributeOldValue))18 {19 return;20 }21 else22 {23 if (xe.GetAttribute(attributeName) == attributeOldValue)24 {25 xe.SetAttribute(attributeName, attributeNewValue);26 }27 }28 }29 SaveXml(path);30 }31 catch (Exception)32 {33 34 throw;35 }36 }1 /// <summary> 2 /// 插入節點操作 3 /// </summary> 4 /// <param name="path">xml文件路徑</param> 5 /// <param name="rootN
新聞熱點
疑難解答