I have a string that contains well formed xml in it. I want to navigate the XML in that string to extract the text in certain nodes. How do I efficiently accomplish this using a built-in .NET class. Which .NET XML class would you use and why?
Many thanks for your help.
Note 1: Linq is not available to me.
Note 2: Editing the XML is not important. Read-only access is what I need.
For speed, use an
XmlReader:The above prints out the text content of every element named “foo” in the XML document. (Well, sort of.
ReadStringdoesn’t handle nested elements very gracefully.)Using an
XPathDocumentis slower, because the entire document gets parsed before you can start searching it, but it has the merit of simplicity:If you’re not concerned with performance or memory utilization, it’s simplest to use an
XmlDocument: