无法更改具有相同名称的节点的值

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

Can't change value of nodes with same name

问题

以下是您的XML文件示例:

<?xml version="1.0" encoding="ISO-8859-1"?>
<sc>
	<fluxs>
		<flux>
			<cible>
				<id>0</id>
			</cible>
		</flux>
		<flux>
			<cible>
				<id>1</id>
			</cible>
		</flux>
		<flux>
			<cible>
				<id>2</id>
				<wsdl_url>a</wsdl_url>
				<wsdl_url>b</wsdl_url>
			</cible>
		</flux>
	</fluxs>
</sc>

我想要更改“wsdl_url”节点的值。

我尝试了这样做:

$xml = [xml](get-content "c:\mme.xml")

# 这个有效
write-host "Before" $xml.sc.fluxs.flux[2].cible.id
$xml.sc.fluxs.flux[2].cible.id = "Other"
write-host "After" $xml.sc.fluxs.flux[2].cible.id

# 这个不起作用
write-host "Before" $xml.sc.fluxs.flux[2].cible.wsdl_url[0]
$xml.sc.fluxs.flux[2].cible.wsdl_url[0] = "AA"
write-host "After" $xml.sc.fluxs.flux[2].cible.wsdl_url[0]

在第一部分中,在更改id节点的值后,我得到了新值("Other")。
在第二部分中,没有任何更改。出了什么问题?

英文:

Here is my XML file example :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&lt;sc&gt;
	&lt;fluxs&gt;
		&lt;flux&gt;
			&lt;cible&gt;
				&lt;id&gt;0&lt;/id&gt;
			&lt;/cible&gt;
		&lt;/flux&gt;
		&lt;flux&gt;
			&lt;cible&gt;
				&lt;id&gt;1&lt;/id&gt;
			&lt;/cible&gt;
		&lt;/flux&gt;
		&lt;flux&gt;
			&lt;cible&gt;
				&lt;id&gt;2&lt;/id&gt;
				&lt;wsdl_url&gt;a&lt;/wsdl_url&gt;
				&lt;wsdl_url&gt;b&lt;/wsdl_url&gt;
			&lt;/cible&gt;
		&lt;/flux&gt;
	&lt;/fluxs&gt;
&lt;/sc&gt;

I want to change the value of nodes "wsdl_url"

I tried this :

$xml = [xml](get-content &quot;c:\mme.xml&quot;)

# This works
write-host &quot;Before&quot; $xml.sc.fluxs.flux[2].cible.id
$xml.sc.fluxs.flux[2].cible.id = &quot;Other&quot;
write-host &quot;After&quot; $xml.sc.fluxs.flux[2].cible.id

# This does not work
write-host &quot;Before&quot; $xml.sc.fluxs.flux[2].cible.wsdl_url[0]
$xml.sc.fluxs.flux[2].cible.wsdl_url[0] = &quot;AA&quot;
write-host &quot;After&quot; $xml.sc.fluxs.flux[2].cible.wsdl_url[0]

In the first part, after having change que value of the id node, I get the new value ("Other").
In the second part, nothing change. Whats's wrong ?

答案1

得分: 1

根据评论中所述,您可以使用SelectNodes方法来获取所有wsdl_url节点,然后由您决定要为每个节点使用哪些值。例如,以下代码通过索引更新现有节点:

$nodes = $xml.SelectNodes('//cible/wsdl_url')
$nodes[0].'#text' = 'hello'
$nodes[1].'#text' = 'world'
$xml.Save('path\to\new.xml')

然后更新后的XML将如下所示:

<?xml version="1.0" encoding="ISO-8859-1"?>
<sc>
  <fluxs>
    <flux>
      <cible>
        <id>0</id>
      </cible>
    </flux>
    <flux>
      <cible>
        <id>1</id>
      </cible>
    </flux>
    <flux>
      <cible>
        <id>2</id>
        <wsdl_url>hello</wsdl_url>
        <wsdl_url>world</wsdl_url>
      </cible>
    </flux>
  </fluxs>
</sc>
英文:

As stated in comments, you can use SelectNodes method to get all wsdl_url nodes and from there is up to you what values would you like to use for each one of them. i.e. the below updates both existing nodes by index:

$nodes = $xml.SelectNodes(&#39;//cible/wsdl_url&#39;)
$nodes[0].&#39;#text&#39; = &#39;hello&#39;
$nodes[1].&#39;#text&#39; = &#39;world&#39;
$xml.Save(&#39;path\to\new.xml&#39;)

Then the updated XML would look like this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&lt;sc&gt;
  &lt;fluxs&gt;
    &lt;flux&gt;
      &lt;cible&gt;
        &lt;id&gt;0&lt;/id&gt;
      &lt;/cible&gt;
    &lt;/flux&gt;
    &lt;flux&gt;
      &lt;cible&gt;
        &lt;id&gt;1&lt;/id&gt;
      &lt;/cible&gt;
    &lt;/flux&gt;
    &lt;flux&gt;
      &lt;cible&gt;
        &lt;id&gt;2&lt;/id&gt;
        &lt;wsdl_url&gt;hello&lt;/wsdl_url&gt;
        &lt;wsdl_url&gt;world&lt;/wsdl_url&gt;
      &lt;/cible&gt;
    &lt;/flux&gt;
  &lt;/fluxs&gt;
&lt;/sc&gt;

huangapple
  • 本文由 发表于 2023年2月19日 23:21:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501162.html
匿名

发表评论

匿名网友

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

确定