您好,登錄后才能下訂單哦!
小編給大家分享一下XML操作的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
在開發(fā)過程中對XML的使用不是太多,要用到時(shí)候也是想辦法繞過去,最近一個(gè)同事給了一個(gè)詳細(xì)的操作。分享一下
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml; namespace RenShiExport { /// <summary> /// XMLHelper XML文檔操作管理器 /// </summary> public class XMLHelper { public XMLHelper() { // // TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } #region XML文檔節(jié)點(diǎn)查詢和讀取 /// <summary> /// 選擇匹配XPath表達(dá)式的第一個(gè)節(jié)點(diǎn)XmlNode. /// </summary> /// <param name="xmlFileName">XML文檔完全文件名(包含物理路徑)</param> /// <param name="xpath">要匹配的XPath表達(dá)式(例如:"//節(jié)點(diǎn)名//子節(jié)點(diǎn)名")</param> /// <returns>返回XmlNode</returns> public static XmlNode GetXmlNodeByXpath(string xmlFileName, string xpath) { XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(xmlFileName); //加載XML文檔 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); return xmlNode; } catch (Exception ex) { return null; //throw ex; //這里可以定義你自己的異常處理 } } /// <summary> /// 選擇匹配XPath表達(dá)式的節(jié)點(diǎn)列表XmlNodeList. /// </summary> /// <param name="xmlFileName">XML文檔完全文件名(包含物理路徑)</param> /// <param name="xpath">要匹配的XPath表達(dá)式(例如:"//節(jié)點(diǎn)名//子節(jié)點(diǎn)名")</param> /// <returns>返回XmlNodeList</returns> public static XmlNodeList GetXmlNodeListByXpath(string xmlFileName, string xpath) { XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(xmlFileName); //加載XML文檔 XmlNodeList xmlNodeList = xmlDoc.SelectNodes(xpath); return xmlNodeList; } catch (Exception ex) { return null; //throw ex; //這里可以定義你自己的異常處理 } } /// <summary> /// 選擇匹配XPath表達(dá)式的第一個(gè)節(jié)點(diǎn)的匹配xmlAttributeName的屬性XmlAttribute. /// </summary> /// <param name="xmlFileName">XML文檔完全文件名(包含物理路徑)</param> /// <param name="xpath">要匹配的XPath表達(dá)式(例如:"//節(jié)點(diǎn)名//子節(jié)點(diǎn)名</param> /// <param name="xmlAttributeName">要匹配xmlAttributeName的屬性名稱</param> /// <returns>返回xmlAttributeName</returns> public static XmlAttribute GetXmlAttribute(string xmlFileName, string xpath, string xmlAttributeName) { string content = string.Empty; XmlDocument xmlDoc = new XmlDocument(); XmlAttribute xmlAttribute = null; try { xmlDoc.Load(xmlFileName); //加載XML文檔 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); if (xmlNode != null) { if (xmlNode.Attributes.Count > 0) { xmlAttribute = xmlNode.Attributes[xmlAttributeName]; } } } catch (Exception ex) { throw ex; //這里可以定義你自己的異常處理 } return xmlAttribute; } #endregion #region XML文檔創(chuàng)建和節(jié)點(diǎn)或?qū)傩缘奶砑印⑿薷? /// <summary> /// 創(chuàng)建一個(gè)XML文檔 /// </summary> /// <param name="xmlFileName">XML文檔完全文件名(包含物理路徑)</param> /// <param name="rootNodeName">XML文檔根節(jié)點(diǎn)名稱(須指定一個(gè)根節(jié)點(diǎn)名稱)</param> /// <param name="version">XML文檔版本號(必須為:"1.0")</param> /// <param name="encoding">XML文檔編碼方式</param> /// <param name="standalone">該值必須是"yes"或"no",如果為null,Save方法不在XML聲明上寫出獨(dú)立屬性</param> /// <returns>成功返回true,失敗返回false</returns> public static bool CreateXmlDocument(string xmlFileName, string rootNodeName, string version, string encoding, string standalone) { bool isSuccess = false; try { XmlDocument xmlDoc = new XmlDocument(); XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, standalone); XmlNode root = xmlDoc.CreateElement(rootNodeName); xmlDoc.AppendChild(xmlDeclaration); xmlDoc.AppendChild(root); xmlDoc.Save(xmlFileName); isSuccess = true; } catch (Exception ex) { throw ex; //這里可以定義你自己的異常處理 } return isSuccess; } /// <summary> /// 依據(jù)匹配XPath表達(dá)式的第一個(gè)節(jié)點(diǎn)來創(chuàng)建它的子節(jié)點(diǎn)(如果此節(jié)點(diǎn)已存在則追加一個(gè)新的同名節(jié)點(diǎn) /// </summary> /// <param name="xmlFileName">XML文檔完全文件名(包含物理路徑)</param> /// <param name="xpath">要匹配的XPath表達(dá)式(例如:"//節(jié)點(diǎn)名//子節(jié)點(diǎn)名</param> /// <param name="xmlNodeName">要匹配xmlNodeName的節(jié)點(diǎn)名稱</param> /// <param name="innerText">節(jié)點(diǎn)文本值</param> /// <param name="xmlAttributeName">要匹配xmlAttributeName的屬性名稱</param> /// <param name="value">屬性值</param> /// <returns>成功返回true,失敗返回false</returns> public static bool CreateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText, string xmlAttributeName, string value) { bool isSuccess = false; XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(xmlFileName); //加載XML文檔 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); if (xmlNode != null) { //存不存在此節(jié)點(diǎn)都創(chuàng)建 XmlElement subElement = xmlDoc.CreateElement(xmlNodeName); subElement.InnerXml = innerText; //如果屬性和值參數(shù)都不為空則在此新節(jié)點(diǎn)上新增屬性 if (!string.IsNullOrEmpty(xmlAttributeName) && !string.IsNullOrEmpty(value)) { XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName); xmlAttribute.Value = value; subElement.Attributes.Append(xmlAttribute); } xmlNode.AppendChild(subElement); } xmlDoc.Save(xmlFileName); //保存到XML文檔 isSuccess = true; } catch (Exception ex) { throw ex; //這里可以定義你自己的異常處理 } return isSuccess; } /// <summary> /// 依據(jù)匹配XPath表達(dá)式的第一個(gè)節(jié)點(diǎn)來創(chuàng)建或更新它的子節(jié)點(diǎn)(如果節(jié)點(diǎn)存在則更新,不存在則創(chuàng)建) /// </summary> /// <param name="xmlFileName">XML文檔完全文件名(包含物理路徑)</param> /// <param name="xpath">要匹配的XPath表達(dá)式(例如:"//節(jié)點(diǎn)名//子節(jié)點(diǎn)名</param> /// <param name="xmlNodeName">要匹配xmlNodeName的節(jié)點(diǎn)名稱</param> /// <param name="innerText">節(jié)點(diǎn)文本值</param> /// <returns>成功返回true,失敗返回false</returns> public static bool CreateOrUpdateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText) { bool isSuccess = false; bool isExistsNode = false;//標(biāo)識節(jié)點(diǎn)是否存在 XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(xmlFileName); //加載XML文檔 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); if (xmlNode != null) { //遍歷xpath節(jié)點(diǎn)下的所有子節(jié)點(diǎn) foreach (XmlNode node in xmlNode.ChildNodes) { if (node.Name.ToLower() == xmlNodeName.ToLower()) { //存在此節(jié)點(diǎn)則更新 node.InnerXml = innerText; isExistsNode = true; break; } } if (!isExistsNode) { //不存在此節(jié)點(diǎn)則創(chuàng)建 XmlElement subElement = xmlDoc.CreateElement(xmlNodeName); subElement.InnerXml = innerText; xmlNode.AppendChild(subElement); } } xmlDoc.Save(xmlFileName); //保存到XML文檔 isSuccess = true; } catch (Exception ex) { throw ex; //這里可以定義你自己的異常處理 } return isSuccess; } /// <summary> /// 依據(jù)匹配XPath表達(dá)式的第一個(gè)節(jié)點(diǎn)來創(chuàng)建或更新它的屬性(如果屬性存在則更新,不存在則創(chuàng)建) /// </summary> /// <param name="xmlFileName">XML文檔完全文件名(包含物理路徑)</param> /// <param name="xpath">要匹配的XPath表達(dá)式(例如:"//節(jié)點(diǎn)名//子節(jié)點(diǎn)名</param> /// <param name="xmlAttributeName">要匹配xmlAttributeName的屬性名稱</param> /// <param name="value">屬性值</param> /// <returns>成功返回true,失敗返回false</returns> public static bool CreateOrUpdateXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName, string value) { bool isSuccess = false; bool isExistsAttribute = false;//標(biāo)識屬性是否存在 XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(xmlFileName); //加載XML文檔 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); if (xmlNode != null) { //遍歷xpath節(jié)點(diǎn)中的所有屬性 foreach (XmlAttribute attribute in xmlNode.Attributes) { if (attribute.Name.ToLower() == xmlAttributeName.ToLower()) { //節(jié)點(diǎn)中存在此屬性則更新 attribute.Value = value; isExistsAttribute = true; break; } } if (!isExistsAttribute) { //節(jié)點(diǎn)中不存在此屬性則創(chuàng)建 XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName); xmlAttribute.Value = value; xmlNode.Attributes.Append(xmlAttribute); } } xmlDoc.Save(xmlFileName); //保存到XML文檔 isSuccess = true; } catch (Exception ex) { throw ex; //這里可以定義你自己的異常處理 } return isSuccess; } #endregion #region XML文檔節(jié)點(diǎn)或?qū)傩缘膭h除 /// <summary> /// 刪除匹配XPath表達(dá)式的第一個(gè)節(jié)點(diǎn)(節(jié)點(diǎn)中的子元素同時(shí)會被刪除) /// </summary> /// <param name="xmlFileName">XML文檔完全文件名(包含物理路徑)</param> /// <param name="xpath">要匹配的XPath表達(dá)式(例如:"//節(jié)點(diǎn)名//子節(jié)點(diǎn)名</param> /// <returns>成功返回true,失敗返回false</returns> public static bool DeleteXmlNodeByXPath(string xmlFileName, string xpath) { bool isSuccess = false; XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(xmlFileName); //加載XML文檔 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); if (xmlNode != null) { //刪除節(jié)點(diǎn) xmlNode.ParentNode.RemoveChild(xmlNode); } xmlDoc.Save(xmlFileName); //保存到XML文檔 isSuccess = true; } catch (Exception ex) { throw ex; //這里可以定義你自己的異常處理 } return isSuccess; } /// <summary> /// 刪除匹配XPath表達(dá)式的第一個(gè)節(jié)點(diǎn)中的匹配參數(shù)xmlAttributeName的屬性 /// </summary> /// <param name="xmlFileName">XML文檔完全文件名(包含物理路徑)</param> /// <param name="xpath">要匹配的XPath表達(dá)式(例如:"//節(jié)點(diǎn)名//子節(jié)點(diǎn)名</param> /// <param name="xmlAttributeName">要刪除的xmlAttributeName的屬性名稱</param> /// <returns>成功返回true,失敗返回false</returns> public static bool DeleteXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName) { bool isSuccess = false; bool isExistsAttribute = false; XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(xmlFileName); //加載XML文檔 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); XmlAttribute xmlAttribute = null; if (xmlNode != null) { //遍歷xpath節(jié)點(diǎn)中的所有屬性 foreach (XmlAttribute attribute in xmlNode.Attributes) { if (attribute.Name.ToLower() == xmlAttributeName.ToLower()) { //節(jié)點(diǎn)中存在此屬性 xmlAttribute = attribute; isExistsAttribute = true; break; } } if (isExistsAttribute) { //刪除節(jié)點(diǎn)中的屬性 xmlNode.Attributes.Remove(xmlAttribute); } } xmlDoc.Save(xmlFileName); //保存到XML文檔 isSuccess = true; } catch (Exception ex) { throw ex; //這里可以定義你自己的異常處理 } return isSuccess; } /// <summary> /// 刪除匹配XPath表達(dá)式的第一個(gè)節(jié)點(diǎn)中的所有屬性 /// </summary> /// <param name="xmlFileName">XML文檔完全文件名(包含物理路徑)</param> /// <param name="xpath">要匹配的XPath表達(dá)式(例如:"//節(jié)點(diǎn)名//子節(jié)點(diǎn)名</param> /// <returns>成功返回true,失敗返回false</returns> public static bool DeleteAllXmlAttributeByXPath(string xmlFileName, string xpath) { bool isSuccess = false; XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(xmlFileName); //加載XML文檔 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); if (xmlNode != null) { //遍歷xpath節(jié)點(diǎn)中的所有屬性 xmlNode.Attributes.RemoveAll(); } xmlDoc.Save(xmlFileName); //保存到XML文檔 isSuccess = true; } catch (Exception ex) { throw ex; //這里可以定義你自己的異常處理 } return isSuccess; } #endregion } }
以上是“XML操作的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。