xml - Endless loop when read .xml-file
问题描述
I made a PowerShell script to read and write infos/settings to an .xml file.
Param(
[string]$mode,
[string]$set,
[string]$xml
)
function readSettings([string]$xmlfile, [string]$setting)
{
$s = readSettings $xmlfile
$v = $s[$setting]
Write-Host $v
}
function exportSettings([string]$xmlfile)
{
$xmlDoc = New-Object XML
$xmlDoc.Load($xmlfile)
$settings = @{}
$xmlDoc.settings.ChildNodes | %{$settings[$_.name] = $_.firstChild.Value}
return $settings
}
function importSettings([hashtable]$ht,[string]$xmlFile){
$xmlDoc = New-Object XML
$xmlDoc.Load($xmlFile)
foreach ($key in $ht.keys){
$settingNode = $xmlDoc.SelectSingleNode("/settings/setting[@name='$key']")
if ($settingNode){
$settingNode.firstChild.Value = $ht[$key]
}else{
$newNode = $xmlDoc.settings.setting[0].Clone()
$newNode.name = $key
$newNode.firstChild.Value = $ht[$key]
$xmlDoc.settings.appendChild($newNode)
}
}
$xmlDoc.Save($xmlFile)
}
if($mode -eq "read")
{
readSettings($xml, $set)
}
if ($mode -eq "write")
{
}
(Also on GitHub.)
Whenever I read an .xml file, it generates an endless loop with RAM consumption up to 2GB.
I thought that
$xmlDoc.settings.ChildNodes | %{$settings[$_.name] = $_.firstChild.Value}
could be the reason, but I don't know how to solve it. Writing to a xml-file works perfectly fine. Can anyone help?
解决方案
I think that readSettings
function call itself without test, and that's enough to loop I think.
function readSettings([string]$xmlfile, [string]$setting)
{
$s = readSettings $xmlfile
$v = $s[$setting]
Write-Host $v
}
推荐阅读
- spring - 类中的字段值不会使用 Spring Boot MVC 控制器更新
- json - 请求 JSON 不会自动映射到 DTO 或 VO 对象
- c# - c# 使用 Process.Start(ProcessStartInfo info) 的程序不起作用
- php - 具有动态类名的 PHP 简单 HTML Dom 访问元素
- javascript - 防止同域 iframe 访问周围窗口的范围
- c# - 为什么在 C# 中 (++x - --x) 的答案对于 x 的每个值总是 1?为什么不是0?
- sql - 在 Postgres 中更改列长度时出错
- python - 为什么python3在yocto中内置?
- powershell - 为什么 $host.UI.RawUI.ReadKey() 方法会捕获未真正按下的 Enter 键?
- javascript - 从组件导航到不同的组件 [React]