【问题标题】:Difference between CreateAttribute and setAttribute c#CreateAttribute和setAttribute c#之间的区别
【发布时间】:2018-04-06 16:19:48
【问题描述】:

我看到有不同的方法可以创建 xml、添加节点并为它们设置属性。但我想知道这两者之间的区别:

XmlNode infoNode = xmlDocument.CreateNode(XmlNodeType.Element, "INFO", string.Empty);
XmlAttribute idAttribute = xmlDocument.CreateAttribute("Id");
idAttribute.Value = this._id.ToString();
infoNode.Attributes.Append(instanceIdAttribute);


XmlElement infoNode= setTCAreaXml.CreateElement("INFO");
infoNode.SetAttribute("Id", this._Id.ToString());

哪个最好用?谢谢!

【问题讨论】:

    标签: c# attributes system.xml


    【解决方案1】:

    CreateAttribute 会做到这一点。创建一个新的Attribute,然后将其Append 转换为Node

    From the doc;

    创建一个具有指定名称的 XmlAttribute。

    SetAttribute 将设置现有Attribute 的值。如果Attribute 不存在,则会创建一个新的并设置其值。

    From the doc;

    设置指定 XmlAttribute 的值。

    【讨论】:

      猜你喜欢
      • 2014-12-13
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 2010-10-23
      • 2013-05-02
      • 1970-01-01
      相关资源
      最近更新 更多