用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

XML文件的操作类

2012-10-19 作者: 神马举报

[c#]代码库

/// XML文件的操作类
/// 
/// 
/// 读取数据
///
/// 路径
/// 节点
/// 属性名,非空时返回该属性值,否则返回串联值
/// string
/**************************************************
 * 使用示列:
 * XmlHelper.Read(path, "/Node", "")
 * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
 ************************************************/
public static string Read ( string path, string node, string attribute )
{
	string value = "";
	try
	{
		XmlDocument doc = new XmlDocument();
		doc.Load ( path );
		XmlNode xn = doc.SelectSingleNode ( node );
		value = ( attribute.Equals ( "" ) ? xn.InnerText : xn.Attributes[attribute].Value );
	}
	catch { }
	return value;
}
///
/// 插入数据
///
/// 路径
/// 节点
/// 元素名,非空时插入新元素,否则在该元素中插入属性
/// 属性名,非空时插入该元素属性值,否则插入元素值
/// 值
///
/**************************************************
 * 使用示列:
 * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
 * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
 * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
 ************************************************/
public static void Insert ( string path, string node, string element, string attribute, string value )
{
	try
	{
		XmlDocument doc = new XmlDocument();
		doc.Load ( "XmlFile.xml" );
		XmlNode xn = doc.SelectSingleNode ( node );
		if ( element.Equals ( "" ) )
		{
			if ( !attribute.Equals ( "" ) )
			{
				XmlElement xe = ( XmlElement ) xn;
				xe.SetAttribute ( attribute, value );
			}
		}
		else
		{
			XmlElement xe = doc.CreateElement ( element );
			if ( attribute.Equals ( "" ) )
				xe.InnerText = value;
			else
				xe.SetAttribute ( attribute, value );
			xn.AppendChild ( xe );
		}
		doc.Save ( path );
	}
	catch { }
}
///
/// 修改数据
///
/// 路径
/// 节点
/// 属性名,非空时修改该节点属性值,否则修改节点值
/// 值
///
/**************************************************
 * 使用示列:
 * XmlHelper.Insert(path, "/Node", "", "Value")
 * XmlHelper.Insert(path, "/Node", "Attribute", "Value")
 ************************************************/
public static void Update ( string path, string node, string attribute, string value )
{
	try
	{
		XmlDocument doc = new XmlDocument();
		doc.Load ( path );
		XmlNode xn = doc.SelectSingleNode ( node );
		XmlElement xe = ( XmlElement ) xn;
		if ( attribute.Equals ( "" ) )
			xe.InnerText = value;
		else
			xe.SetAttribute ( attribute, value );
		doc.Save ( path );
	}
	catch { }
}
///
/// 删除数据
///
/// 路径
/// 节点
/// 属性名,非空时删除该节点属性值,否则删除节点值
/// 值
///
/**************************************************
 * 使用示列:
 * XmlHelper.Delete(path, "/Node", "")
 * XmlHelper.Delete(path, "/Node", "Attribute")
 ************************************************/
public static void Delete ( string path, string node, string attribute )
{
	try
	{
		XmlDocument doc = new XmlDocument();
		doc.Load ( path );
		XmlNode xn = doc.SelectSingleNode ( node );
		XmlElement xe = ( XmlElement ) xn;
		if ( attribute.Equals ( "" ) )
			xn.ParentNode.RemoveChild ( xn );
		else
			xe.RemoveAttribute ( attribute );
		doc.Save ( path );
	}
	catch { }
}
#region 使用方法
//==================================================
//XmlFile.xml:
//
//
//==================================================

//string xml = Server.MapPath("XmlFile.xml");
//插入元素
//XmlHelper.Insert(xml, "/Root", "Studio", "", "");
//插入元素/属性
//XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "小路工作室");
//XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "丁香鱼工作室");
//XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "谱天城工作室");
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Master", "", "红尘静思");
//插入属性
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='小路工作室']", "", "Url", "http://www.wzlu.com/");
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "", "Url", "http://www.luckfish.net/");
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "", "Url", "http://www.putiancheng.com/");
//修改元素值
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']/Master", "", "RedDust");
//修改属性值
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Url", "http://www.putiancheng.net/");
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Name", "PuTianCheng Studio");
//读取元素值
//Response.Write("
" + XmlHelper.Read(xml, "/Root/Studio/Site/Master", "") + "
");
//读取属性值
//Response.Write("
" + XmlHelper.Read(xml, "/Root/Studio/Site", "Url") + "
");
//读取特定属性值
//Response.Write("
" + XmlHelper.Read(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "Url") + "
");
//删除属性
//XmlHelper.Delete(xml, "/Root/Studio/Site[@Name='小路工作室']", "Url");
//删除元素
//XmlHelper.Delete(xml, "/Root/Studio", "");
#endregion


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...