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" ...
随机推荐
- Extjs下拉树代码测试总结
http://blog.csdn.net/kunoy/article/details/8067801 首先主要代码源自网络,对那些无私的奉献者表示感谢! 笔者对这些代码做了二次修改,并总结如下: Ex ...
- ExtJs 3 自定义combotree
ExtJs 3 自定义combotree /** * 自定义下拉树,支持初始化值时自动定位树节点. * 还没有考虑性能问题.继承自Ext.form.ComboBox也很浪费. * 代码中的cu.get ...
- oc/object-c/ios哪种遍历NSArray/NSDictionary方式快?测试报告
做app的时候,总免不了要多次遍历数组或者字典.究竟哪种遍历方式比较快呢?我做了如下测试:首先定义测试用宏: ? 1 2 3 4 5 6 7 8 9 #define MULogTimeinterval ...
- Struts2中表单与Action传递数据三种方式
1. Action中的属性与表单中的属性一致就可以 JSP中的表单 <form action="login.action" method="post&q ...
- 腾讯PHP工程师面试题两份
试题一: PHP开发工程师笔试试卷 姓名:__________ 一.PHP开发部分 1.合并两个数组有几种方式,试比较它们的异同 2.请写一个函数来检查用户提交的数据是否为整数(不区分数据类型,可以为 ...
- change kernel defconfig
make -C kernel/goldfish O=/media/linux/1/touch/export/phablet-ubuntu-20130618/out/target/product/gen ...
- Raspberry pi,一个好玩的派:第八季 Raspbmc(下)
上一季安装好Raspbmc就等着这一季好好玩耍呢. 我们要在这一季中完毕例如以下任务:调整分辨率.连接wifi并在无线路由器中设置固定IP.手机遥控Raspbmc.改变语言为中文.远程訪问Raspbe ...
- document.readyState等属性,判断页面是否加载完
如何在页面加载完成后再去做某事?什么方法可以判断当前页面加载已完成?document.readyState 判断页面是否加载完成?javascript提供了document.readyState==& ...
- Linux开发环境必备十大开发工具
Linux是一个优秀的开发环境,但是如果没有好的开发工具作为武器,这个环境给你带来的好处就会大打折扣.幸运的是,有很多好用的Linux和开源开发工具供你选择,如果你是一个新手,你可能不知道有哪些工具可 ...
- [Android Studio] Android Studio中查看类的继承关系
转载自:http://blog.csdn.net/hyr83960944/article/details/38098091 查看类的继承关系的快捷键F4,在Android Studio常用快捷键这篇文 ...