来自不同文件的 Powershell XML importnode [英] Powershell XML importnode from different file

查看:44
本文介绍了来自不同文件的 Powershell XML importnode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

profile.xml 的内容:

Content of profile.xml:

<files>
  <file folder="CaptureServer" filename="CSConfig" object="CSConfig">
    <Profile name="BBH1200Kofax">
      <OutputCache>\</OutputCache>
      <EncryptedConnectionString>564rgr=</EncryptedConnectionString>
      <ConvertDocsBeforeRelease>false</ConvertDocsBeforeRelease>
    </Profile>
  </file>
  <file folder="CaptureServices3" filename="CSConfig" object="CSConfig">
    <Profile name="BBH1200Kofax">
      <ReleaseToEnterprise>true</ReleaseToEnterprise>
      <CaptureServerUrl />
      <OutputCache />
      <Credentials>
        <EncryptedPassword>46s4rg=</EncryptedPassword>
        <UserName />
        <Domain />
      </Credentials>
      <ConvertDocsBeforeRelease>false</ConvertDocsBeforeRelease>
    </Profile>
  </file>
</files>

rules.xml 的内容:

Content of rules.xml:

<file folder="" filename="Rules" object="ArrayOfIBarcodeRule">
  <Profile name="Test471">
    <IBarcodeRule>
      <RuleName>DOC-TESTTESTTEST-Code128</RuleName>
      <FieldSequenceNumber>1</FieldSequenceNumber>
      <FieldRectangle>
        <Location>
          <X>0</X>
          <Y>0</Y>
        </Location>
        <Size>
          <Width>0</Width>
          <Height>0</Height>
        </Size>
      </FieldRectangle>
      <SeparationValue>TESTTESTTEST</SeparationValue>
    </IBarcodeRule>
  </Profile>
</file>

我正在尝试将 rules.xml(文件节点)的全部内容添加为 profile.xml 中的另一个节点.如您所见,profile.xml 中有一堆其他文件节点,而 rules.xml 将是另一个.

I am trying to add the entire contents of rules.xml (the file node) as another node in profile.xml. As you can see, there are a bunch of other file nodes in the profile.xml, and the rules.xml would be another one.

这是我试过的代码,它似乎没有做任何事情:

This is the code I have tried, and it does not seem to do anything:

$xml = [xml](Get-Content ".\profile.xml")
$newxml = [xml](Get-Content ".\rules.xml")
$xml.ImportNode($newxml.get_DocumentElement(), $true)
$xml.Save(".\profile.xml")

推荐答案

您真的很接近,但 ImportNode 只制作一个副本,实际上并未将复制的节点插入到文档中.试试这个:

You're really close but ImportNode only makes a copy and doesn't actually insert the copied nodes into the document. Try this:

$newNode = $newxml.ImportNode($xml.get_DocumentElement(), $true)
$newxml.DocumentElement.AppendChild($newNode)
$xml.Save("$pwd\profile.xml")  

这篇关于来自不同文件的 Powershell XML importnode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆