xmltextreader
Random thought: I like the whole XmlReader philosophy. I use it much more often than XmlDocument. I haven't made an XmlDocument in a while. Every once in a while an XmlDocument shows up when you need an XmlNode for some SOAP stuff, but for the most part, I like XmlReaders.
随意的想法:我喜欢整个XmlReader哲学。 我使用它的次数比XmlDocument多得多。 我有一段时间没有制作XmlDocument了。 当您需要一个XmlNode来处理一些SOAP内容时,有时会出现一个XmlDocument,但是在大多数情况下,我喜欢XmlReaders。
Someone wanted a chunk of code that grabbed RSS Enclosures from a feed. They didn't care about the content, they just wanted the enclosures' attributes. Here's what I sent them 2 mins later.
有人想要从提要中获取RSS附件的大量代码。 他们不在乎内容,只想要附件的属性。 这是我2分钟后发送给他们的内容。
Sure this code could have been done with XmlDocument.SelectNodes (and I'm sure one of you will show me how) but without getting to much into premature optimization, I know that using an XmlReader will always give me better performance. Always. If use it for little one-off stuff like this, I know when I need real performance for a real app, the usage will be fresh in my mind.
当然,可以使用XmlDocument.SelectNodes来完成此代码(我敢肯定你们中的一个会向我展示如何做),但在不进行过早优化的情况下,我知道使用XmlReader总是可以为我带来更好的性能。 总是。 如果将它用于这样的一次性应用,我知道当我需要真正的应用程序的真正性能时,这种用法将是新鲜的。
using System;
使用系统;
using System.Xml;
使用System.Xml;
namespace ConsoleApplication1
命名空间ConsoleApplication1
{
{
class Program
班级计划
{
{
static void Main(string[] args)
静态void Main(字符串[] args)
{
{
XmlTextReader tr = new XmlTextReader("http://feeds.feedburner.com/ScottHanselman");
XmlTextReader tr =新的XmlTextReader(“ http://feeds.feedburner.com/ScottHanselman”);
object enclosure = tr.NameTable.Add("enclosure");
对象附件= tr.NameTable.Add(“附件”);
while (tr.Read())
而(tr.Read())
{
{
if (tr.NodeType == XmlNodeType.Element &&
如果(tr.NodeType == XmlNodeType.Element &&
Object.ReferenceEquals(tr.LocalName, enclosure))
Object.ReferenceEquals(tr.LocalName,附件))
{
{
while (tr.MoveToNextAttribute())
同时(tr.MoveToNextAttribute())
{
{
Console.WriteLine(String.Format("{0}:{1}", tr.LocalName, tr.Value));
Console.WriteLine(String.Format(“ {0}:{1}”,tr.LocalName,tr.Value));
}
}
}
}
}
}
}
}
}
}
}
}
翻译自: https://www.hanselman.com/blog/xmltextreader-more-and-more
xmltextreader