using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Windows.Forms; namespace MyCommanHelper { public class XMLHelper { #region 读取节点值 /// <summary> /// 读取节点值 /// </summary> /// <param name="doc"></param> /// <param name="Section"></param> /// <param name="Key"></param> /// <returns></returns> public static string XmlReadValue(XmlDocument doc, string Section, string Key) { XmlNode result = doc.SelectSingleNode(Section); string ss = ""; if (null != result) { ss = result.SelectSingleNode(Key).InnerText; } return ss; } /// <summary> /// 获取二层小结下的键值 /// </summary> /// <param name="doc"></param> /// <param name="Section"></param> /// <param name="subSection"></param> /// <param name="Key"></param> /// <returns></returns> public static string XmlReadValue(XmlDocument doc, string Section, string subSection, string Key) { try { XmlNode result = doc.SelectSingleNode(Section); if (null != result) { XmlNodeList childnodes = result.ChildNodes; for (int i = 0; i < childnodes.Count; i++) { XmlNode node = childnodes[i]; if (node.LocalName.Equals(subSection)) { return node.SelectSingleNode(Key).InnerText; } } } return ""; } catch { return ""; } } /// <summary> /// 获取三层小结下的键值 /// </summary> /// <param name="doc"></param> /// <param name="Section"></param> /// <param name="subSection"></param> /// <param name="sub2Section"></param> /// <param name="Key"></param> /// <returns></returns> public static string XmlReadValue(XmlDocument doc, string Section, string subSection, string sub2Section, string Key) { try { XmlNode result = doc.SelectSingleNode(Section); if (null != result) { XmlNodeList parentNodes = result.ChildNodes; for (int i = 0; i < parentNodes.Count; i++) { XmlNode node = parentNodes[i]; if (node.LocalName.Equals(subSection)) { XmlNodeList childNodes = node.ChildNodes; for (int j = 0; j < childNodes.Count; j++) { XmlNode subNode = childNodes[j]; if (subNode.LocalName.Equals(sub2Section)) { return subNode.SelectSingleNode(Key).InnerText; } } } } } return ""; } catch (Exception) { return ""; } } /// <summary> /// 获取四层小结下的键值 /// </summary> /// <param name="doc"></param> /// <param name="Section"></param> /// <param name="subSection"></param> /// <param name="sub2Section"></param> /// <param name="sub3Section"></param> /// <param name="Key"></param> /// <returns></returns> public static string XmlReadValue(XmlDocument doc, string Section, string subSection, string sub2Section, string sub3Section, string Key) { try { XmlNode result = doc.SelectSingleNode(Section); if (null != result) { XmlNodeList childnodes = result.ChildNodes; for (int i = 0; i < childnodes.Count; i++) { XmlNode node = childnodes[i]; if (node.LocalName.Equals(subSection)) { XmlNodeList subNodeList = node.ChildNodes; for (int j = 0; j < subNodeList.Count; j++) { XmlNode subNode = subNodeList[j]; if (subNode.LocalName.Equals(sub2Section)) { XmlNodeList sub2NodeList = subNode.ChildNodes; for (int k = 0; k < sub2NodeList.Count; k++) { XmlNode sub2Node = sub2NodeList[k]; if (sub2Node.LocalName.Equals(sub3Section)) { return sub2Node.SelectSingleNode(Key).InnerText; } } } } } } } return ""; } catch { return ""; } } #endregion #region 写入节点值 /// <summary> /// 写入节点值 /// </summary> /// <param name="doc"></param> /// <param name="sXMLPath"></param> /// <param name="Section"></param> /// <param name="Key"></param> /// <param name="Value"></param> public static void XmlWriteValue(XmlDocument doc, string sXMLPath, string Section, string Key, string Value) { try { XmlNode result = doc.SelectSingleNode(Section); if (null == result) { result = (XmlNode)doc.CreateElement(Section); doc.AppendChild(result); XmlNode subNode = doc.CreateElement(Key); subNode.InnerText = Value; result.AppendChild(subNode); } else { XmlNode subNode = result.SelectSingleNode(Key); if (null == subNode) { subNode = doc.CreateElement(Key); subNode.InnerText = Value; result.AppendChild(subNode); } else { subNode.InnerText = Value; } } doc.Save(sXMLPath); } catch (System.Exception ex) { MessageBox.Show("写入节点值错误! " + ex.Message); } } public static void XmlWriteValue(XmlDocument doc, string sXMLPath, string Section, string subSection, string Key, string Value) { try { XmlNode result = doc.SelectSingleNode(Section); if (null == result) { result = (XmlNode)doc.CreateElement(Section); doc.AppendChild(result); XmlElement subEle = doc.CreateElement(subSection); result.AppendChild(subEle); XmlElement sub2Ele = doc.CreateElement(Key); sub2Ele.InnerText = Value; subEle.AppendChild(sub2Ele); } else { XmlNode subEle = result.SelectSingleNode(subSection); if (null == subEle) { subEle = doc.CreateElement(subSection); result.AppendChild(subEle); XmlElement sub2Ele = doc.CreateElement(Key); sub2Ele.InnerText = Value; subEle.AppendChild(sub2Ele); } else { XmlNode sub2Ele = subEle.SelectSingleNode(Key); if (null == sub2Ele) { sub2Ele = doc.CreateElement(Key); sub2Ele.InnerText = Value; subEle.AppendChild(sub2Ele); } else { sub2Ele.InnerText = Value; } } } } catch (System.Exception ex) { MessageBox.Show("写入节点值错误! " + ex.Message); } } public static void XmlWriteValue(XmlDocument doc, string sXMLPath, string Section, string subSection, string sub2Section, string Key, string Value) { try { XmlNode result = doc.SelectSingleNode(Section); if (null == result) { result = (XmlNode)doc.CreateElement(Section); doc.AppendChild(result); } XmlNode subEle = result.SelectSingleNode(subSection); if (null == subEle) { subEle = doc.CreateElement(subSection); result.AppendChild(subEle); } XmlNode sub2Ele = result.SelectSingleNode(sub2Section); if (null == sub2Ele) { sub2Ele = doc.CreateElement(sub2Section); subEle.AppendChild(sub2Ele); } XmlNode sub3Ele = subEle.SelectSingleNode(Key); if (null == sub3Ele) { sub3Ele = doc.CreateElement(Key); sub3Ele.InnerText = Value; sub2Ele.AppendChild(sub2Ele); } else { sub3Ele.InnerText = Value; } doc.Save(sXMLPath); } catch (System.Exception ex) { MessageBox.Show("写入节点值错误! " + ex.Message); } } public static void XmlWriteValue(XmlDocument doc, string sXMLPath,string Section, string subSection, string sub2Section,string sub3Section, string Key, string Value) { try { XmlNode result = doc.SelectSingleNode(Section); if (null == result) { result = (XmlNode)doc.CreateElement(Section); doc.AppendChild(result); } XmlNode subEle = result.SelectSingleNode(subSection); if (null == subEle) { subEle = doc.CreateElement(subSection); result.AppendChild(subEle); } XmlNode sub2Ele = result.SelectSingleNode(sub2Section); if (null == sub2Ele) { sub2Ele = doc.CreateElement(sub2Section); subEle.AppendChild(sub2Ele); } XmlNode sub3Ele = result.SelectSingleNode(sub3Section); if (null == sub2Ele) { sub3Ele = doc.CreateElement(sub3Section); sub2Ele.AppendChild(sub3Ele); } XmlNode sub4Ele = subEle.SelectSingleNode(Key); if (null == sub3Ele) { sub4Ele = doc.CreateElement(Key); sub4Ele.InnerText = Value; sub3Ele.AppendChild(sub2Ele); } else { sub4Ele.InnerText = Value; } doc.Save(sXMLPath); } catch (System.Exception ex) { MessageBox.Show("写入节点值错误! " + ex.Message); } } #endregion #region 修改节点值 /// <summary> /// 修改节点值 /// </summary> /// <param name="doc"></param> /// <param name="sXMLPath"></param> /// <param name="Section"></param> /// <param name="Key"></param> /// <param name="Value"></param> public static void XmlEditValue(XmlDocument doc, string sXMLPath, string Section, string Key, string Value) { XmlNode result = doc.SelectSingleNode(Section); if (null != result) { if (Value == "") { result.SelectSingleNode(Key).InnerText = "无"; } else { result.SelectSingleNode(Key).InnerText = Value; } } doc.Save(sXMLPath); } public static void XmlEditValue(XmlDocument doc, string sXMLPath, string Section, string sValue1, string Key, string Value) { XmlNode result = doc.SelectSingleNode(Section); if (null != result) { XmlNodeList childnodes = result.ChildNodes; for (int i = 0; i < childnodes.Count; i++) { XmlNode node = childnodes[i]; if (node.LocalName.Equals(sValue1)) { if (Value == "") { node.SelectSingleNode(Key).InnerText = "无"; } else { node.SelectSingleNode(Key).InnerText = Value; } } } } doc.Save(sXMLPath); } public static void XmlEditValue(XmlDocument doc, string sXMLPath, string Section, string sValue1, string sValue2, string Key, string Value) { XmlNode result = doc.SelectSingleNode(Section); if (null != result) { XmlNodeList parentNodes = result.ChildNodes; for (int ii = 0; ii < parentNodes.Count; ii++) { XmlNode node = parentNodes[ii]; if (node.LocalName.Equals(sValue1)) { XmlNodeList childNodes = node.ChildNodes; for (int i = 0; i < childNodes.Count; i++) { XmlNode node2 = childNodes[i]; if (node2.LocalName.Equals(sValue2)) { if (Value == "") { node2.SelectSingleNode(Key).InnerText = "无"; } else { node2.SelectSingleNode(Key).InnerText = Value; } } } } } } doc.Save(sXMLPath); } public static void XmlEditValue(XmlDocument doc, string sXMLPath, string Section, string sValue1, string sValue2, string sValue3, string Key, string Value) { XmlNode node = doc.SelectSingleNode(Section); if (null != node) { XmlNodeList childNodes = node.ChildNodes; for (int i = 0; i < childNodes.Count; i++) { XmlNode node2 = childNodes[i]; if (node2.LocalName.Equals(sValue1)) { XmlNodeList list2 = node2.ChildNodes; for (int j = 0; j < list2.Count; j++) { XmlNode node3 = list2[j]; if (node3.LocalName.Equals(sValue2)) { XmlNodeList list3 = node3.ChildNodes; for (int k = 0; k < list3.Count; k++) { XmlNode node4 = list3[k]; if (node4.LocalName.Equals(sValue3)) { if (Value == "") { node4.SelectSingleNode(Key).InnerText = "无"; } else { node4.SelectSingleNode(Key).InnerText = Value; } } } } } } } } doc.Save(sXMLPath); } #endregion } }
public void createXml(double meters) { XmlDocument xml = new XmlDocument(); //xml文件的相对路径 string stringPath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf('\')) + "\searchStation.xml"; if (!System.IO.File.Exists(stringPath)) { System.IO.FileStream fileStream = new System.IO.FileStream(stringPath, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite); fileStream.Close(); System.IO.File.WriteAllText(stringPath, "<?xml version="1.0"?><ROOT><AREA>"+meters.ToString()+"</AREA></ROOT>"); } xml.Load(stringPath); XmlNode nodes = xml.DocumentElement; foreach (XmlNode item in nodes.ChildNodes) { if (item.Name == "AREA") { item.InnerText = meters.ToString(); } } xml.Save(stringPath); }
更多:https://i.cnblogs.com/EditPosts.aspx?postid=3673944