简体   繁体   中英

Appending another child element to xml using powershell

I have the following xml file . I would like to add some thing to it, I have tried several different ways but just can't seem to get it right.

This is what the xml files looks like .

<Root>
 <Device>
  <Name>c:</Name>
   <Time>
    <TimeOfCheck>2014.3.18.22.36.43</TimeOfCheck>
    <Size>120.14</Size>
    <FreeSpace>38.18</FreeSpace>
  </Time>
 </Device>
 <Device>
  <Name>x:</Name>
   <Time>
    <TimeOfCheck>2014.3.18.22.36.43</TimeOfCheck>
    <Size>23.23</Size>
    <FreeSpace>11.47</FreeSpace>
   </Time>
 </Device>
</Root>

I am trying to add this

<Time>
 <TimeOfCheck>2014.3.18.22.36.43</TimeOfCheck>
 <Size>120.14</Size>
 <FreeSpace>25</FreeSpace>
</Time>

So it ends up looking like

<Root>
 <Device>
  <Name>c:</Name>
   <Time>
    <TimeOfCheck>2014.3.18.22.36.43</TimeOfCheck>
    <Size>120.14</Size>
    <FreeSpace>38.18</FreeSpace>
   </Time>
   <Time>
    <TimeOfCheck>2014.3.18.22.36.43</TimeOfCheck>
    <Size>120.14</Size>
    <FreeSpace>25</FreeSpace>
   </Time>
 </Device>
 <Device>
  <Name>x:</Name>
   <Time>
    <TimeOfCheck>2014.3.18.22.36.43</TimeOfCheck>
    <Size>23.23</Size>
    <FreeSpace>11.47</FreeSpace>
   </Time>
  </Device>
</Root>

This is my code that loads the xml file checks if there is a node called C: or D:

# Set the File Name
$filePath = "C:\dump\Report.xml"

# load the values that we going to add to the file  
$disk = Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" | Select-Object DeviceID,@{Name="Size";Expression={"{0:N2}" -f($_.Size/1GB)}},@{Name="FreeSpace";Expression={ "{0:N2}" -f ($_.FreeSpace/1GB) }},@{Name="Time";Expression={Get-Date -format yyyy.M.d.H.mm.ss}}

# check if the file exists 

If (Test-Path $filePath)
{#If the file exists 
  #load the file 
 [xml]$XmlExists = Get-Content $filePath

 foreach($obj in $disk)
 {$Node = "//Device/Name[text() ='" + $obj.DeviceID + "']"
  $NodesTest =$XmlExists.SelectNodes("$node")
    #check if the node exists 
      IF ($NodesTest.get_Count() -gt 0)
        {
      # The node exists but how do we add to it 

        }
     Else 
        {
         # The node does not exists how do we crate it 

        }
 }
}

Edit 1#

Ok have managed to get it to work using this code.

$AppendPath = "//Device[Name/text() ='" + $obj.DeviceID + "']"
                # Build the xml to Append

                $Time = $XmlExists.CreateElement('Time')
                $TimeOfCheck = $XmlExists.CreateElement('TimeOfCheck')
                               $TimeOfCheck.set_InnerText($obj.time)
                $Size = $XmlExists.CreateElement('Size')
                        $Size.set_InnerText($obj.Size)
                $FreeSpace = $XmlExists.CreateElement('FreeSpace')
                             $FreeSpace.set_InnerText($obj.FreeSpace)
                #Append opjects

                $Time.AppendChild($TimeOfCheck)
                $Time.AppendChild($Size)
                $Time.AppendChild($FreeSpace)

                $NodeToAppendTo= $XmlExists.SelectSingleNode($AppendPath)
                $NodeToAppendTo.AppendChild($Time)
                $XmlExists.Save($filePath)

To insert nodes there are a number of methods on the XmlElement type (one instance for each element in the document).

To add an element $ne as the last child to an XmlElement called $xe :

$xe.InsertAfter($ne, $xe.LastChild)

(If $xe has no children, then $xe.LastChild will be none, which means InsertAfter inserts at the start of the list of children, which is fine when it is empty.)

To create new elements, you can XmlDocument.CreateElement . However if most of what you want to create is not determined dynamically, create a string and parse, then import from that document to insert into another:

$tempDoc = [xml]"<element><inner>data</inner></element>";
$newEl = $tempDoc.DocumentElement;

$newEl = $destDoc.ImportNode($newEl, $true); # $true => deep
$destDoc.DocumentElement.InsertAfter($newEl, $destDoc.DocumentElement.LastChild);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM