1

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.

1 Answer 1

1

Don't forget the xml tag.

$filePath="C:\Filename.xml"
[xml]$doc=Get-Content $filePath
$x= $doc.CreateElement("ComponentGroupRef", $doc.xml.Product.NamespaceURI)
$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" />
    </Feature>
  </Product>
</xml>
Sign up to request clarification or add additional context in comments.

7 Comments

How are you saving it? Something like this? $doc.save("C:\Filename.new.xml")
yes i am saving it using $doc.save("C:\Filename.xml")
What about this? $doc = $doc.OuterXml.Replace("xmlns=`"http://schemas.microsoft.com/wix/2006/wi`"", "")
no Ben,it's not working. I only want to remove the xmlns=" " from <ComponentGroupRef id="Mycomponentname" xmlns="" /> and not from the root.Thanks Ben for helping me
I got it correct by using $doc=[xml] $doc.OuterXml.Replace(" xmlns="'", "")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.