【问题标题】:Add a new element to xml using powershell使用 powershell 向 xml 添加新元素
【发布时间】:2017-03-11 12:07:45
【问题描述】:

我有一个如下所示的 XML 文件

<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>

需要在第一个Feature标签feature id=f1之间添加一个元素。我要添加的元素格式如下所示

<ComponentGroupRef Id="Mycomponentname" />.

生成的 xml 应如下所示

<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>

我尝试了以下代码来添加元素,但失败了

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

我在最后一行代码中遇到的错误如下所示

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

【问题讨论】:

    标签: c# xml powershell


    【解决方案1】:

    不要忘记 xml 标签。

    $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)
    

    一个注释是因为它是附加的,它会在fg1之后出现在最后。像这样的:

    <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>
    

    【讨论】:

    • 你是如何保存它的?像这样的东西? $doc.save("C:\Filename.new.xml")
    • 是的,我正在使用 $doc.save("C:\Filename.xml") 保存它
    • 这个怎么样? $doc = $doc.OuterXml.Replace("xmlns=`"http://schemas.microsoft.com/wix/2006/wi`"", "")
    • 不,本,它不工作。我只想从 中删除 xmlns="" 而不是从根目录中删除。感谢 Ben 帮助我
    • 我通过使用 $doc=[xml] $doc.OuterXml.Replace(" xmlns="'", "") 得到了正确的结果
    猜你喜欢
    • 1970-01-01
    • 2017-10-22
    • 2018-12-07
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多