开发者

Does an XPathDocument load the whole xml document?

If I do

XPathDocument doc = new XPathDocument("filename.xml");

Does that load the entire document into memory? I'm writing a mobile phone app and the document might store lots of data that doesn't eve开发者_开发知识库r need to all be loaded at the same time. Mobile phones don't usually have too much ram!


Yes, the whole XML document is represented in memory.

One solution is to split a large XML document into many smaller ones.

Or, alternatively, you may write your own XmlReader that will ignore unwanted subtrees. You can pass this XmlReader as argument to the XpathDocument() constructor -- need to camouflage it as TextReader.


You probably want something more like the System.Xml.XmlReader.


You might want to look at the XStreamingElement of LINQ to XML:

Represents elements in an XML tree that supports deferred streaming output.

This class allows you to create an XML tree that supports deferred streaming output. You use this class to create an XML tree in a very similar fashion to creating an XML tree using XElement. However, there is a fundamental difference. When you use a LINQ query to specify content when creating an XML tree using XElement, the query variable is iterated at the time of construction of the XML tree, and the results of the query are added to the XML tree. In contrast, when you create an XML tree using XStreamingElement, a reference to the query variable is stored in the XML tree without being iterated. Queries are iterated only upon serialization. This allows you to create larger XML trees while maintaining a smaller memory footprint.

If you are streaming from an input source, such as a text file, then you can read a very large text file, and generate a very large XML document while maintaining a small memory footprint.


https://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathdocument.aspx

"Provides a fast, read-only, in-memory representation of an XML document by using the XPath data model."


You might want to try the other constructor XPathDocument(stream). Streaming the document won't use as much memory.

Maybe you wan't to take a look at Linq to XML, as it's easier, faster to code and make more beautiful code than to use SAX/DOM parsing. See this post : How to Store data without using Database and how to retrieve them?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜