Appending another child element to xml using powershell -


i have following xml file . add thing it, have tried several different ways can’t seem right.

this xml files looks .

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

<time>  <timeofcheck>2014.3.18.22.36.43</timeofcheck>  <size>120.14</size>  <freespace>25</freespace> </time> 

so ends looking

<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 code loads xml file checks if there node called c: or d:

# set file name $filepath = "c:\dump\report.xml"  # load values going add 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 file exists   if (test-path $filepath) {#if file exists    #load file   [xml]$xmlexists = get-content $filepath   foreach($obj in $disk)  {$node = "//device/name[text() ='" + $obj.deviceid + "']"   $nodestest =$xmlexists.selectnodes("$node")     #check if node exists        if ($nodestest.get_count() -gt 0)         {       # node exists how add           }      else          {          # node not exists how crate           }  } } 

edit 1#

ok have managed work using code.

$appendpath = "//device[name/text() ='" + $obj.deviceid + "']"                 # build xml 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 number of methods on xmlelement type (one instance each element in document).

to add element $ne last child xmlelement called $xe:

$xe.insertafter($ne, $xe.lastchild) 

(if $xe has no children, $xe.lastchild none, means insertafter inserts @ start of list of children, fine when empty.)

to create new elements, can xmldocument.createelement. if of want create not determined dynamically, create string , parse, import document insert 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); 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -