4

is it possible to use xpath on a streamed xml file using streamreader (file obtained from the internet) ?

I know the exact location of the data i need but not sure how best to get at it?

Thanks

simon
  • 185
  • 3
  • 9

2 Answers2

1

Use the XDocument.Load(Stream, LoadOptions) method to parse the XML out of the stream. Then you can use XDocument.XPathEvaluate to get the value.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
1

While it would be theoretically possible to build a stream reader that executed an XPath query on a stream, I don't know of any such implementation; the XPath processors in the .NET framework (in XDocument, XmlDocument, and XPathDocument) all read the document into memory before executing the query. All of these objects can read streams.

If speed is a concern, XPathDocument and XPathNavigator are likely to be the fastest, since those objects let you directly iterate over the nodes as the query executes, rather than executing the query and returning a list of nodes for you to iterate over. (Actually XDocument.XPathEvaluate may do this too; the documentation doesn't say.)

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218