C#:xml操作(待补充)
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
C#:xml操作(待补充)的更多相关文章
- LINQ系列:LINQ to XML操作
LINQ to XML操作XML文件的方法,如创建XML文件.添加新的元素到XML文件中.修改XML文件中的元素.删除XML文件中的元素等. 1. 创建XML文件 string xmlFilePath ...
- T-Sql(五)xml操作
t-sql中的xml操作在我们平时做项目的过程中用的很少,因为我们处理的数据量很少,除非一些用到xml的地方,t-sql中xml操作一般用在数据量很大,性能优化的地方,当然我在平时做项目的时候也是没用 ...
- XML格式示例 与 XML操作(读取)类封装
header('Content-Type: text/xml'); <?xml version="1.0" encoding="utf-8" standa ...
- 【Java EE 学习 33 上】【JQuery样式操作】【JQuery中的Ajax操作】【JQuery中的XML操作】
一.JQuery中样式的操作 1.给id=mover的div采用属性增加样式.one $("#b1").click(function(){ $("#mover" ...
- 简单的XML操作类
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
- .net学习笔记---xml操作及读写
一.XML文件操作中与.Net中对应的类 微软的.NET框架在System.xml命名空间提供了一系列的类用于Dom的实现. 以下给出XML文档的组成部分对应.NET中的类: XML文档组成部分 对应 ...
- C#常用操作类库三(XML操作类)
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
- php xml 操作。
参考 文章:http://www.cnblogs.com/zcy_soft/archive/2011/01/26/1945482.html DOMDocument相关的内容. 属性: Attribut ...
- XML Helper XML操作类
写的一个XML操作类,包括读取/插入/修改/删除. using System;using System.Data;using System.Configuration;using System.Web ...
- 我的PHP之旅--XML操作
XML操作 XML主要是做数据存储和WEB服务的,所以我们难免要操作它,这里只介绍PHP的simpleXML方式. 我们要操作的XML: <?xml version="1.0" ...
随机推荐
- iPhone X 适配手机端 H5 页面通用解决方案
一:本文提供两种解决方案 1.终端解决方案(最优,建议选择) 2.web解决方案 导语: iPhone X的出现,一方面对于整个手机行业的发展极具创新领头羊的作用,另一方面也对现有业务的页面适配带来了 ...
- ExtJs 3 自定义combotree
ExtJs 3 自定义combotree /** * 自定义下拉树,支持初始化值时自动定位树节点. * 还没有考虑性能问题.继承自Ext.form.ComboBox也很浪费. * 代码中的cu.get ...
- Spring内部bean实例
在Spring框架中,一个bean仅用于一个特定的属性,这是提醒其声明为一个内部bean.内部bean支持setter注入“property”和构造器注入"constructor-arg“. ...
- How to run WPF – XBAP as Full Trust Application
Recently I work on WPF-XBAP application that will run from intranet website: This application must h ...
- ICLR 2013 International Conference on Learning Representations深度学习论文papers
ICLR 2013 International Conference on Learning Representations May 02 - 04, 2013, Scottsdale, Arizon ...
- 【USACO1.2_2】★Transformations 方块转换
一块N x N(1<=N<=10)正方形的黑白瓦片的图案要被转换成新的正方形图案.写一个程序来找出将原始图案依照下面列转换方法转换成新图案的最小方式: 1:转90度:图案按顺时针转90度. ...
- 盗COOKIE之方法总结
1.xss跨站盗cookie 2.ajax跨域盗cookie 3.hosts文件映射 对于第一种方法,首先:在有跨站漏洞的页面贴上跨站代码如: <script>window.open('h ...
- window yii2 安装插件 报yiisoft/yii2 2.0.x-dev requires ext-mbstring错
Problem 1 - yiisoft/yii2 2.0.x-dev requires ext-mbstring * -> the requested PHP extens ion mbstri ...
- JS-为句柄添加监听函数
具体谈如何实现JS为句柄添加监听函数之前先看一段代码,算是抛出这个问题. <html> <head> <title>JS为句柄添加监听函数</title> ...
- 在Docker和Kubernetes上运行MongoDB微服务
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟.容器是完全使用沙箱机制,相互之间不会有任何接 ...