文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C#操作Xml通用工具类

C#操作Xml通用工具类

时间:2010-11-09  来源:枢木

 

代码
using System;
using System.Xml;

namespace WLERP.Common.Util
{
/// <summary>
/// Xml 操作工具类
/// </summary>
public sealed class XmlUtil
{
private XmlUtil() { }

/// <summary>
/// 创建节点
/// </summary>
/// <param name="node"></param>
/// <param name="newElementName"></param>
/// <returns></returns>
public static XmlNode AppendElement(XmlNode node, string newElementName)
{
return AppendElement(node, newElementName, null);
}

/// <summary>
/// 创建节点
/// </summary>
/// <param name="node"></param>
/// <param name="newElementName"></param>
/// <param name="innerValue"></param>
/// <returns></returns>
public static XmlNode AppendElement(XmlNode node, string newElementName, string innerValue)
{
XmlNode oNode;
if (node is XmlDocument)
{
oNode
= node.AppendChild(((XmlDocument)node).CreateElement(newElementName));
}
else
{
oNode
= node.AppendChild(node.OwnerDocument.CreateElement(newElementName));
}

if (innerValue != null)
{
oNode.AppendChild(node.OwnerDocument.CreateTextNode(innerValue));
}
return oNode;
}

/// <summary>
/// 创建属性
/// </summary>
/// <param name="xmlDocument"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
public static XmlAttribute CreateAttribute(XmlDocument xmlDocument, string name, string value)
{
XmlAttribute oAtt
= xmlDocument.CreateAttribute(name);
oAtt.Value
= value;
return oAtt;
}

/// <summary>
/// 设置属性的值
/// </summary>
/// <param name="node"></param>
/// <param name="attributeName"></param>
/// <param name="attributeValue"></param>
public static void SetAttribute(XmlNode node, string attributeName, string attributeValue)
{
if (node.Attributes[attributeName] != null)
{
node.Attributes[attributeName].Value
= attributeValue;
}
else
{
node.Attributes.Append(CreateAttribute(node.OwnerDocument, attributeName, attributeValue));
}
}

/// <summary>
/// 获取属性的值
/// </summary>
/// <param name="node"></param>
/// <param name="attributeName"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static string GetAttribute(XmlNode node, string attributeName, string defaultValue)
{
XmlAttribute att
= node.Attributes[attributeName];
if (att != null)
{
return att.Value;
}
else
{
return defaultValue;
}
}

/// <summary>
/// 获取节点的值
/// </summary>
/// <param name="parentNode"></param>
/// <param name="nodeXPath"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static string GetNodeValue(XmlNode parentNode, string nodeXPath, string defaultValue)
{
XmlNode node
= parentNode.SelectSingleNode(nodeXPath);
if (node.FirstChild != null)
{
return node.FirstChild.Value;
}
else if (node != null)
{
return node.Value;
}
else
{
return defaultValue;
}
}
}
}

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载