[VBS] vbs specify encoding to save text files (with xml, ini, whatever)
This article is still a product derived during the tossing of the installer.
My installation package there had this action during installation:.
- Have the user fill in some information and write it to the specified ini, xml file in the software installation directory after the installation is complete
The OP was talking about how to write ini with vbs, ini got it done, this time messing with xml. It would have been easy to manipulate xml in most languages, including vbs, with the help of xml's well-established DOM implementation, by calling Microsoft.XMLDOM objects such as.
Dim doc Set doc = CreateObject("Microsoft.XMLDOM") doc.load("c:a.xml") doc.documentElement.appendChild(doc.createElement("ElementOne")) doc.save("c:a.xml")
This completes the task of adding an element to c:a.xml.
But the bias ran into a coding problem. My xml is to be encoded in utf-8, doc.save is getting ANSI encoded file ~ let's not save ah not save, nerve adding an encoding parameter to save method will die ah ~ well, civilized blogging. As the saying goes, God's work is rewarded, and with a lot of fussing, it's finally done.
Dim doc Set doc = CreateObject("Microsoft.XMLDOM") doc.preserveWhiteSpace = True 'Does not automatically handle blanks doc.load("c:a.xml") doc.documentElement.appendChild(doc.createElement("ElementOne")) Dim stream Set stream=CreateObject("ADODB.Stream") stream.Mode= 3 ' It has to be this.[adModeReadWrite] stream.Charset = "utf-8" ' Assigned code stream.Open ' Open the stream doc.save stream ' That's the key.，save Not necessarily writing to a file， It is also possible to write to the stream object stream.SaveToFile "c:a.xml", 2 ' Saving files with stream objects stream.Close ' Close the stream
Simply put, the DOMDocument object is used to manipulate the xml and the Stream object is used to save the file, and the stream is able to specify the encoding, so the problem gets fixed. Equivalent to the c# XmlDocument.Save (Stream) method
Solving this problem is a bit of a blind cat and a dead cat, that is, the DOMDocument's save method of the document (http://msdn.microsoft.com/en-us/library/ms753769(v=vs.85).aspx ) I've read the word "stream" N times and I can't find the word "stream", and I'm not good at E. I don't know which of these things it says is representative of stream.