英文:
Writing values from .csv file to .xml using PowerShell. My script isn't working - critique my code
问题
这是您的翻译:
# 加载csv文件
$csv = Import-Csv -Path "C:\sourcelabels.csv"
# 加载xml文件
$xml = [xml](Get-Content -Path "C:\labelfile.xml")
# 循环遍历csv文件中的每一行
foreach ($row in $csv) {
# 查找xml文件中与列A中的值匹配的元素
$elements = $xml.SelectNodes("//element[propertyA='$($row.'Column A')']")
# 用列B中的值替换<name>元素中的值
foreach ($element in $elements) {
$element.SelectSingleNode("name").InnerText = $row.'Column B'
}
}
# 保存更新后的xml文件
$xml.Save("C:\labelfile.xml")
希望这对您有所帮助。
英文:
I'm trying to automate editing of an xml file using a Powershell script. The script needs to use a .csv file as a data source. The .csv has two columns. A has the original values, B has the replacement values.
Because there are duplicates in the .xml file, I only want to replace values between a particular element, in this case the name element.
I tried writing this script (see code) but the replacement process doesn't work. I don't receive an error message when running the script, but when I open the .xml file after executing it, none of the target values have been changed to the values shown in the .csv file.
The expected behaviour would be that any values contained in the name element will have been changed to the values listed under column B in the .csv file.
# Load the csv file
$csv = Import-Csv -Path "C:\sourcelabels.csv"
# Load the xml file
$xml = [xml](Get-Content -Path "C:\labelfile.xml")
# Loop through each row in the csv file
foreach ($row in $csv) {
# Find the elements in the xml file that match the value in column A
$elements = $xml.SelectNodes("//element[propertyA='$($row.'Column A')']")
# Replace the value in the <name> element with the value in column B
foreach ($element in $elements) {
$element.SelectSingleNode("name").InnerText = $row.'Column B'
}
}
# Save the updated xml file
$xml.Save("C:\labelfile.xml")
答案1
得分: 1
Sure, here is the translated content:
如果你的 labelfile.xml 看起来像这样:
<labels>
<label>
<element propertyA="changeMe">
<name>oldValue</name>
</element>
</label>
<label>
<element propertyA="DoNotChange">
<name>KeepMyName</name>
</element>
</label>
<label>
<element propertyA="changeThisOneToo">
<name>UpdateThisName</name>
</element>
</label>
</labels>
以及你的 sourcelabels.csv 文件如下:
"Column A","Column B"
"changeMe","NewValue"
"changeThisOneToo","This Name Has Been Updated"
那么以下的代码应该可以工作:
# 加载 csv 文件
$csv = Import-Csv -Path "C:\sourcelabels.csv"
# 加载 xml 文件 (这种方式将使用文件的正确编码)
$xml = [System.Xml.XmlDocument]::new()
$xml.Load("C:\labelfile.xml")
# 获取元素节点的集合
$elementNodes = $xml.SelectNodes("//element")
# 遍历 csv 文件中的每一行
foreach ($row in $csv) {
# 查找 xml 文件中属性 propertyA 与列 A 中的值匹配的元素
$elementNodes | Where-Object { $_.propertyA -eq $row.'Column A' } | ForEach-Object {
$_.Name = $row.'Column B'
}
}
# 保存更新后的 xml 文件
$xml.Save("C:\labelfile.xml")
请注意,这是原文的翻译,包括代码和标签。
英文:
If your labelfile.xml looks anything like this:
<labels>
<label>
<element propertyA="changeMe">
<name>oldValue</name>
</element>
</label>
<label>
<element propertyA="DoNotChange">
<name>KeepMyName</name>
</element>
<label>
</label>
<element propertyA="changeThisOneToo">
<name>UpdateThisName</name>
</element>
</label>
</labels>
and your sourcelabels.csv file looks like
"Column A","Column B"
"changeMe","NewValue"
"changeThisOneToo","This Name Has Been Updated"
Then below should work:
# Load the csv file
$csv = Import-Csv -Path "C:\sourcelabels.csv"
# Load the xml file (this way will use the correct encoding of the file)
$xml = [System.Xml.XmlDocument]::new()
$xml.Load("C:\labelfile.xml")
# get a collection of element nodes
$elementNodes = $xml.SelectNodes("//element")
# Loop through each row in the csv file
foreach ($row in $csv) {
# Find the elements in the xml file whose propertyA attribute match the value in column A
$elementNodes | Where-Object { $_.propertyA -eq $row.'Column A' } | ForEach-Object {
$_.Name = $row.'Column B'
}
}
# Save the updated xml file
$xml.Save("C:\labelfile.xml")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论