C # output xml in the form of a file stream (which can solve memory overflow)-XmlTextWriter

Source: Internet
Author: User

1. XmlTextWriter indicates the writer that provides a fast, non-cached, and only-in method. This method generates XML data (the data conforms to W3C Extensible Markup Language (XML) 1.0 and "namespace in XML" are recommended) streams or files. Public class XmlTextWriter: XmlWriter XmlTextWriter inherits the abstract class XmlWriterXmlTextWriter. Example: string resultFileName = "c:/44.txt"; if (! System. IO. directory. exists (System. IO. path. getDirectoryName (resultFileName) {System. IO. directory. createDirectory (System. IO. path. getDirectoryName (resultFileName);} XmlTextWriter result = new XmlTextWriter (resultFileName, null); result. formatting = Formatting. indented; result. writeStartElement ("PolicyList"); result. writeStartElement ("Policy"); result. writeAttributeString ("username", "USERNAME" + I. ToString (); result. writeEndElement (); result. writeEndElement (); result. close (); XmlTextWriter does not cause memory overflow because it writes data to the hard disk as a stream. 2. I made a comparison between XmlTextWriter and common concatenated characters: XmlTextWriter string resultFileName = "c:/44.txt"; if (! System. IO. directory. exists (System. IO. path. getDirectoryName (resultFileName) {System. IO. directory. createDirectory (System. IO. path. getDirectoryName (resultFileName);} XmlTextWriter result = new XmlTextWriter (resultFileName, null); result. formatting = Formatting. indented; result. writeStartElement ("PolicyList"); for (int I = 0; I <100000000; I ++) {result. writeStartElement ("Policy"); result. writeAtt RibuteString ("username", "USERNAME" + I. toString (); result. writeEndElement ();} result. writeEndElement (); result. close (); normal: XmlDocument xmlD = new XmlDocument (); StringBuilder sb = new StringBuilder (); for (int I = 0; I <100000000; I ++) {sb. append ("<PolicyList username = 'username" + I. toString () + "'/>");} xmlD. loadXml (sb. toString (); StreamWriter sw = new StreamWriter ("c:/55.txt"); sw. write ("www" ); Sw. Close (); the first is displayed normally, and the second is displayed with a memory overflow prompt. Therefore, when the data volume is large. It must be written as a stream to prevent memory overflow. 3. This reminds me of the fact that we did not think about writing data to the hard disk when we used. net to write the output. If memory overflow occurs, it is not feasible. In fact, I did not really want to talk about writing data to the hard disk? Wrong. I thought of it. But I immediately deny myself. I always feel that this is impossible. This is the limitation of thinking. My thinking is limited. Later I thought. net is actually a framework. All the things that can be implemented by binary assembly can also be implemented by advanced languages. A computer is nothing more than a binary operation. It can write data to the memory, or write data to the hard disk. After all, we are doing this kind of thing every day. Just ignore it. So I want to break my mind limitations and boldly imagine it!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.