2012-01-07 49 views
1

我有一個哈希表,其中組件名稱爲Key和Baseline作爲值。如何從powershell中的哈希表值追加xml

下面的線條勾勒出我的哈希表

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內容必須被複制則每次更改名稱和單獨基線元素

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

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

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

回答

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") 
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") 
+0

「鑄造」 的字符串爲'[XML]'是完全與創建XmlDocument並加載它相同。我說「鑄造」是因爲'[xml]'不是一種類型,而是一種類型_accelerator_。 – x0n 2012-01-08 01:20:35

+0

我完全同意你我只是沒有使用它的想法。 – JPBlanc 2012-01-08 07:56:01

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

這個答案是創建XML文件中的所有元素。但在我的情況下,我需要將名稱和基線元素添加到現有的XML中。 – Samselvaprabu 2012-01-07 12:38:48