Powershell XML-Writer

Startseite – Aktivität Foren Allgemeines Powershell XML-Writer

This topic contains 0 replies, has 1 voice, and was last updated by  Martin De Roma 1 month, 2 weeks ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #1951

    Martin De Roma
    Keymaster

    Hallo *,

    weil ich früher sehr oft dran rum gefummelt habe und nun endlich ein Stabil laufendes Snippen habe: Hier mein lieblings XML-Writer den ich immer dann nutze wenn ich Scriptbasiert ein XML ” in ordentlich” schreiben will:

    $path = "C:\temp\xmlwriter.xml"
    # Set the File Encoding
    $encoding = [System.Text.Encoding]::UTF8
    # Create The Document
    $XmlWriter = New-Object System.XMl.XmlTextWriter($path, $encoding)	
    # Set The Formatting
    $xmlWriter.Formatting = "Indented"
    $xmlWriter.Indentation = "4"		
    # Write the XML Decleration
    $xmlWriter.WriteStartDocument()
    # Write Root Element
    $xmlWriter.WriteStartElement("Element1")
    $xmlWriter.WriteStartElement("Element2")
    #write Element Loops can Start Here!
    $xmlWriter.WriteStartElement("Element3") # <-- New Element
    $xmlWriter.WriteElementString("ElementString1", "[String1]")
    $xmlWriter.WriteElementString("ElementSting2", "[String2]")
    $xmlWriter.WriteRaw("<RAWELEMENT/>")
    
    $xmlWriter.WriteEndElement() # <-- Closing request
    $xmlWriter.WriteEndElement() # <-- Closing requests
    $xmlWriter.WriteEndElement() # <-- Closing webmonconfig
    # End the XML Document
    $xmlWriter.WriteEndDocument()
    #cleanup
    $xmlWriter.Finalize
    $xmlWriter.Flush
    $xmlWriter.Close()

    Ergebnis:

    <?xml version="1.0" encoding="utf-8"?>
    <Element1>
        <Element2>
            <Element3>
                <ElementString1>[String1]</ElementString1>
                <ElementSting2>[String2]</ElementSting2>
                <RAWELEMENT/>
            </Element3>
        </Element2>
    </Element1>

    falls euch das weiterhilft schreibt mir doch bitte einen Kommentar, Fragen beantworte ich natürlich auch gerne 😉

    Gruß
    Martin

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.