Answer the question
In order to leave comments, you need to log in
How to close the root element in XML with PowerShell and export a bunch of XML files into a single CSV?
There is a bunch of logs for the last few years in the form of XML:
<root>
<item date="05-08-2015" time="05:45:32.12" data1="888.8">
<data2>LOW</data2>
</item>
...
<item date="05-08-2015" time="23:45:32.04" data2="1956.2">
<data3>OK</data3>
</item>
[xml]$XmlDocument = Get-Content -Path LogXML-05-08-2015.log
$XmlDocument.selectNodes('//root/item') | foreach{ New-Object -TypeName psobject -Property @{date=$_.date; time=$_.time; data1=$_.data1;} } |
Export-Csv data.csv -NoTypeInformation -Encoding UTF8
Unexpected end of file. The following elements are not closed: root., line 3312, position 8.
</root>
and execute my script? Answer the question
In order to leave comments, you need to log in
Good day.
1. If we know exactly how the xml file is damaged, then we can fix it. To do this, we first load the contents of the file as text. We check for an error, fix the content, and only then convert it to xml.
$WorkFolder = "c:\WorkFolder"
$LogFile = $WorkFolder + "\" + "sample_0001.log"
$FileContent = (Get-Content $LogFile).Trim()
if (-not ($FileContent -match "</root>$")){$FileContent += "</root>"}
[xml]$XmlFile = $FileContent
$WorkFolder = "c:\WorkFolder"
$ResultFile = "$WorkFolder\result.csv"
Get-ChildItem "$WorkFolder\*.log" | ForEach-Object {
$FileContent = (Get-Content $_).Trim()
if (-not ($FileContent -match "</root>$")){$FileContent += "</root>"}
[xml]$XmlFile = $FileContent
$XmlFile.root.item | Select-Object @(
@{l="date"; e={$_.date}}
@{l="time"; e={$_.time}}
@{l="data1"; e={$_.data1}}
) | Export-Csv -Path $ResultFile -NoTypeInformation -Append -Encoding UTF8
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question