如何写入或读取XML配置文件(.config) [英] how to write or read from XML config file (.config)

查看:503
本文介绍了如何写入或读取XML配置文件(.config)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在安装后的部分中写一个配置文件,如果存在较旧版本的产品,则从配置文件中读取。

I have to write a config file in the post section of installation and read from the config file if older version of the product exists.

配置文件是XML配置文件(.config)
配置文件将有很多条目,例如

config file is XML Configuration File (.config) config file will have so many entries like

<name>
   abcd
</name>
<company>
    xyz
</company>
<choise>
   choise1
</choise>

如何仅读取选择标签的文本和仅覆盖选择标签的文本。

How to read only choise tag's text and overwrite only choise tag's text.

推荐答案

NSIS总共有4个XML插件可供选择; NsisXML(由Wizou提供) XML插件 NsisXML(由Joel提供) NsXML

NSIS has a total of 4 XML plug-ins to choose from; NsisXML (by Wizou), XML plug-in, NsisXML (by Joel) and NsXML

使用NsisXML(由Wizou提供):

Using NsisXML (by Wizou):

Outfile "$%temp%\NSISTest.exe"
RequestExecutionLevel user
Installdir "$Temp"
Showinstdetails show
!include LogicLib.nsh
Page InstFiles

!define XMLFILE "$instdir\myxml.xml"

Section
StrCpy $9 "Did not exist"
nsisXML::create
nsisXML::load "${XMLFILE}"
${If} $0 = 0
    ;build a new basic XML file:
    nsisXML::create
    nsisXML::createProcessingInstruction "xml" 'version="1.0" encoding="UTF-8" standalone="yes"'
    nsisXML::appendChild
    nsisXML::release $2
${EndIf}
nsisXML::select '/choise'
${If} $2 = 0
    StrCpy $1 $0
    nsisXML::createElement "choise"
    nsisXML::appendChild
${Else}
    nsisXML::getText
    StrCpy $9 $3
${EndIf}
DetailPrint "Old value: $9"
System::Call 'kernel32::GetTickCount()i.r5' ;Get some "random" value to save
nsisXML::setText "$5"
nsisXML::release $2
nsisXML::save "${XMLFILE}"
nsisXML::release $0
DetailPrint "Saved new value: $5"
SectionEnd

第一次运行,我得到:

Old value: Did not exist
Saved new value: 709289703
Completed

并在第二次运行中得到:

and on the second run I got:

Old value: 709289703
Saved new value: 709308687
Completed

这篇关于如何写入或读取XML配置文件(.config)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆