Writing values from .csv file to .xml using PowerShell. My script isn't working – critique my code

huangapple go评论57阅读模式
英文:

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 &quot;C:\sourcelabels.csv&quot;

# Load the xml file
$xml = [xml](Get-Content -Path &quot;C:\labelfile.xml&quot;)

# 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(&quot;//element[propertyA=&#39;$($row.&#39;Column A&#39;)&#39;]&quot;)

  # Replace the value in the &lt;name&gt; element with the value in column B
  foreach ($element in $elements) {
    $element.SelectSingleNode(&quot;name&quot;).InnerText = $row.&#39;Column B&#39;
  }
}

# Save the updated xml file
$xml.Save(&quot;C:\labelfile.xml&quot;)

答案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:

&lt;labels&gt;
	&lt;label&gt;
		&lt;element propertyA=&quot;changeMe&quot;&gt;
			&lt;name&gt;oldValue&lt;/name&gt;
		&lt;/element&gt;
	&lt;/label&gt;
	&lt;label&gt;
		&lt;element propertyA=&quot;DoNotChange&quot;&gt;
			&lt;name&gt;KeepMyName&lt;/name&gt;
		&lt;/element&gt;
	&lt;label&gt;
	&lt;/label&gt;
		&lt;element propertyA=&quot;changeThisOneToo&quot;&gt;
			&lt;name&gt;UpdateThisName&lt;/name&gt;
		&lt;/element&gt;
	&lt;/label&gt;
&lt;/labels&gt;

and your sourcelabels.csv file looks like

&quot;Column A&quot;,&quot;Column B&quot;
&quot;changeMe&quot;,&quot;NewValue&quot;
&quot;changeThisOneToo&quot;,&quot;This Name Has Been Updated&quot;

Then below should work:

# Load the csv file
$csv = Import-Csv -Path &quot;C:\sourcelabels.csv&quot;

# Load the xml file (this way will use the correct encoding of the file)
$xml = [System.Xml.XmlDocument]::new()
$xml.Load(&quot;C:\labelfile.xml&quot;)
# get a collection of element nodes
$elementNodes = $xml.SelectNodes(&quot;//element&quot;)

# 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.&#39;Column A&#39; } | ForEach-Object {
        $_.Name = $row.&#39;Column B&#39;
    }
}

# Save the updated xml file
$xml.Save(&quot;C:\labelfile.xml&quot;)

huangapple
  • 本文由 发表于 2023年4月6日 22:16:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定