0

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!!!

2
  • 1
    What is the output that you do get? Commented Jul 11, 2013 at 11:25
  • @alroc please see the edit I have made Commented Jul 11, 2013 at 11:31

1 Answer 1

1

using

$xmlWriter.WriteEndElement()

instead of

$xmlWriter.WriteEndElement

should fix your problem

same problem in this post

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.