简体   繁体   中英

Write nested XML file with powershell in foreach loops

I am trying to write a nested XML file using powershell and XmlWriter. However the output is not what I am expecting, nor does it adhere to how I have logically set it out in the code. I know its slightly long but please look at the code below:

function newXML($output_file, $hubs_array){
        #create directory
    new-item -path "$outage_dir\$current_year\$current_month" -type directory -force
    # Create The Document and format
    $XmlWriter = New-Object System.XMl.XmlTextWriter($output_file,$Null)
    #format xml
    $xmlWriter.Formatting = [System.Xml.Formatting]::Indented
    # Write the XML Decleration
    $xmlWriter.WriteStartDocument()
    # Set the XSL
    $XSLPropText = "type='text/xsl' href='style.xsl'"
    $xmlWriter.WriteProcessingInstruction("xml-stylesheet", $XSLPropText)
    # <identifier><start of outage><end of outage><outage as perc>
    # Write Root Element    
    $xmlWriter.WriteStartElement("Hubs")
    # Write the Document
    foreach ($hub in $hubs_array){
            $xmlWriter.WriteStartElement("Hub")
            $xmlWriter.WriteAttributeString("name",$hub.hubname)
            foreach($device in $hub.devices){
                $xmlWriter.WriteStartElement("Device")
                $xmlWriter.WriteElementString("identifier", $device.device_id)
                $xmlWriter.WriteEndElement
                $xmlWriter.WriteElementString("sla", $device.SLA)
                $xmlWriter.WriteEndElement
                $xmlWriter.WriteElementString("outage_as_perc", $device.down_4dp)
                $xmlWriter.WriteEndElement
                $xmlWriter.WriteStartElement("Outages")
                $xmlWriter.WriteAttributeString("total",$device.outages)
                foreach($outage in $device.outagesArray){
                    $xmlWriter.WriteStartElement("Outage")
                    $xmlWriter.WriteElementString("start_of_outage", $outage.down_start)
                    $xmlWriter.WriteEndElement
                    $xmlWriter.WriteElementString("end_of_outage", $outage.down_end)
                    $xmlWriter.WriteEndElement
                    $xmlWriter.WriteElementString("total_downtime", $outage.downtime)
                    $xmlWriter.WriteEndElement
                    #close tag for outage
                    $xmlWriter.WriteEndElement
                }
                #close tag for outages
                $xmlWriter.WriteEndElement
                #close tag for device
                $xmlWriter.WriteEndElement

            }
            #close tag for hub
            $xmlWriter.WriteEndElement
    }
    #close tag for hubs
    $xmlWriter.WriteEndElement
    $xmlWriter.WriteEndDocument() 
    # Finish The Document
    $xmlWriter.Finalize
    $xmlWriter.Flush
    $xmlWriter.Close()
}

I assumed this would output in the following format:

<Hubs>
  <Hub>
    <Device>
      <identifier></>
      <sla></>
      <outage_as_perc></>
      <Outages total="2">
         <outage>
         </outage>
      </Outages>
    </Device>
   </Hub>
</Hubs>

The fact that I am looping through seems to be messing it up but I don't know why....

The result i do get is:

<Hubs>
  <Hub>
    <Device>
      <identifier></>
      <sla></>
      <outage_as_perc></>
      <Outages total="2">
         <outage>
           <outage>
              <outage>
                 <outage>
                    <outage>
                    </outage>
                 </outage>
              </outage>
           </outage>
         </outage>
      </Outages>
    </Device>
   </Hub>
 </Hubs>

Thanks in advance and sorry for the long read!!!

using

$xmlWriter.WriteEndElement()

instead of

$xmlWriter.WriteEndElement

should fix your problem

same problem in this post

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