Pergunta

I am reading the following file in powershell.

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <nested1>
    <level1 xsi:nil="true" />
    <level2>2</level2>
  </nested1>
  <nested2>
    <level1 xsi:nil="true" />
    <level2>2</level2>
  </nested2>
</root>

using...

[xml]$XmlDoc = get-content $XMLFile

I would like to set $XmlDoc.root.nested1.level2 so it has the attribute xsi:nil="true"

so the file appears as

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <nested1>
    <level1 xsi:nil="true" />
    <level2 xsi:nil="true" />
  </nested1>
  <nested2>
    <level1 xsi:nil="true" />
    <level2>2</level2>
  </nested2>
</root>

Many Thanks for any advice offered.

Foi útil?

Solução

Use SetAttribute() and provide the namespace URI.

$node = $XmlDoc.SelectSingleNode('//nested1/level2')
$node.SetAttribute('nil', 'http://www.w3.org/2001/XMLSchema-instance', 'true') | 
    Out-Null
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top