经常需要操作xmlDocument,其中经常会涉及到节点操作和属性操作,因此就写了下边两个方法来实现对xml节点的操作
{
if (node.NodeType != XmlNodeType.Element)
{
throw new ArgumentException("传入的节点不是一个XmlNodeType.Element节点");
}
XmlDocument tempXdoc = node.OwnerDocument;
if (node.Attributes[attrName]==null)
{
node.Attributes.Append(tempXdoc.CreateAttribute(attrName));
}
node.Attributes[attrName].Value =attrValue;
}
/// <summary>
/// 添加一个新节点,如果有内容,同时为节点赋值,如果没有,则只创建
/// </summary>
/// <param name="fathernode"></param>
/// <param name="name"></param>
/// <param name="content"></param>
/// <returns></returns>
public static XmlNode AddNewNode(XmlNode fathernode, string name, string content)
{
XmlDocument xdoc = fathernode.OwnerDocument;
XmlNode snode = xdoc.CreateElement(name);
if (!String.IsNullOrEmpty(content))
{
snode.InnerXml = content;
}
fathernode.AppendChild(snode);
return snode;
}