ASP.NET 操作XML
2012-07-30 15:04
453 查看
//调用XML的方法
XMLReader rd = new XMLReader("/xml/user.xml");
return rd.SelectNode("节点[@属性名=\"" + 属性值 + "\"]", "节点中的子节点名");
///操作XML的类
using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Xml;
using log4net;
namespace Apps_Factory.Common
{
public class XMLReader
{
public static readonly ILog exlog = null;
private XmlDocument xmlDoc;
private string xmlpath;
static XMLReader()
{
exlog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
public XMLReader(string path)
{
try
{
xmlpath = HttpContext.Current.Server.MapPath(path);
xmlDoc = new XmlDocument();
if (xmlpath != null)
{
xmlDoc.Load(xmlpath);
}
}
catch (Exception ex) { }
}
public XMLReader(string path, int type)
{
try
{
xmlDoc = new XmlDocument();
if (path != null)
{
xmlDoc.Load(path);
}
}
catch (Exception ex) { }
}
/// <summary>
/// 获取节点集
/// </summary>
/// <param name="xpath"></param>
/// <returns></returns>
public XmlNodeList GetXMLFileodes(string xpath)
{
if (xmlDoc != null)
{
return xmlDoc.SelectNodes(xpath);
}
else
{
return null;
}
}
/// <summary>
/// 获取指定节点的指定项名
/// </summary>
/// <param name="nodeName"></param>
/// <param name="attribute"></param>
/// <param name="attributeValue"></param>
/// <param name="dataAttribute"></param>
/// <returns></returns>
public string GetDataFromxml(string nodeName, string attribute, string attributeValue, string dataAttribute)
{
if (xmlDoc != null)
{
XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeName).ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
if (xe.GetAttribute(attribute) == attributeValue)//如果genre属性值为“李赞红”
{
//xe.SetAttribute("genre", "update李赞红");//则修改该属性为“update李赞红”
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
if (xe2.Name == dataAttribute)//如果找到
{
return xe2.InnerText;
}
}
break;
}
}
return "";
// xmlDoc.Save(path);//保存。
}
else
{
return "";
}
}
/// <summary>
/// 选择node
/// "/节点 [@属性 = 属性值 ]"
/// 根节点[子节点/子节点='值']
/// </summary>
/// <param name="select"></param>
/// <returns></returns>
public XmlNode SelectNode(string select)
{
XmlNode selectNode;
if (xmlDoc != null)
{
selectNode = xmlDoc.SelectSingleNode("descendant::" + select);
if (selectNode == null)
{
return null;
}
}
else
{
return null;
}
return selectNode;
}
/// <summary>
/// 选择节点
/// </summary>
/// <param name="select">根节点[子节点/子节点='值']</param>
/// <param name="nodeName">具体某个节点名称</param>
/// <returns>返回node节点的值</returns>
public string SelectNode(string select, string nodeName)
{
XmlNode selectNode;
string nodeInntext;
if (xmlDoc != null)
{
try
{
selectNode = xmlDoc.SelectSingleNode("descendant::" + select);
if (selectNode == null)
{
return "";
}
else
{
try
{
nodeInntext = selectNode.SelectSingleNode("descendant::" + nodeName).InnerText;
}
catch (Exception e)
{
exlog.Error(e.Message);
return "";
}
}
}
catch (Exception ex)
{
exlog.Error(ex.Message);
return "";
}
}
else
{
return "";
}
return nodeInntext;
}
/// <summary>
/// innertext
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public string GetXMLNodeText(XmlNode n)
{
if (n != null)
{
return n.InnerText;
}
else
{
return "";
}
}
/// <summary>
/// attribute value
/// </summary>
/// <param name="n"></param>
/// <param name="attribute"></param>
/// <returns></returns>
public string GetXMLNodeText(XmlNode n, string attribute)
{
if (n != null)
{
return n.Attributes[attribute].Value;
}
else
{
return "";
}
}
}
}
//XML文件
<?xml version="1.0" encoding="utf-8"?>
<user>
<haoma v="1">
<title>11111</title>
<values>1您本月命犯桃花哦</values>
</haoma>
<haoma v="2">
<title>22222</title>
<values>2您本月命犯桃花哦</values>
</haoma>
<haoma v="3">
<title>33333</title>
<values>3您本月命犯桃花哦</values>
</haoma>
<haoma v="4">
<title>4444</title>
<values>4您本月命犯桃花哦</values>
</haoma>
<haoma v="5">
<title>55555</title>
<values>5您本月命犯桃花哦</values>
</haoma>
<haoma v="6">
<title>66666</title>
<values>6您本月命犯桃花哦</values>
</haoma>
<haoma v="7">
<title>77777</title>
<values>7您本月命犯桃花哦</values>
</haoma>
<haoma v="8">
<title>88888</title>
<values>8您本月命犯桃花哦</values>
</haoma>
<haoma v="9">
<title>999999</title>
<values>9您本月命犯桃花哦</values>
</haoma>
</user>
XMLReader rd = new XMLReader("/xml/user.xml");
return rd.SelectNode("节点[@属性名=\"" + 属性值 + "\"]", "节点中的子节点名");
///操作XML的类
using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Xml;
using log4net;
namespace Apps_Factory.Common
{
public class XMLReader
{
public static readonly ILog exlog = null;
private XmlDocument xmlDoc;
private string xmlpath;
static XMLReader()
{
exlog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
public XMLReader(string path)
{
try
{
xmlpath = HttpContext.Current.Server.MapPath(path);
xmlDoc = new XmlDocument();
if (xmlpath != null)
{
xmlDoc.Load(xmlpath);
}
}
catch (Exception ex) { }
}
public XMLReader(string path, int type)
{
try
{
xmlDoc = new XmlDocument();
if (path != null)
{
xmlDoc.Load(path);
}
}
catch (Exception ex) { }
}
/// <summary>
/// 获取节点集
/// </summary>
/// <param name="xpath"></param>
/// <returns></returns>
public XmlNodeList GetXMLFileodes(string xpath)
{
if (xmlDoc != null)
{
return xmlDoc.SelectNodes(xpath);
}
else
{
return null;
}
}
/// <summary>
/// 获取指定节点的指定项名
/// </summary>
/// <param name="nodeName"></param>
/// <param name="attribute"></param>
/// <param name="attributeValue"></param>
/// <param name="dataAttribute"></param>
/// <returns></returns>
public string GetDataFromxml(string nodeName, string attribute, string attributeValue, string dataAttribute)
{
if (xmlDoc != null)
{
XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeName).ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
if (xe.GetAttribute(attribute) == attributeValue)//如果genre属性值为“李赞红”
{
//xe.SetAttribute("genre", "update李赞红");//则修改该属性为“update李赞红”
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
if (xe2.Name == dataAttribute)//如果找到
{
return xe2.InnerText;
}
}
break;
}
}
return "";
// xmlDoc.Save(path);//保存。
}
else
{
return "";
}
}
/// <summary>
/// 选择node
/// "/节点 [@属性 = 属性值 ]"
/// 根节点[子节点/子节点='值']
/// </summary>
/// <param name="select"></param>
/// <returns></returns>
public XmlNode SelectNode(string select)
{
XmlNode selectNode;
if (xmlDoc != null)
{
selectNode = xmlDoc.SelectSingleNode("descendant::" + select);
if (selectNode == null)
{
return null;
}
}
else
{
return null;
}
return selectNode;
}
/// <summary>
/// 选择节点
/// </summary>
/// <param name="select">根节点[子节点/子节点='值']</param>
/// <param name="nodeName">具体某个节点名称</param>
/// <returns>返回node节点的值</returns>
public string SelectNode(string select, string nodeName)
{
XmlNode selectNode;
string nodeInntext;
if (xmlDoc != null)
{
try
{
selectNode = xmlDoc.SelectSingleNode("descendant::" + select);
if (selectNode == null)
{
return "";
}
else
{
try
{
nodeInntext = selectNode.SelectSingleNode("descendant::" + nodeName).InnerText;
}
catch (Exception e)
{
exlog.Error(e.Message);
return "";
}
}
}
catch (Exception ex)
{
exlog.Error(ex.Message);
return "";
}
}
else
{
return "";
}
return nodeInntext;
}
/// <summary>
/// innertext
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public string GetXMLNodeText(XmlNode n)
{
if (n != null)
{
return n.InnerText;
}
else
{
return "";
}
}
/// <summary>
/// attribute value
/// </summary>
/// <param name="n"></param>
/// <param name="attribute"></param>
/// <returns></returns>
public string GetXMLNodeText(XmlNode n, string attribute)
{
if (n != null)
{
return n.Attributes[attribute].Value;
}
else
{
return "";
}
}
}
}
//XML文件
<?xml version="1.0" encoding="utf-8"?>
<user>
<haoma v="1">
<title>11111</title>
<values>1您本月命犯桃花哦</values>
</haoma>
<haoma v="2">
<title>22222</title>
<values>2您本月命犯桃花哦</values>
</haoma>
<haoma v="3">
<title>33333</title>
<values>3您本月命犯桃花哦</values>
</haoma>
<haoma v="4">
<title>4444</title>
<values>4您本月命犯桃花哦</values>
</haoma>
<haoma v="5">
<title>55555</title>
<values>5您本月命犯桃花哦</values>
</haoma>
<haoma v="6">
<title>66666</title>
<values>6您本月命犯桃花哦</values>
</haoma>
<haoma v="7">
<title>77777</title>
<values>7您本月命犯桃花哦</values>
</haoma>
<haoma v="8">
<title>88888</title>
<values>8您本月命犯桃花哦</values>
</haoma>
<haoma v="9">
<title>999999</title>
<values>9您本月命犯桃花哦</values>
</haoma>
</user>
相关文章推荐
- asp.net 对XML的操作
- Asp.Net XML操作基类(修改,删除,新增,创建)
- ASP.NET对XML进行读,改,删操作代码
- ASP.NET控件GridView的使用& Xml操作注意事项
- ASP.Net 向xml文件中添加数据(insert操作)
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目
- asp.net操作xml
- asp.net 操作xml
- Asp.Net XML操作基类(修改,删除,新增,创建)
- asp.net操作XML
- asp.net操作XML(转)
- 技巧百问(9):ASP.NET和C#中对XML的操作,以及简单的xml与xsl !
- Asp.Net XML操作基类(修改,删除,新增,创建)
- asp.net操作xml小结
- asp.net 操作xml
- ASP.NET对XML进行读,改,删操作代码
- asp.net 操作xml
- ASP.NET 利用Linq和XPath操作XML
- Asp.Net+XML操作基类(修改,删除,新增,创建)第1/2页
- ASP.NET XML读取、增加、修改和删除操作