user1539205
user1539205

Reputation: 79

Not able to Get the Last node of an xml node properly

I am having 2 diffrent xml file with. Currently im trying to get the Last xml node. In the 1st ouput im getting because empty or Null value since i have a commented line. Please help how to handle in this case.

#Below line Run firstfile.xml
$xmlfile = 'C:\Programs\MyTasks\TCPROD-29658\Test_1309\D_SimulationTest\firstfile.xml'
#Below line to Run secondfile.xml
#$xmlfile = 'C:\Programs\MyTasks\TCPROD-29658\Test_1309\D_SimulationTest\secondfile.xml'

$xmllastsitenode = [xml]::new() 
$xmllastsitenode.Load($xmlFile)
$lastsite_id = $xmllastsitenode.fccconfig.fccdefaults.LastChild.id
write-host 'Print1'
write-host $lastsite_id
if (!$lastsite_id) { Write-Host "variable is null" }
if ($lastsite_id) { Write-Host "variable is NOT null" }
write-host 'Print2'

Below is the xml files im trying to run

1st XML file

<?xml version="1.0" encoding="UTF-8"?>
<fccconfig version="1.3.2">
   <fccdefaults>
      <property name="CacheLocation" value="C:/Users/Public/" overridable="true"/>
      <site id="-1940805554" overridable="true">
         <parentfsc address="http://abcdefgh:1234/" priority="0" />
      </site>
      <!--__ANT_MARK__-->   

   </fccdefaults>
   <!-- default parentfsc - this is a marker that will be overwritten by the installer -->
   <parentfsc address="xyzlmnopq:10010" priority="0" transport="lan"/>
</fccconfig>

Output:-

Print1

variable is null Print2

2nd XML file

<?xml version="1.0" encoding="UTF-8"?>
<fccconfig version="1.3.2">
   <fccdefaults>
      <property name="CacheLocation" value="C:/Users/Public/" overridable="true"/>
      <site id="-1940805554" overridable="true">
         <parentfsc address="http://abcdefgh:1234/" priority="0" />
      </site>

   </fccdefaults>
   <!-- default parentfsc - this is a marker that will be overwritten by the installer -->
   <parentfsc address="xyzlmnopq:10010" priority="0" transport="lan"/>
</fccconfig>

Output:-

Print1 -1940805554 variable is NOT null Print2

Latest Code which have tried meantime

#Below line Run firstfile.xml
$xmlfile = 'C:\Programs\MyTasks\TCPROD-29658\Test_1309\D_SimulationTest\firstfile.xml'
#Below line to Run secondfile.xml
#$xmlfile = 'C:\Programs\MyTasks\TCPROD-29658\Test_1309\D_SimulationTest\secondfile.xml'

$xmllastsitenode = [xml]::new() 
$xmllastsitenode.Load($xmlFile)
$lastsite_id = $xmllastsitenode.fccconfig.fccdefaults.LastChild.id
write-host 'Print1'
write-host $lastsite_id
if (!$lastsite_id) 
{
     Write-Host "variable is null" 
     # New logic to handle firt xml file cases
     #$somesitetag ---> Having some xml tag content here
     $newSiteNode = $xmlcontent.ImportNode($somesitetag, $true)
     $antmarkline = (Get-Content $xmlfile) | select-string -Pattern $antmark
     $xmlcontent.fccconfig.fccdefaults.InsertAfter($newSiteNode,$antmarkline)
}

if ($lastsite_id)
{
    Write-Host "variable is NOT null"
    #Insterting some new nodes
    #This Working as second file conetent works here
}
write-host 'Print2'

Upvotes: 0

Views: 457

Answers (1)

Ash
Ash

Reputation: 3246

You need to ignore the comments. Check the output of this for the first XML file.

$settings = [System.Xml.XmlReaderSettings]::new()
$settings.IgnoreComments = $true
$xmlreader = [System.Xml.XmlReader]::Create('./Desktop/Test.xml', $settings)
$xmldoc = [System.Xml.XmlDocument]::new()
$xmldoc.Load($xmlreader)
$xmldoc.fccconfig.fccdefaults.LastChild

Upvotes: 1

Related Questions