我計劃為我的小型 powershell 應用程式創建配置 xml 檔案。為此,我創建了一個初始化程序,該程序輪詢用戶,然后根據用戶輸入的資料生成組態檔。主程式會從中獲取資料,而不是每次啟動時。該程序應創建一個具有以下結構的檔案:
<CamInfoSettings>
<Application>
<AppFolder>C:\Temp\01</AppFolder>
<PictureFolder>images</PictureFolder>
<LogFiles>CamInfo.log</LogFiles>
</Application>
<Sections>
<Count>1</Count>
<Section id="1">
<Name>FirstSection Name</Name>
<Description>FirstSection Description</Description>
<SectionNetworksCount>2</SectionNetworksCount>
<FileName>C:\Temp\01\Section1.config</FileName>
<SectionIpNetworks>
<SectionIpNetwork id="1">
<Network>192.168.12.</Network>
<StartIp>22</StartIp>
<FinishIp>99<FinishIp>
<SectionIpNetwork id="2">
<Network>192.168.13.</Network>
<StartIp>1</StartIp>
<FinishIp>254<FinishIp>
</SectionIpNetworks>
</Section>
我學習了如何使用來自該站點或 Internet 的示例創建 xml,但是我被困在需要在 SectionIpNetworks 部分創建多個子元素的部分。由于有許多網路可以與主程式一起使用,我想創建一個完全具有這種結構的檔案。我確定我可以創建一個或多個網路,但我不能為它們分配“id”屬性并在每個網路中創建子項。
我請你幫忙舉一個小例子。我的程式代碼詢問用戶網路的數量,然后在回圈中他必須添加專案。下面是一個創建分支的代碼示例,但進一步的作業停滯不前。我很可能犯了某種全域錯誤,我已經好幾天沒弄明白了。我的代碼:
$SectionEnumber = [int]$Configfile.CamInfoSettings.Sections.Count 1
$Configfile.SelectSingleNode("CamInfoSettings/Sections/Count").InnerText = $SectionEnumber
$newSectionNode = $Configfile.CamInfoSettings.Sections.AppendChild($Configfile.CreateElement("Section"))
$newSectionNode.SetAttribute("id",$SectionEnumber)
$newSectionName = $newSectionNode.AppendChild($Configfile.CreateElement("Name"))
$newSectionName.AppendChild($Configfile.CreateTextNode($SectionName)) | Out-Null
$newDescription = $newSectionNode.AppendChild($Configfile.CreateElement("Description"))
$newDescription.AppendChild($Configfile.CreateTextNode($SectionDescription)) | Out-Null
$newSegmentsCount = $newSectionNode.AppendChild($Configfile.CreateElement("SectionNetworksCount"))
$newSegmentsCount.AppendChild($Configfile.CreateTextNode($SectionNetworksCount)) | Out-Null
$newFileName = $newSectionNode.AppendChild($Configfile.CreateElement("FileName"))
$newFileName.AppendChild($Configfile.CreateTextNode($WritePath "\" "Section$SectionEnumber.config")) | Out-Null
$newSectionNode.AppendChild($Configfile.CreateElement("SectionNetworks")) | Out-Null
$newNetworksNode = $Configfile.SelectSingleNode("CamInfoSettings/Sections/Section[@id=$SectionEnumber]/SectionNetworks")
$newNetworksNode.SetAttribute("count",$SectionNetworksCount)
foreach ($item in 1..$SectionNetworksCount) {
$newNetworksNode.AppendChild($Configfile.CreateElement("SectionNetwork")) |Out-Null
Here i must create Elements for SectionIpNetwork and set attruibute id=$item
}
uj5u.com熱心網友回復:
我認為當您嘗試將 XML 作為配置格式時,您會受到很大傷害。我建議切換到 JSON。
這相當于您的配置結構。
{
"Application": {
"AppFolder": "C:\\Temp\\01",
"PictureFolder": "images",
"LogFiles": "CamInfo.log"
},
"Sections": [{
"id": 1,
"Name": "FirstSection Name",
"Description": "FirstSection Description",
"FileName": "C:\\Temp\\01\\Section1.config",
"SectionIpNetworks": [{
"id": 1,
"Network": "192.168.12.",
"StartIp": 22,
"FinishIp": 99
},
{
"id": 2,
"Network": "192.168.13.",
"StartIp": 1,
"FinishIp": 254
}
]
}]
}
閱讀它很簡單:
$config = Get-Content config.json -Raw -Encoding UTF8 | ConvertFrom-Json
訪問它很簡單(1):
$appFolder = $config.Application.AppFolder
$section = $config.Sections | Where id -eq 1
改變它很簡單(2):
# adding an array entry
$section.SectionIpNetworks = [pscustomobject]@{
id = 3
Network = "192.168.14."
StartIp = 1
FinishIp = 254
}
# removing an array entry
$section.SectionIpNetworks = $section.SectionIpNetworks | Where id -ne 1
撰寫它很簡單(3):
$config | ConvertTo-Json -Depth 10 | Set-Content config.json -Encoding UTF8
(1)在這種情況下,該部分實際上對 XML 和 JSON 的作業方式完全相同。
(2)這里需要從 PowerShell 哈希 ( @{}) 到[pscustomobject]強制轉換。但是您可以看到原生 PowerShell 資料結構轉換為 JSON 是多么容易。
(3)的-Depth引數是重要的。此外,PowerShell 對如何格式化 JSON 有一些非常規的想法,但它是可以管理的。
作為一般提示:不要將不言而喻的東西存盤為配置選項。您不需要存盤部分計數或網路計數的值。如果您需要知道有多少,請計算它們:
$numSections = $config.Sections.Length
Storing these things only leads to bugs once they get out of sync with reality for whatever reason.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/321711.html
上一篇:從包含XML資料的特定列創建表
