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.
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.
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.
using System;
using System.Xml;
namespace ConsoleApplication1
{
class Program
static void Main(string[] args)
XmlTextReader tr = new XmlTextReader("http://feeds.feedburner.com/ScottHanselman");
object enclosure = tr.NameTable.Add("enclosure");
while (tr.Read())
if (tr.NodeType == XmlNodeType.Element &&
Object.ReferenceEquals(tr.LocalName, enclosure))
while (tr.MoveToNextAttribute())
Console.WriteLine(String.Format("{0}:{1}", tr.LocalName, tr.Value));
}
Now playing: Rent - Rent
Scott at DevReach in Bulgaria in October
Developer Stand up Comedy - Coding 4 Fun
TechDays/DevDays Netherlands and Belgium:
Posts by Category Posts by Month
Greatest Hits Dev Tools List