XmlTextReader more and more
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));
}
}
}
}
}
}
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
I tried posting a comment on your "XmlReader More and more" post but the comment spammer test image wouldn't display. I only saw the red X for broken image link. Anyway, I wanted to let you know about another way to do this is kind of neat. This is what I was going to post in the comment:
For a really quick one off, try this from the beta Monad (Microsoft Command Shell - yeah right from the command prompt):
$url = "http://feeds.feedburner.com/ScottHanselman"
$feed = [xml](new-object System.Net.WebClient).DownloadString($url)
$feed.GetElementsByTagName("enclosure") | format-list
url : http://perseus.franklins.net/hanselminutes_0008_lo.wma
type : audio/x-ms-wma
length : 8902435
url : http://perseus.franklins.net/hanselminutes_0006_lo.wma
type : audio/x-ms-wma
length : 7835219
Download Monad (and one heck of a .NET based, CLI scripting environment) from here:
http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
--
Keith Hill
MVP for the Microsoft Command Shell
Isn't that the same case with an XmlDocument, that it uses an XmlReader to populate the document?
cheers
Thanks Scott!!
//Foo[1]/Bar[@val = 'gerrard']
rather than ten or 15 lines of procedural code.
Perf is great, but in most of my apps simple maintanability and easy ramp up are way more important that getting every single drop of speed...
For what you are using it, XmlReader is perfect, however, I believe that if you are already using a standard XML ATOM/RSS/etc... you should be using the whole package of standards that accompany it as much as is benefical, in this case I would use XPath to query the xml doc, you can use XmlDocument or XPathReader (http://workspaces.gotdotnet.com/xpathreader also http://blogs.msdn.com/mfussell/archive/2004/05/12/130312.aspx and http://msdn.microsoft.com/xml/default.aspx?pull=/library/en-us/dnexxml/html/xml05192004.asp) which is an implementation of XmlReader, that supports XPath. Using XPath is a no-brainer for anyone that knows a bit of XML, and I think, makes the code easier to understand.
If that's the case, you probably missed by post from about two years ago: http://clariusconsulting.net/blogs/kzu/archive/2004/05/31/241.aspx.
It's NEVER necessary to load an XML document for web services ;-)
We now even provide a factory method so that you can return strongly typed objects that need to be exposed only as XmlNodes on the client end by means of XmlSerializer:
public static XmlNode Create(object value)
{
return new ObjectNode(value);
}
All without XmlDocuments whatsoever... enjoy buddy!
Comments are closed.
XmlDocument doc = new XmlDocument();
doc.Load("http://feeds.feedburner.com/ScottHanselman");
foreach (XmlNode node in doc.SelectNodes("//enclosure"))
{
foreach (XmlAttribute attr in node.Attributes)
{
Console.WriteLine(string.Format("{0}:{1}", attr.Name, attr.Value));
}
}