Using xpath on a stream
is it pos开发者_高级运维sible 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
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.
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.)
精彩评论