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" ...
随机推荐
- Altium Protel PCB Layer
The layers themselves are grouped by their functional types: Signal Layers – Top Layer, Bottom Layer ...
- springMvc的一些简介 和基于xml的handlerMapping基本流程
其它步骤就不在介绍了 在大多数情况,都会使用基于annotation的方式进行HandlerMapping处理,在这里基于对这个流程的了解,就采用了基于xml配置了一个HandlerMapping & ...
- [js插件]JqueryUI日期插件
引言 之前使用jqueryUi中的弹出框做了一个可拖拽的弹出登录框,也顺便将里面的常用的日期插件和文本框智能提示插件,也学习了一下. 使用方法 首先在项目中引入以下文件: <!-- 日期插件 默 ...
- 用CSS3来代替JS实现交互
[CSS3和JS] 对于CSS了解的同学都知道,CSS的实现是最底层的,在实现方式和性能上都不是,JS这种提供接口的脚本可比的:从CSS3的动画和JS动画对比角度来看两者,会更清晰:而且随着前端框架的 ...
- 在w7下的wamp中配置memcache
php版本是5.4.16 ,我的电脑是w7 64位的. 一. memcache和memcached的区别 在自己的新程序中打算全面应用memcached技术,这个很容易理解这是memcached是内 ...
- js继承——到继承结束
说到继承,其它语言里可能有两种: 接口继承是继承方法签名,而实现继承则继承实际方法.ES函数没有签名,不能实现接口继承,只支持实现继承,而实现继承主要依靠原型链.(这两句话,说来轻松,理解来不易,且行 ...
- cocos3.0使用cocostudio动画帧结合地图对象键值创建精灵动画
内容例如以下: #include "cocos2d.h" #include "cocostudio/CocoStudio.h" //精灵猫和其它精灵的tag t ...
- CDC之CreateCompatibleDC与BitBlt
CreateCompatibleDC 创建一个与指定设备一致的内存设备描写叙述表. HDC CreateCompatibleDC(HDC hdc //设备描写叙述表句柄); 參数 hdc 现有的设备描 ...
- HDU 3436 Queue-jumpers
题意: n个人站成一排 一開始是从1到n有序的 如今有三个操作 Top操作是将一个人排到队首 Query操作是询问某个人如今排第几 Rank操作是询问排某个位置的人是谁 思路: 将队伍扭来扭 ...
- Appium+python自动化11-adb必知必会的几个指令
前言 学android测试,adb是必学的,有几个常用的指令需要熟练掌握 一.检查设备 1.如何检查手机(或模拟器)是连上电脑的,在cmd输入: >adb devices