2012-03-30 28 views
5

Nội dung profile.xml:Powershell XML importnode từ tập tin khác nhau

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

Nội dung 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> 

Tôi cố gắng để thêm toàn bộ nội dung của rules.xml (file node) như một nút khác trong tệp tin profile.xml. Như bạn có thể thấy, có một loạt các nút tập tin khác trong tệp tin cấu hình, và tệp rules.xml sẽ là một nút khác.

Đây là mã tôi đã cố gắng, và nó dường như không làm bất cứ điều gì:

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

Trả lời

13

Bạn thực sự sắp nhưng ImportNode chỉ làm cho một bản sao và không thực sự chèn các nút sao chép vào tài liệu. Hãy thử điều này:

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

Hoạt động hoàn hảo, cảm ơn bạn! – SeanM

Các vấn đề liên quan