c# XML Helper
来源:互联网 发布:怎么用ps做淘宝细节图 编辑:程序博客网 时间:2024/05/17 01:41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
namespace Helper
{
public class XmlHelper
{
private static XmlHelper _xmlHelper;
/// <summary>
/// 获取Helper的日子
/// </summary>
public string HelperLog
{
private set;
get;
}
public static XmlHelper GetInstance()
{
if (_xmlHelper == null)
{
_xmlHelper = new XmlHelper();
}
return _xmlHelper;
}
XmlHelper()
{
}
/// <summary>
/// 创建一个Xml文档
/// </summary>
/// <param name="XmlFilePath">xml文档路径</param>
/// <param name="RootNode">根节点名称,为空则默认“root”</param>
/// <returns>创建是否成功</returns>
public bool CreateXmlDocument(string XmlFilePath,string RootNode)
{
try
{
HelperLog = "";
XmlDocument doc= new XmlDocument();
if (String.IsNullOrEmpty(RootNode))
{
RootNode = "root";
}
doc.LoadXml("<"+RootNode+"/>");
//XmlElement root=doc.DocumentElement;
if(!Directory.Exists(Path.GetDirectoryName(XmlFilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(XmlFilePath));
}
doc.Save(XmlFilePath);
return true;
}catch(Exception ee)
{
HelperLog = ee.Message;
}
return false;
}
/// <summary>
/// 插入操作
/// </summary>
/// <param name="XmlFilePath">xml文档路径</param>
/// <param name="NodePath">XPath 节点的父路径,为空表示父节点为根节点</param>
/// <param name="NodeName">新节点名称</param>
/// <param name="innerText">新节点innerText值</param>
/// <param name="attributes">新节点属性</param>
/// <returns></returns>
public bool Insert(string XmlFilePath, string NodePath, string NodeName, string innerText, IDictionary<string, string> attributes)
{
HelperLog = "";
if (!File.Exists(XmlFilePath))
{
HelperLog = "xml文件不存在";
return false;
}
try
{
XmlDocument doc = new XmlDocument();
doc.Load(XmlFilePath);
XmlNode node = null;
if (String.IsNullOrEmpty(NodePath))
{
node = doc.DocumentElement as XmlNode;
}
else
{
node = doc.SelectSingleNode(NodePath);
}
if (node == null)
{
HelperLog = "节点不存在";
return false;
}
XmlElement newNode = doc.CreateElement(NodeName);
newNode.InnerText = innerText;
if (attributes != null && attributes.Count > 0)
{
foreach (string key in attributes.Keys)
{
string value = attributes[key];
newNode.SetAttribute(key, value);
}
}
node.AppendChild(newNode);
doc.Save(XmlFilePath);
}
catch (Exception ee)
{
HelperLog = ee.Message;
}
return false;
}
/// <summary>
/// 读取操作
/// </summary>
/// <param name="XmlFilePath">xml文档路径</param>
/// <param name="NodePath">XPath 节点的父路径,为空表示父节点为根节点</param>
/// <param name="attribute">节点属性,为空则返回节点innerText</param>
/// <returns></returns>
public string Read(string XmlFilePath, string NodePath,string attribute)
{
HelperLog = "";
if (!File.Exists(XmlFilePath))
{
HelperLog = "xml文件不存在";
return "";
}
try
{
XmlDocument doc = new XmlDocument();
doc.Load(XmlFilePath);
XmlNode node = null;
if (String.IsNullOrEmpty(NodePath))
{
node = doc.DocumentElement as XmlNode;
}
else
{
node = doc.SelectSingleNode(NodePath);
}
if (node == null)
{
HelperLog = "节点不存在";
return "";
}
if (!String.IsNullOrEmpty(attribute))
{
if (node.Attributes != null && node.Attributes.Count >= 0)
{
if (node.Attributes[attribute] != null)
{
return node.Attributes[attribute].Value;
}
}
}
else
{
return node.InnerText;
}
return "";
}
catch (Exception ee)
{
HelperLog = ee.Message;
}
return "";
}
public bool Update(string XmlFilePath, string NodePath, string newNodeName, string newinnerText, IDictionary<string, string> newattributes)
{
HelperLog = "";
if (!File.Exists(XmlFilePath))
{
HelperLog = "xml文件不存在";
return false;
}
try
{
XmlDocument doc = new XmlDocument();
doc.Load(XmlFilePath);
XmlNode node = null;
if (String.IsNullOrEmpty(NodePath))
{
node = doc.DocumentElement as XmlNode;
}
else
{
node = doc.SelectSingleNode(NodePath);
}
if (node == null)
{
HelperLog = "节点不存在";
return false;
}
XmlElement xe = node as XmlElement;
if (newNodeName != null)
{
//node.Name = newNodeName;
}
if (newinnerText != null)
{
node.InnerText = newinnerText;
}
if (newattributes != null && newattributes.Count > 0)
{
foreach (string key in newattributes.Keys)
{
string value = newattributes[key];
xe.SetAttribute(key, value);
}
}
doc.Save(XmlFilePath);
return true;
}
catch (Exception ee)
{
HelperLog = ee.Message;
}
return false;
}
public bool Delete(string XmlFilePath,string NodePath)
{
HelperLog = "";
if (!File.Exists(XmlFilePath))
{
HelperLog = "xml文件不存在";
return false;
}
try
{
XmlDocument doc = new XmlDocument();
doc.Load(XmlFilePath);
XmlNode node = doc.SelectSingleNode(NodePath);
if (node == null)
{
HelperLog = "节点不存在";
return false;
}
node.ParentNode.RemoveChild(node);
doc.Save(XmlFilePath);
return true;
}
catch (Exception ee)
{
HelperLog = ee.Message;
}
return false;
}
}
}
- C# XML helper
- c# XML Helper
- 【Serialization】Xml Serialize Helper
- Linq to XML Helper
- XML Helper XML操作类
- XML Helper XML操作类
- C#压缩解压Helper
- C# MySQL Helper
- C# SHA256 hash helper
- SQLite Helper Library For C#
- C#异步执行操作Helper
- C#通用类Helper整理
- Helper
- c# IE插件 Browser Helper Object (BHO)
- C#压缩解压文件及文件夹Helper
- C# MVC 自定义分页Helper扩展
- C#链接Sql Server的Sql Helper
- (转载)C# Helper Object( Helper命名的类的含义 )
- ubuntu下安装配置SSH,Putty
- A Glimpse of Quaternion and A Few Discussions
- Android学习之ViewFlipper
- Oracle 常用技术收集
- IA64与x86-64的区别
- c# XML Helper
- [转载]CMD网络命令
- 服务器安全基础知识系列(三)关于网页木马
- VC++的108个问题
- 判断一个日期是否在二个日期之间
- android application apk build
- gSoap客户端调用WebService完成后注意内存释放顺序
- IT项目管理的六种错误思维
- Android背后