Help with converting to xml

Welcome Forums General PowerShell Q&A Help with converting to xml

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
1 year, 7 months ago.

  • Author
    Posts
  • #77334

    Participant
    Points: 0
    Rank: Member

    Hi,

    I have a text file that looks in this form:

    [\0]
    Parameter1=Value1
    Parameter2=Value2

    [\1]
    Parameter3=Value3
    Parameter4=Value4
    Parameter5=Value5
    Parameter6=Value6
    [\1\Data]
    Parameter7=Value7
    Parameter8=Value8
    Parameter9=Value9
    Parameter10=Value10
    [\1\Data\MoreData]
    Parameter11=Value11
    Parameter12=Value12
    Parameter13=Value13
    Parameter14=Value14
    [\2]
    Parameter15=Value15
    Parameter16=Value16
    [\2\Data]
    Parameter17=Value17
    Parameter18=Value18
    Parameter19=Value19

    etc...

    As you Notice, this is actually a text representation of a logical tree –
    My question is: can i some how convert it to an xml?

    Thanks

  • #77335

    Participant
    Points: 1
    Rank: Member

    It very mutch looks like an old ini file, so see :
    https://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91

    Basicly it loads th ini file in a hash table:

    $Ini=@{}
    switch -regex -file .\textfile.txt
    {
        "^\[(.+)\]$" {
            $section = $matches[1]
            $ini[$section] = @{}
        }
        "(.+)=(.+)" {
            $name,$value = $matches[1..2]
            $ini[$section][$name] = $value
        }
    }
    
    

    Then export it to an xml.

    $ini|Export-Clixml test.xml
    
  • #77338

    Participant
    Points: 64
    Rank: Member

    you could try something like this :-

    $intext = get-content C:\test\basetext.txt
    $converted = $intext | ConvertTo-XML
    $converted.save("C:\test\converted.xml")

    or use Export-CliXml

    But it may not be true XML

    Converting PowerShell to XML

The topic ‘Help with converting to xml’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort