cool hit counter [VBS] vbs specify encoding to save text files (with xml, ini, whatever)_Intefrankly

[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.

Of course, I found out later that I was negligent, but I've mentioned it in Remarks.

I just hate that the screen was full of E text shaking blind my titanium eyes, anyway, at that time in the case of not knowing that I could save to stream, holding the fearless spirit of hitting the south wall, made a save stream, the miracle happened, at that time that chicken move ah ~ to say more is a tear.

- Man Bi -


Recommended>>
1、Yang Lan AI may become human competitor but human intelligence is irreplaceable
2、Through Bulls and Bears Whether Himalayan or Hysterical
3、Lin County Peoples Congress Office Budget Networking and Supervision Center Construction Project Competitive Negotiation Notice
4、Protection of Seedlings Internet Safety Lesson No 2 Stay away from undesirable information
5、Blockchain is estimated to be worthless for other functions than issuing coins

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号