asp.net Xml操作类
using System;
using System.Xml;
namespace WebLayout.App_Code
{
public class XmlHelper
{
public string XmlPath { get; set; }
private XmlDocument xmldoc = new XmlDocument();
/// <summary>
/// 加载文件
/// </summary>
public void LoadXml()
{
try
{
xmldoc.Load(XmlPath);
}
catch (Exception e)
{
//文件加载失败
}
}
/// <summary>
/// 获取指定的节点
/// </summary>
/// <param name="nodeName">节点</param>
/// <returns></returns>
public XmlNode Node(string nodeName)
{
return xmldoc.SelectSingleNode(nodeName);
}
/// <summary>
/// 获取指定节点下的所有直接子节点
/// </summary>
/// <param name="nodeName">节点</param>
/// <returns></returns>
public XmlNodeList NodeList(string nodeName)
{
XmlNode root = Node(nodeName);
return root.ChildNodes;
}
/// <summary>
/// 判断指定节点下是否有子节点
/// </summary>
/// <param name="nodeName">节点</param>
/// <returns></returns>
public bool IsHasChildNode(string nodeName)
{
return Node(nodeName).HasChildNodes;
}
/// <summary>
/// 获取指定节点的文本
/// </summary>
/// <param name="nodeName">节点</param>
/// <returns></returns>
public string GetNodeText(string nodeName)
{
XmlNode curruentNode = Node(nodeName);
return curruentNode.InnerText;
}
/// <summary>
/// 获取节点指定的属性值
/// </summary>
/// <param name="nodeName">节点</param>
/// <param name="attributeName">属性名字</param>
/// <returns></returns>
public string GetNodeAttributeValue(string nodeName, string attributeName)
{
XmlNode node = Node(nodeName);
return node.Attributes[attributeName].Value;
}
/// <summary>
/// 获取子节点的数量
/// </summary>
/// <param name="nodeName">节点</param>
/// <returns></returns>
public int GetChildNodeCount(string nodeName)
{
XmlNode node = Node(nodeName);
return node.ChildNodes.Count;
}
}
}