XmlAttributeCollection的Indexer和GetNamedItem方法
今天改一段code,发现ms guy这样得到一个XmlNode的Attribute:
XmlNode attribute = node.Attributes.GetNamedItem("myAttribute");
为什么不直接用
XmlAttribute attribute = node.Attributes["myAttribute"];
于是就check了一下,发现
XmlAttributeCollection的Indexer实际上是call GetNamedItem方法,然后做了一个转换:
public virtual XmlAttribute this[string name]
{
get
{
return (XmlAttribute) this.GetNamedItem(name);
}
}

而直接call GetNamedItem方法,就可以少转换一次。这样在循环处理xml文档的时候,可以一定程度的提高效率. :)
XmlNode attribute = node.Attributes.GetNamedItem("myAttribute");为什么不直接用
XmlAttribute attribute = node.Attributes["myAttribute"];于是就check了一下,发现
XmlAttributeCollection的Indexer实际上是call GetNamedItem方法,然后做了一个转换:
public virtual XmlAttribute this[string name]
{
get
{
return (XmlAttribute) this.GetNamedItem(name);
}
}

而直接call GetNamedItem方法,就可以少转换一次。这样在循环处理xml文档的时候,可以一定程度的提高效率. :)


浙公网安备 33010602011771号