I. Review of the previous chapter
The last two articles all describe the use of LINQ to XML, it is important to note that LINQ to XML is the processing of in-memory , so how many nodes, how much memory, if the XML is large, but the system memory is limited how to do?
Let's step through the analysis.
Second, set up and achieve the goal
What you need to do today is to export all the files and directories in a directory to an XML, for example:
<?XML version= "1.0" encoding= "gb2312"?><foldername= "Bin"> <foldername= "Debug"> <filename= "ConsoleApplication6.exe" /> <filename= "ConsoleApplication6.exe.config" /> <filename= "Consoleapplication6.pdb" /> <filename= "ConsoleApplication6.vshost.exe" /> <filename= "ConsoleApplication6.vshost.exe.config" /> <filename= "ConsoleApplication6.vshost.exe.manifest" /> </folder> <foldername= "Release"> <filename= "ConsoleApplication6.exe" /> <filename= "ConsoleApplication6.exe.config" /> <filename= "Consoleapplication6.pdb" /> <filename= "ConsoleApplication6.vshost.exe" /> <filename= "ConsoleApplication6.vshost.exe.config" /> <filename= "ConsoleApplication6.vshost.exe.manifest" /> </folder></folder>
1. Using LINQ to XML
If you use the LINQ to XML described earlier, you can write the code quickly:
Static voidMain (string[] args) { varDI =NewDirectoryInfo ("e:\\pdf"); XDocument Doc=NewXDocument (Getfoldercontent (DI)); Doc. Save (Console.Out); Console.readkey ();}Private StaticXElement getfoldercontent (DirectoryInfo di) {return NewXElement ("folder", NewXAttribute ("name", Di. Name), fromSubDirinchdi. GetDirectories ()Selectgetfoldercontent (subdir), fromFileinchdi. GetFiles ()Select NewXElement ("file",NewXAttribute ("name", file. Name )));}
However, it is important to note thatLinq to XML works in a in-memory way , that is, if there are 1000 levels of directory nesting, there are 100 subdirectories, there is a long 100^1000 XElement in memory created. OK, so calculate how much memory the whole process needs, 10 of the 2000 * each XElement consumes memory, even if only 1byte,1g memory can only handle 10 30, so to deal with this scene, how much memory it needs! Can be said to be impossible to achieve.
2. Analysis
The above implementations benefit from the simplicity of the LINQ to XML API, but are subject to the In-memory Model of LINQ to XML, and if you have a simple, non-in-memory model that benefits from LINQ to XML, you can use both worlds.
Is there such a good thing in the world? Don't rush to the conclusion, let's look at MSDN: How to: Perform a streaming transformation of a large XML document.
Here is a xstreamingelement class, just from the name, it can be guessed that this class is a similar xelement type, but it is a stream-like class, not in-memory, Of course, it depends on MSDN.
There is a clear explanation in MSDN: The elements in an XML tree that support deferred stream output.
And the note says: If you stream from an input source (and a text file), you can read very large text files and generate very large XML documents, and colleagues maintain a small memory footprint.
That is, it is clear that this class of xstremingelement itself is (or is analogous to) streaming, not like the normal xelement of in-memory processing.
3. Implement
Once you have found xstreamingelement this alternative API for LINQ to XML, you can achieve the previous goal.
The usage of xstreamingelement is very similar to that of XElement, just a little modification of the previous method:
Private Staticxstreamingelement getfoldercontent (DirectoryInfo di) {return NewXStreamingElement ("folder", NewXAttribute ("name", Di. Name), fromSubDirinchdi. GetDirectories ()Selectgetfoldercontent (subdir), fromFileinchdi. GetFiles ()Select NewXElement ("file",NewXAttribute ("name", file. Name )));}
However, you need to modify the external calling method:
var New DirectoryInfo ("d:\\sourcecode"); Getfoldercontent (DI). Save (console.out);
Note: The XStreamingElement Save method must be used here, otherwise the feature of the deferred solution may fail.
Iii. Summary
This article describes the heterogeneous: XStreamingElement in LINQ to XML, which benefits from the simplicity of LINQ to XML and the small memory characteristics of streaming, which can make work less effective when operating with very large XML.
C # Operations XML (IV)