1

I have got an excel file which happens to be an XML table, I have saved it as an xml file instead and whilst trying to use powershell to import it to a SQL database. It is coming up with blank values.

<?xml version="1.0"?>
 <RecordsExport xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <RecordsExport>
     <ResultSet>
       <SRN>1</SRN>
       <Name>Tom</Name>
       <Gender>Male</Gender>
       <ChangeDate xsi:nil="true" />
    </ResultSet>
    <ResultSet>
      <SRN>2</SRN>
      <Name>Jack</Name>
      <Gender>Male</Gender>
      <ChangeDate xsi:nil="true" />
    </ResultSet>
  </RecordsExport>
</RecordsExport>

Powershell code

$xmldata = Get-Content -Path 'C:\temp\test.xml'
 $import_data = foreach ($data in $xmldata.RecordsExport.ResultSet){
    
 [PSCustomObject]@{
     SRN = $data.SRN
     Name= $data.Name
     Gender = $data.Gender
     ChangeDate = $data.ChangeDate
 }
    
 }
3
  • 2
    Get-Content returns an array of strings, you need to convert it to an xml document somehow before you can treat it as such. $xmldata = ... -> [xml]$xmldata = ... should do the trick Commented Feb 23, 2021 at 16:22
  • @user3535468, don't forget to mark an answer for other user that have the same prob as you :) Commented Feb 25, 2021 at 8:13
  • 1
    cannot Import-clixml do this better? Commented Oct 9, 2022 at 17:31

2 Answers 2

3

Simply,

$xml_file = get-content "C:\TEMP\test.xml"
$xml_file = [xml]$xml_file

Then, example in your case :

$xml_file.RecordsExport.ResultSet

To print Result Set.

Sign up to request clarification or add additional context in comments.

Comments

0
[xml]$xml_file = get-content "C:\TEMP\test.xml"

Comments

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.