从一个文件复制XML节点并将其附加到另一个XML文件 [英] Copy XML Node From One File And Append It To Another XML File

查看:84
本文介绍了从一个文件复制XML节点并将其附加到另一个XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



在Powershell脚本上需要帮助从一个文件复制XML节点并将其附加到另一个XML文件



目标 :我们的xml文件具有相同的节点结构。但是,每次使用特定值更新来源时,&bbsp; b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  必须手动将其更新为目标xml节点。我们会自动化这个,无论何时来源<
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;文件更新团队中的任何人都会让自动化人员了解它然后自动化人员将执行  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;
 此脚本用最新值更新目标xml节点。





当前情况:  A 成功:当前的Powershell脚本能够读取源xml节点,并且还能够&bbsp
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;删除目标节点,然后使用源xml节点值附加目标xml。



&NBSP; &NBSP; &NBSP; &NBSP;的即可。
失败或错误:异常调用" ImportNode"用"2"表示"2"。参数:"无法导入空节点。"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;在E:\ Carbon \ xmlreplacenode-1.ps1:51 char:1

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; + [void] $ parent.AppendChild($ servicefactoryconfig.ImportNode($ newNode。... ...
                         + ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       ;                 + CategoryInfo     :NotSpecified :( :) [],MethodInvocationException

                         + FullyQualifiedErrorId:DotNetMethodException







Powershell脚本: 







################# ###获取源XML< Section Name =" APILibraries">节点值##########################
$


$ CustomWinClientConfigXmlSource =" SourcePath \Config.xml"
$


[xml] $ SourceConfigXml = Get-Content -Path" $ CustomWinClientConfigXmlSource" -Raw

$ SourceXmlNode = $ SourceConfigXml | Select-Xml -XPath" // Section [@ Name ='APILibraries']"
$
$ SourceXmlOutput = Write-Output" $ SourceXmlNode"

$ SourceXMLNodeValue = " $ SourceXmlOutput"



$
$


##### ###############获取目标XML< Section Name =" APILibraries">节点值并删除它##############
$


$ WinClientConfigFiles =" Config.xml"

$ CustomWinClientConfigXmlTarget =" TargetPath \ $ WinClientConfigFiles"



$ Path =" $ CustomWinClientConfigXmlTarget"



[Xml] $ servicefactoryconfig = Get-Content -Path $ Path -Raw



$ old = $ servicefactoryconfig.SelectSingleNode(" / Configuration / Data / Section [@ Name ='APILibraries']")



$ parent = $ old.ParentNode



[void] $ parent.RemoveChild($ old)





############### ##################附加目标XML< Section Name =" APILibraries">使用源XML节点值###########为


尝试



{



$ newNode = [Xml] @"
$
$ SourceXMLNodeValue

" @



}

$
Catch



{



写错误 - 消息'忽略错误消息'-ErrorAction忽略



}



[void] $ parent.AppendChild($ servicefactoryconfig.ImportNode($ newNode.DocumentElement,$ true))



$ servicefactoryconfig .save($ path)

Hi ALL,

Need Help On Powershell Script For Copy XML Node From One File And Append It To Another XML File.

Objective : We have xml files with same node structure.However, everytime source is updated with certain value 
                   it has to be updated manually into target xml node. We would to automate this one where whenever source
            file is updated anyone from team will let the automation guy know about it and then automation guy will execute                      this script to update the target xml node with laest value.


Current Situation Is A. Success : Current Powershell Script is able to read the source xml node and also able to 
                                      delete target node and then append the target xml with source xml node value.

        B. Failure or Error : Exception calling "ImportNode" with "2" argument(s): "Cannot import a null node."
                                              At E:\Carbon\xmlreplacenode-1.ps1:51 char:1
                                              + [void] $parent.AppendChild($servicefactoryconfig.ImportNode($newNode. ...
                                              + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                              + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
                                              + FullyQualifiedErrorId : DotNetMethodException



Powershell Script : 



#################### Get Source XML <Section Name="APILibraries"> Node Value ##########################

$CustomWinClientConfigXmlSource = "SourcePath\Config.xml"

[xml]$SourceConfigXml = Get-Content -Path "$CustomWinClientConfigXmlSource" -Raw
$SourceXmlNode = $SourceConfigXml | Select-Xml -XPath "//Section[@Name='APILibraries']"
$SourceXmlOutput = Write-Output "$SourceXmlNode"
$SourceXMLNodeValue = "$SourceXmlOutput"




#################### Get The Target XML <Section Name="APILibraries"> Node Value And Delete It ##############

$WinClientConfigFiles = "Config.xml"
$CustomWinClientConfigXmlTarget = "TargetPath\$WinClientConfigFiles"

$Path = "$CustomWinClientConfigXmlTarget"

[Xml]$servicefactoryconfig = Get-Content -Path $Path -Raw

$old = $servicefactoryconfig.SelectSingleNode("/Configuration/Data/Section[@Name='APILibraries']")

$parent = $old.ParentNode

[void] $parent.RemoveChild($old)


################################# Append The Target XML <Section Name="APILibraries"> With Source XML Node Value ###########

Try

{

$newNode = [Xml] @"
$SourceXMLNodeValue
"@

}

Catch

{

Write-Error -Message 'Ignoring The Error Message' -ErrorAction Ignore

}

[void] $parent.AppendChild($servicefactoryconfig.ImportNode($newNode.DocumentElement,$true))

$servicefactoryconfig.save($path)

XML Source&目标:节点名称是APILibaries,它是   < Section Name =" APILibraries" >  < Item
Name = /> 
< / section>

XML Source & Target : Node Name is APILibaries which is  <Section Name="APILibraries"> <Item Name=/> </Section>

源XML文件外观 :    ; &NBSP; &NBSP; &NBSP;   

<?xml version =" 1.0"编码= QUOT; UTF-8英寸?>

<配置>

<数据>

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<Data>

  &NBSP;  < Section Name =" Settings">

  &NBSP; &NBSP; <项目名称= />

  &NBSP;   < Item Name = />

  &NBSP; &NBSP; <项目名称= />

  &NBSP; &NBSP; <项目名称= />

     <Section Name="Settings">
      <Item Name=/>
      <Item Name=/>
      <Item Name=/>
      <Item Name=/>

  &NBSP; &NBSP; <项目名称= />

      <Item Name=/>

  &NBSP; &NBSP;  < Item Name = />

  &NBSP; < / Section>

       <Item Name=/>
    </Section>

< Section Name =" APILibraries">

< Item Name = />

<Section Name="APILibraries">
<Item Name=/>

< / section>

< / section>

< / Data>

< / Configuration>

</Section>
</Section>
</Data>
</Configuration>

目标XML节点外观: 

Target XML Node Looks : 

<?xml version =" 1.0"编码= QUOT; UTF-8英寸?>

<配置>

<数据>

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<Data>

  &NBSP;  < Section Name =" Settings">

  &NBSP; &NBSP; <项目名称= />

  &NBSP;   < Item Name = />

  &NBSP; &NBSP; <项目名称= />

  &NBSP; &NBSP; <项目名称= />

     <Section Name="Settings">
      <Item Name=/>
      <Item Name=/>
      <Item Name=/>
      <Item Name=/>

  &NBSP; &NBSP; <项目名称= />

      <Item Name=/>

  &NBSP; &NBSP;  < Item Name = />

  &NBSP; < / Section>

       <Item Name=/>
    </Section>

< Section Name =" APILibraries">

<项目名称= />

<Section Name="APILibraries">
<Item Name=/>

< / section>

< / section>

< / Data>

< / Configuration>

</Section>
</Section>
</Data>
</Configuration>

推荐答案

请使用编辑工具栏上提供的代码发布工具正确发布您的代码。
Please post your code correctly using the code posting tool provided on the edit toolbar.


这篇关于从一个文件复制XML节点并将其附加到另一个XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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