Author Posts

August 16, 2017 at 6:42 am

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

August 16, 2017 at 7:32 am

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

August 16, 2017 at 7:35 am

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