0

I only need Powershell for this task and only have experience with coding in Stata - help appreciated.

I have a large number of xml files where each one is a single record from a survey, so it has variable names and responses for one person.

I need to convert those files to a csv file. From reading other posts I understand that I need to loop over the files, but I don't know how to tell Powershell that I want the survey data to be exported, rather than file attributes.

I've tried something like this:

$items = Get-ChildItem C:\Users\fs59\Desktop\temp\* -Include *.xml
foreach ($item in $items) {
    $xml = [XML](Get-Content $item) 
    $xml | Export-Csv -Path C:\Users\fs59\Desktop\temp\myoutput.csv -NoTypeInformation -Append 
} 

This runs without an error but the output does not contain the survey data.

UPDATE: Hi, I have managed to send the results to csv but the output is one long column. Is there a way of having the output in several rows, one per each xml file?

# Get all XML files
$items = Get-ChildItem C:\Users\user\Desktop\test\* -Include *.xml

# Loop over them and append them to the document
foreach ($item in $items) {
    $inputFile = [XML](Get-Content $item) #load xml document    

#export xml as csv
$inputFile.TT_client_survey_2018_v1.ChildNodes | Export-Csv -Path    C:\Users\user\Desktop\test\myoutput12.csv -NoTypeInformation -Append 
}
4
  • 2
    Possible duplicate of Powershell - convert XML to CSV Commented Oct 5, 2018 at 16:00
  • 1
    How many levels of XML Commented Oct 5, 2018 at 16:03
  • What does your xml look like? What is in the output? Commented Oct 5, 2018 at 16:15
  • Hi @ArcSet, 3 levels of xml. Commented Oct 6, 2018 at 12:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.