用XmlDocument的处理XML文件

/// <summary>
/// 封装XmlDocument的处理
/// </summary>
public static class XmlHelper
{
///<summary>
/// 获得node下node指定的子节点实体
/// </summary>
/// <param name="node">节点</param>
/// <param name="nodeName">node下子节点名称,多个时用/隔开</param>
/// <returns>node下nodeName指定的子节点实体,若null没找到,否则返回第一个匹配的</returns>
public static XmlNode GetNode(XmlNode node, string nodeName)
{
if (node == null || String.IsNullOrEmpty(nodeName))
{
return null;
}
else
{
string nodePath = "/" + node.Name;
if (nodeName.StartsWith("/"))
{
nodePath += nodeName;
}
else
{
nodePath += "/" + nodeName;
}
return node.SelectSingleNode(nodePath);
}
}
///<summary>
/// 获得node下node指定的子节点的集合
/// </summary>
/// <param name="node">节点</param>
/// <param name="nodeName">node下子节点名称,多个时用/隔开</param>
/// <returns>node下nodeName指定的子节点的集合,若null没找到</returns>
public static XmlNodeList GetNodes(XmlNode node, string nodeName)
{
if (node == null || String.IsNullOrEmpty(nodeName))
{
return null;
}
else
{
string nodePath = "/" + node.Name;
if (nodeName.StartsWith("/"))
{
nodePath += nodeName;
}
else
{
nodePath += "/" + nodeName;
}
return node.SelectNodes(nodePath);
}
}

/// <summary>
/// 给指定的属性赋值
/// </summary>
/// <param name="node">节点实体 空不能赋值</param>
/// <param name="name">属性值 属性值为空不能赋值</param>
/// <param name="value">值对象 空或ToString()==""则不赋值 去除特殊字符</param>
public static void SetAttribute(XmlNode node, string name, object value)
{
if (node != null && !String.IsNullOrEmpty(name)
&& value != null && !Convert.IsDBNull(value))
{
string v = "";
if (value is DateTime)
{
v = Convert.ToDateTime(value).ToString(Constants.DATE_FORMART);
}
else
{
v = GetAttributeValue(value.ToString().Trim());
}
if (v != "")
{
XmlAttribute att = node.Attributes[name];
if (att == null)
{
att = node.OwnerDocument.CreateAttribute(name);
att.Value = v;
node.Attributes.Append(att);
}
else
{
node.Attributes[name].Value = v;
}
}
}
}
/// <summary>
/// 给指定的节点的最后一个子节点的属性赋值
/// </summary>
/// <param name="node">节点实体 空不能赋值</param>
/// <param name="nodeName">node下子节点名称,多个时用/隔开</param>
/// <param name="name">属性值 属性值为空不能赋值</param>
/// <param name="value">值对象 空或ToString()==""则不赋值 去除特殊字符</param>
public static void SetAttribute(XmlNode node, string nodeName, string name, object value)
{
if (node != null && !String.IsNullOrEmpty(name)
&& value != null)
{
if (!String.IsNullOrEmpty(nodeName))
{
node = GetNode(node, nodeName);
}
if (node != null)
{
SetAttribute(node, name, value);
}
}
}
/// <summary>
/// 给指定的节点的最后一个子节点的属性赋值
/// </summary>
/// <param name="node">节点实体 空不能赋值</param>
/// <param name="nodeName">node下子节点名称,多个时用/隔开</param>
/// <param name="nvc">属性及值集合</param>
public static void SetAttribute(XmlNode node, string nodeName, NameValueCollection nvc)
{
if (node != null && nvc != null)
{
if (!String.IsNullOrEmpty(nodeName))
{
node = GetNode(node, nodeName);
}
if (node != null)
{
for (int i = 0; i < nvc.Count; i++)
{
SetAttribute(node, nvc.GetKey(i), nvc[i]);
}
}
}
}
/// <summary>
/// 去除属性值中不符合xml要求的字符
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static string GetAttributeValue(string value)
{
if (value == "")
{
return "";
}
else
{
return value.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("/"", "&quot;").Replace("%", "");
}
}

}

你可能感兴趣的:(document)