XML as a way of data storage, when the data is very large, we will encounter a lot of XML processing problems. In general, the most straightforward way to edit an XML file is to load the XML file into XmlDocument, modify the XmlDocument in memory, and then save it to disk. In this way we will have to load the entire XML document into memory, which is obviously unwise (for large data XML files, memory will be very large, the elder brother said the pear is very big). The next thing we'll talk about is how to efficiently add content (object entity content) to an XML log file.
(i) Design overview
In general, we'll create two different files (through code), the first of which is an XML file, and the second is an XML fragment (TXT file), as shown in the following illustration:
We associate 2 different files with the following definition.
<! ENTITY yourentityrefname SYSTEM "Your XML fragement address (relative or Obsolute address)" >
(ii) generation of XML files
Let's take a look at how to create a related XML file with the following code:
private static void Initxmlfile (String xmllogfilepath, String xmllogcontentfilename, String entityref) { String docType = String. Format ("\n<! DOCTYPE xmllogfile \ n [\ n <!
ENTITY {0} SYSTEM \ "{1}\" >\n]>\n ", EntityRef, Xmllogcontentfilename);
XmlWriterSettings wrappersettings = new XmlWriterSettings () {Indent = true}; using (XmlWriter writer = XmlWriter.Create (Xmllogfilepath, wrappersettings)) {Write
R.writestartdocument (); Writer.
WriteRaw (DocType); Writer.
WriteStartElement (Configresource.xmllogfile); Writer.
WriteStartElement (configresource.xmllogcontent); Writer.
Writeentityref (ENTITYREF); Writer.
WriteEndElement (); Writer.
WriteEndElement (); Writer.
Close (); }
}
Writes to the contents of an XML file are performed primarily through XmlWriter. This method is relatively simple, no longer explained, look at the contents of the file we generated through this method:
<?xml version= "1.0" encoding= "Utf-8"?> <!
DOCTYPE xmllogfile
[
<! ENTITY locations SYSTEM "Xmllogcontentfile-20110220000120.txt" >
]>
<XmlLogFile>
< Xmllogcontent>&locations;</xmllogcontent>
</XmlLogFile>
Locations is the entity reference name, which corresponds to the &Locations; 。
Xmllogcontentfile-20110220000120.txt is the file name of the XML fragment, and the path is relative to the xmllogfile-20110220000120.xml.
&Locations; acts as placeholders, replacing Xmllogfile-20110220000120.xml & with the contents of the Xmllogcontentfile-20110220000120.txt file locations;