我的脚本目标是更改Accounts.xml文件中的Pidgin服务器。 当我运行代码时,一切都是最重要的,但是XML中的一个节点现在已经添加了新行,导致帐户崩溃,因为它编辑了一个不应编辑的节点。
我试图创建-查找模式,用新行替换行,没有运气。
XML的结构:
<?xml version='1.0' encoding='UTF-8' ?>
<account version='1.0'>
<account>
<protocol>prpl-jabber</protocol>
<name>name1@old-domain/</name>
<password>123qweASD</password>
<statuses>
<status type='available' name='Available' active='true'>
<attributes/>
</status>
<status type='mood' name='Feeling' active='false'>
<attributes/>
</status>
<status type='freeforchat' name='Chatty' active='false'>
<attributes/>
</status>
<status type='away' name='Away' active='false'>
<attributes/>
</status>
<status type='extended_away' name='Extended away' active='false'>
<attributes/>
</status>
<status type='dnd' name='Do Not Disturb' active='false'>
<attributes/>
</status>
<status type='offline' name='Offline' active='false'>
<attributes/>
</status>
</statuses>
<userinfo></userinfo>
<settings>
<setting name='bosh_url' type='string'></setting>
<setting name='buddy_icon_timestamp' type='int'>0</setting>
<setting name='auth_plain_in_clear' type='bool'>0</setting>
<setting name='custom_smileys' type='bool'>1</setting>
<setting name='port' type='int'>5222</setting>
<setting name='connect_server' type='string'></setting>
<setting name='check-mail' type='bool'>0</setting>
<setting name='buddy_icon'/>
<setting name='ft_proxies' type='string'></setting>
<setting name='connection_security' type='string'>opportunistic_tls</setting>
<setting name='use-global-buddyicon' type='bool'>1</setting>
</settings>
<settings ui='gtk-gaim'>
<setting name='auto-login' type='bool'>0</setting>
</settings>
<current_error/>
</account>
<account>
<protocol>prpl-jabber</protocol>
<name>name2@old-domain/</name>
<password>123qweASD</password>
<statuses>
<status type='available' name='Available' active='true'>
<attributes/>
</status>
<status type='mood' name='Feeling' active='false'>
<attributes/>
</status>
<status type='freeforchat' name='Chatty' active='false'>
<attributes/>
</status>
<status type='away' name='Away' active='false'>
<attributes/>
</status>
<status type='extended_away' name='Extended away' active='false'>
<attributes/>
</status>
<status type='dnd' name='Do Not Disturb' active='false'>
<attributes/>
</status>
<status type='offline' name='Offline' active='false'>
<attributes/>
</status>
</statuses>
<userinfo></userinfo>
<settings>
<setting name='bosh_url' type='string'></setting>
<setting name='buddy_icon_timestamp' type='int'>0</setting>
<setting name='auth_plain_in_clear' type='bool'>0</setting>
<setting name='custom_smileys' type='bool'>1</setting>
<setting name='port' type='int'>5222</setting>
<setting name='connect_server' type='string'></setting>
<setting name='check-mail' type='bool'>0</setting>
<setting name='buddy_icon'/>
<setting name='ft_proxies' type='string'></setting>
<setting name='connection_security' type='string'>opportunistic_tls</setting>
<setting name='use-global-buddyicon' type='bool'>1</setting>
</settings>
<settings ui='gtk-gaim'>
<setting name='auto-login' type='bool'>0</setting>
</settings>
<current_error/>
</account>
</account>
$path="$Env:USERPROFILE\AppData\Roaming\.purple\"
$file="$path\accounts.xml"
$myxml = [xml](gc -Encoding utf8 $path\accounts.xml)
$myxml.SelectNodes('//account/account/name') | foreach {
$account = $_.'#text'.split('@')[0]
$server = $_.'#text'.split('@')[1]
Write-Host "$account @ $server"
$_.'#text'= $account+'@'+$server -Replace "old.domain/","new-domain/"
}
$myxml.Save($file)
Stop-Process -processname pidgin -Force
Start-Sleep -s 1
Start-Process -FilePath "C:\Program Files (x86)\Pidgin\pidgin.exe"
Start-Process -FilePath "$Env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Pidgin\pidgin.exe"
Write-Host "OK"
在运行时,所有条目的account \ account \ name中的数据都会更改。 但是现在,对于设置中的每个条目,对于bosch url,Powershell都会创建“七个”空格“”和换行,从而导致换行,该行应类似于:
<setting name='bosh_url' type='string'></setting>
显示为:
<setting name='bosh_url' type='string'>
</setting>
但是在代码中,我什至没有接触设置节点。
谢谢
答案 0 :(得分:-1)
尝试:
$xml = New-Object System.Xml.XmlDocument
$xml.PreserveWhitespace = $true
$xml.Load($file)
...do something
$xml.Save($file)