如何在powershell中从哈希表值中追加xml

时间:2012-01-07 05:39:41

标签: xml powershell powershell-v2.0

我有一个哈希表,其中组件名称为Key,Baseline为Values。

以下行描述了我的哈希表

Name                           Value                                                                                                                           
----                           -----                                                                                                                                       
Comp_Bin                       Comp_12_23_2011.1276                                                                            
Complicen                      Comp_11_2_2011.461                                               
SupportComp                    Comp_2.1.0.17.1135                                                                                                  

我想将此值添加到已有的xml文件

<Component>
    <Name></Name>
    <Baseline></Baseline>
    <KLOC>0</KLOC>
    <IsCount>True</IsCount>
</Component>

将哈希值添加到xml并按如下所示进行

<Components>
   <Name>Comp_Bin</Name>
   <Baseline>Comp_12_23_2011.1276</Baseline>
   <KLOC>0</KLOC>
   <IsCount>True</IsCount>
</Component>

如何将哈希表转换为xml?请帮帮我。

我已经像下面一样解析了我的哈希表,但是下面的代码失败了,因为每次都必须复制XML内容然后单独更改Name和Baseline元素

$CCountDoc= [XML] (Get-Content "ccount.xml")
    foreach($key in $($hash.keys)){

     $Baseline = $hash[$key]
     $Name= $key 

     $CCountDoc.Name=$Name
     $CCountDoc.Baseline=$Baseline
    }

3 个答案:

答案 0 :(得分:2)

这似乎正在做你需要的事情:

clear 
$hash = @{ "Comp_Bin" = "Comp_12_23_2011.1276"; "Complicen" = "Comp_11_2_2011.461"; "SupportComp" = "Comp_2.1.0.17.1135" }

$xml = [xml] "<root namespace=`"namespace`"></root>" 
foreach($key in $hash.Keys) 
{ 
$insert = [xml] [string]::Format("<Component> 
    <Name>{0}</Name> 
    <Baseline>{1}</Baseline> 
    <KLOC>0</KLOC> 
    <IsCount>True</IsCount> 
    </Component> 
    ", $key, $hash[$key]) 
$importNode = $xml.ImportNode($insert.DocumentElement, $true)  
$xml.root.AppendChild($importNode) |Out-Null 
} 
$xml.Save("c:\PST\result.xml") 

这将是输出文件:

<root namespace="namespace">
  <Component>
    <Name>Complicen</Name>
    <Baseline>Comp_11_2_2011.461</Baseline>
    <KLOC>0</KLOC>
    <IsCount>True</IsCount>
  </Component>
  <Component>
    <Name>SupportComp</Name>
    <Baseline>Comp_2.1.0.17.1135</Baseline>
    <KLOC>0</KLOC>
    <IsCount>True</IsCount>
  </Component>
  <Component>
    <Name>Comp_Bin</Name>
    <Baseline>Comp_12_23_2011.1276</Baseline>
    <KLOC>0</KLOC>
    <IsCount>True</IsCount>
  </Component>
</root>

答案 1 :(得分:2)

这将处理现有文件。这是您的输入文件:

<Components>
  <Component>
    <Name></Name>
    <Baseline></Baseline>
    <KLOC>0</KLOC>
    <IsCount>True</IsCount>
  </Component>
</Components>

将转变为:

<Components>
  <Component>
    <Name>Complicen</Name>
    <Baseline>Comp_11_2_2011.461</Baseline>
    <KLOC>0</KLOC>
    <IsCount>True</IsCount>
  </Component>
  <Component>
    <Name>SupportComp</Name>
    <Baseline>Comp_2.1.0.17.1135</Baseline>
    <KLOC>0</KLOC>
    <IsCount>True</IsCount>
  </Component>
  <Component>
    <Name>Comp_Bin</Name>
    <Baseline>Comp_12_23_2011.1276</Baseline>
    <KLOC>0</KLOC>
    <IsCount>True</IsCount>
  </Component>
</Components>

这是代码:

clear 
$hash = @{ "Comp_Bin" = "Comp_12_23_2011.1276"; "Complicen" = "Comp_11_2_2011.461"; "SupportComp" = "Comp_2.1.0.17.1135" }
$keys = New-Object object[] $hash.Count
$hash.Keys.CopyTo($keys,0) 

$xml = [xml] [System.IO.File]::ReadAllText("c:\pst\1.xml")
$node = $xml.Components.Component.Clone()

$xml.Components.Component.Name = $keys[0]
$xml.Components.Component.Baseline = $hash[$keys[0]]

for($i = 1; $i -lt $hash.Count; $i++) 
{ 
$node.Name = $keys[$i]
$node.Name

$node.Baseline = $hash[$keys[$i]]
$importNode = $xml.ImportNode($node, $true)  
$xml.Components.AppendChild($importNode) | Out-Null 
} 
$xml.Save("c:\PST\result.xml") 

答案 2 :(得分:1)

以下是使用XmlDocument的方法:

Clear-Host
$hTable = @{"Comp_Bin"="Comp_12_23_2011.1276";"Complicen"="Comp_11_2_2011.461";"SupportComp"="Comp_2.1.0.17.1135"}

# Create XML root
[xml]$xmlDoc = New-Object system.Xml.XmlDocument
$xmlDoc.LoadXml("<?xml version=`"1.0`" encoding=`"utf-8`"?><Root></Root>")


foreach($entry in $hTable.keys)
{
  Write-Host 
  # Create a text nod
  $xmlCpn = $xmlDoc.CreateElement("Component")

  $xmlElt = $xmlDoc.CreateElement("Name")
  $xmlText = $xmlDoc.CreateTextNode($entry)
  $null = $xmlElt.AppendChild($xmlText)
  $null = $xmlCpn.AppendChild($xmlElt)

  $xmlElt = $xmlDoc.CreateElement("Baseline")
  $xmlText = $xmlDoc.CreateTextNode($hTable[$entry])
  $null = $xmlElt.AppendChild($xmlText)
  $null = $xmlCpn.AppendChild($xmlElt)

  $xmlElt = $xmlDoc.CreateElement("KLOC")
  $xmlText = $xmlDoc.CreateTextNode("0")
  $null = $xmlElt.AppendChild($xmlText)
  $null = $xmlCpn.AppendChild($xmlElt)

  $xmlElt = $xmlDoc.CreateElement("IsCount")
  $xmlText = $xmlDoc.CreateTextNode("True")
  $null = $xmlElt.AppendChild($xmlText)
  $null = $xmlCpn.AppendChild($xmlElt)

  # Add the nod to the document
  $null = $xmlDoc.LastChild.AppendChild($xmlCpn);
}

# Backup to file
$xmlDoc.Save("c:\Temp\Component.xml")