经常需要操作xmlDocument,其中经常会涉及到节点操作和属性操作,因此就写了下边两个方法来实现对xml节点的操作

 attrValue)
{
    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;
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2021-07-04
  • 2022-12-23
  • 2021-05-01
猜你喜欢
  • 2021-06-24
  • 2021-08-06
  • 2021-10-17
  • 2021-04-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案