3

我有一个 powershell 脚本,我正在尝试为导入构建一个冗长的 XML 文件。我遇到的问题是,运行时,XML 的输出包含所有打开的标签,但直到最后都不会关闭它们。即使我有“WriteEndElement”功能可以按我想要的顺序关闭标签,但在所有“WriteStartElement”标签完成之前它不会兑现这些标签。即,输出看起来像

<1>
  <2>
    <3>
      <4>
      </4>
    </3>
  </2>
</1>

但在我的代码中,3 的收盘应该在 4 的开盘之前。
我的脚本:

$groups = gc "./groups.txt"
$regions = gc "./regions.txt"
$types = gc "./Types.txt"

$output = "./Workgroups.txt"
$xmlOut = "c:\wgIMPORT.xml"
$wgExt = 60001


# Create The Document
$XmlWriter = New-Object System.XMl.XmlTextWriter($xmlOut,$Null)
# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
# Set the XSL
$XSLPropText = "type='text/xsl' href='style.xsl'"
$xmlWriter.WriteProcessingInstruction("xml-stylesheet", $XSLPropText)
# Write Root Element
$xmlWriter.WriteStartElement("Scenarios")
# Write the Document
$xmlWriter.WriteStartElement("Scenario")
$xmlWriter.WriteElementString("Schedule","Immediate")



for($i=0;$i -lt $groups.Count;$i++){
    for($j=0;$j -lt $regions.Count;$j++){
        for($k=0;$k -lt $types.Count;$k++){ 
            #$groups[$i] + "_" + $regions[$j] + "_" + $types[$k] >> $output
            $wg = $groups[$i] + "_" + $regions[$j] + "_" + $types[$k]
            $xmlWriter.WriteStartElement("WorkgroupCommand")
            $XmlWriter.WriteAttributeString("Name","$wg")
            $XmlWriter.WriteAttributeString("Extension","$wgExt")
            $XmlWriter.WriteAttributeString("HasQueue","1")
            $xmlWriter.WriteStartElement("Attributes")
            $xmlWriter.WriteStartElement("Attribute")
            $XmlWriter.WriteAttributeString("Value","206")
            $xmlWriter.WriteEndElement # end Attribute
            $xmlWriter.WriteElementString("Value","TestSkill")
                $xmlWriter.WriteEndElement # end Attributes
            $xmlWriter.WriteStartElement("Members")
            $xmlWriter.WriteElementString("Member","tonkaadmin")
            $xmlWriter.WriteEndElement # end  </Members> Command
            $xmlWriter.WriteEndElement # end Workgroup Command
            $wgExt++
        }
    }
}    

$xmlWriter.WriteEndElement # end Scenario
# Write Close Tag for Root Element
$xmlWriter.WriteEndElement # end Scenarios
# End the XML Document
$xmlWriter.WriteEndDocument() 
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()
4

1 回答 1

3

使用

$xmlWriter.WriteEndElement()

代替

$xmlWriter.WriteEndElement

应该解决你的问题

于 2013-07-19T06:57:55.797 回答