I am trying to add attributes to an XML root node. Using the Master-PowerShell book, I could successfully add attributes to the root node.
However, the xsi
prefix disappears. I am sure it's something silly I have missed, grateful if I could please get some help. Below is my scenario.
XML file before modification:
<?xml version="1.0" encoding="UTF-8"?>
<InvoiceBatch>
...
</InvoiceBatch>
Code I wrote in PowerShell to add new attributes (modify XML):
$xmldata = New-Object XML
$xmldata.Load("c:\users\SFDC_batch_0201.xml")
$xmldata.InvoiceBatch.SetAttribute("batchSource","SFDC")
$xmldata.InvoiceBatch.SetAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
$xmldata.InvoiceBatch.SetAttribute("xsi:noNameSpaceSchemaLocation","g:\TrgFiles\xsd\invoice.xsd")
$xmldata.Save("c:\users\SFDC_batch_0201.xml")
I get below output:
<?xml version="1.0" encoding="UTF-8"?>
<InvoiceBatch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="g:\TrgFiles\xsd\invoice.xsd" batchSource="SFDC">
...
</InvoiceBatch>
Output XML (expected result):
<?xml version="1.0" encoding="UTF-8"?>
<InvoiceBatch batchSource="SFDC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNameSpaceSchemaLocation="g:\TrgFiles\xsd\invoice.xsd" >
...
</InvoiceBatch>
Did lot of googling, but was unable to find something related to my scenario. There were using C# etc., but not powershell. Pardon me if I missed any information as part of my search.