user1654136 user1654136 - 1 month ago 8
C# Question

Add a new element to xml using powershell

I have an XML file like given below

<xml xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product>


<Feature Id="f1" Absent="qqq" Title="test" Level="1">

<ComponentGroupRef Id="ComponentGroupRef1" />
<ComponentGroupRef Id="ComponentGroupRef2" />
<ComponentGroupRef Id="ComponentGroupRef3" />
<ComponentGroupRef Id="ComponentGroupRef4" />
<ComponentGroupRef Id="WindowsFolder1" />
<ComponentGroupRef Id="ComponentGroupRef5" />
<ComponentGroupRef Id="ComponentGroupRef6" />
<ComponentGroupRef Id="ComponentGroupRef7" />
<Feature Id="f2" Display="hidden">
<ComponentRef Id="Component1" />
</Feature>
<FeatureGroupRef Id ="fg1"/>
</Feature>

</Product>

</xml>


Need to add an element between first Feature tag feature id=f1. The element format i want to add is given below

<ComponentGroupRef Id="Mycomponentname" />.


The resultant xml should look like below

<Feature Id="f1" Absent="qqq" Title="test" Level="1">

<ComponentGroupRef Id="ComponentGroupRef1" />
<ComponentGroupRef Id="ComponentGroupRef2" />
<ComponentGroupRef Id="ComponentGroupRef3" />
<ComponentGroupRef Id="ComponentGroupRef4" />
<ComponentGroupRef Id="WindowsFolder1" />
<ComponentGroupRef Id="ComponentGroupRef5" />
<ComponentGroupRef Id="ComponentGroupRef6" />
<ComponentGroupRef Id="ComponentGroupRef7" />
<ComponentGroupRef Id="Mycomponentname" />
<Feature Id="f2" Display="hidden">
<ComponentRef Id="Component1" />
</Feature>
<FeatureGroupRef Id ="fg1"/>
</Feature>

</Product>

</xml>


I tried the following code for adding the element but it is failing

$filePath="C:\Filename.xml"
[xml]$doc=Get-Content $filePath
$x= $doc.CreateElement("ComponentGroupRef")
$x.SetAttribute('id','Mycomponentname')
$doc.Product.Feature.AppendChild($x)


The error i am getting at the last line of code is given below

You cannot call a method on a null-valued expression.

Answer

Don't forget the xml tag.

$filePath="C:\Filename.xml"
[xml]$doc=Get-Content $filePath
$x= $doc.CreateElement("ComponentGroupRef")
$x.SetAttribute('id','Mycomponentname')
$doc.xml.Product.Feature.AppendChild($x)

One note is since it's appended, it'll show up at the end after fg1. Something like this:

<xml xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product>
    <Feature Id="f1" Absent="qqq" Title="test" Level="1">
      <ComponentGroupRef Id="ComponentGroupRef1" />
      <ComponentGroupRef Id="ComponentGroupRef2" />
      <ComponentGroupRef Id="ComponentGroupRef3" />
      <ComponentGroupRef Id="ComponentGroupRef4" />
      <ComponentGroupRef Id="WindowsFolder1" />
      <ComponentGroupRef Id="ComponentGroupRef5" />
      <ComponentGroupRef Id="ComponentGroupRef6" />
      <ComponentGroupRef Id="ComponentGroupRef7" />
      <Feature Id="f2" Display="hidden">
        <ComponentRef Id="Component1" />
      </Feature>
      <FeatureGroupRef Id="fg1" />
      <ComponentGroupRef id="Mycomponentname" xmlns="" />
    </Feature>
  </Product>
</xml>