Я хочу добавить новые элементы в существующий файл XML. Но я думаю, что я не изменил существующий скрипт правильно.
PowerShell:
$pathToConfig = "C:\Temp\Config.xml"
#force the config into an XML
$xml = (get-content $pathToConfig)
#find the node to insert after
$foundNode = $xml.ACConfig.KfxDB
#build new node by hand and force it to be an XML object
$newNode = @"
<EBCAcquisition RestoreScannerSource="0" />
<ACIServer RemoteSite="1" DisableSharingProfiles="1">
<TransferTimeout>300</TransferTimeout>
</ACIServer>
"@
#add new node AFTER the configsections node
$newNode = $xml.ImportNode($newNode.appSettings,$true)
$xml.configuration.InsertAfter($newNode,$foundNode) |out-null
#save file
$xml.Save("$pathToConfig.CHANGED.XML")
XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ACConfig>
<Services>
<Licensing>
<Service Key="Licensing\Licensing Service" Host="Y1234567" Protocol="http" Port="4949" />
</Licensing>
</Services>
<Reporting Enable="0" />
<CurrentVersion>11.0</CurrentVersion>
<KfxDB>
<SQL-Server>
<Database>Database</Database>
<StandardInstance>Test</StandardInstance>
<UserID>sa</UserID>
<Password>1234567</Password>
<AuthenticationMode>SqlServer</AuthenticationMode>
</SQL-Server>
<IsDatabaseInstalled>1</IsDatabaseInstalled>
</KfxDB>
</ACConfig>
Сообщение об ошибке:
Не удалось вызвать метод, поскольку [System.String] не содержит метод с именем «ImportNode». В строке: 18 символов: 1 + $ newNode = $ xml.ImportNode ($ newNode.appSettings, $ true) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo: InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId: MethodNotFound It is not possible to call a method for an expression that has the NULL. In line: 19
characters: 1
+ $ xml.configuration.InsertAfter ($ newNode, $ foundNode) | out-null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo: InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId: InvokeMethodOnNull Error calling the method because [System.String] does not contain a method named Save.
В строке: 22 символа: 1 + $ xml.Save ("$ pathToConfig.CHANGED.XML") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: InvalidOperation: (:) [],RuntimeException + FullyQualifiedErrorId: MethodNotFound