Output xml field data from files to csv file

I am new to the forum but in need of some help. I have a script that is supposed to look at xml files in a folder and then copy them to a new folder based on reading the patient name, dob, person number and a timestamp. However, I’m having issues with the timestamp format but beyond that just having an output csv file with the info would be great.
What I get with the script below is the filename and the filedate but none of the data that should be getting pulled from inside the file.
However, after running the script it does actually print to the screen all the info needed.
Example of what prints:
Filename : \Test\EMR\CCD\0000001.xml
FirstName : ZZtest
LastName : Adele
PersonNbr : 12345
BirthDate : 9/13/1984 12:00:00 AM

Script used:

$SourceFolder =  "\\ngimage\Nextgenroot\Test\EMR\CCD"
$ReportLocation = "C:\TestXMLOutput\$FileCompatibleDateStr`_FileReport.csv"


## Parse all XML files in SourceFolder
$Patients = Get-ChildItem -File -Path $SourceFolder -Filter *.xml | ForEach-Object {
    $XML = [Xml](Get-Content -Path $_.FullName)
     [PSCustomObject]@{
         Filename        =   $_.FullName
         FirstName       =   $XML.ClinicalDocument.RecordTarget.PatientRole.Patient.Name.Family
         LastName        =   $XML.ClinicalDocument.RecordTarget.PatientRole.Patient.Name.Given.InnerText
         PersonNbr       =   $XML.ClinicalDocument.RecordTarget.PatientRole.Id.Extension
         BirthDate       =   [DateTime]::ParseExact($XML.ClinicalDocument.RecordTarget.PatientRole.Patient.BirthTime.Value, 'yyyyMMdd', $null)
       ##EffectiveDate   =   [DateTime]::ParseExact($XML.ClinicalDocument.ComponentOf.EncompassingEncounter.EffectiveTime.Value,'yyyyMMddHmm', $null)
    }
 }

## Outputs all unique patients with their most recent visit date. I also added a copy item example if you need to copy somewhere else. Update the path and remove the -WhatIf to actually copy files.
$Patients | Sort-Object EffectiveDate -Descending | Group-Object BirthDate, LastName, FirstName | ForEach-Object {
    $_.Group[0]
    Copy-Item -Path "$($_.Group[0].Filename)" -Destination "C:\TestXMLOutput" 
    }

    
#Exports the array created earlier into a CSV file, sorting the entries first by last name, then first name, then DOB, and then by file date
$FileArray |Sort-Object LastName,FirstName,PersonNbr,Birthdate,Filename |Export-Csv -Path $ReportLocation

Mike,
Welcome to the forum. :wave:t4:

Technically you’re actually not asking any question. :wink:

It would have been nice to have some (sanitized) sample data to play with. :wink:

No. The code you posted copies actually only the first file of each group of your grouped data. But all of them to the same destination folder "C:\TestXMLOutput".

What issue do you have with it?

I’m unsure if I understand. The output you shared is exactly the output I’d expect when I look at your code snippet.
BTW: You should format console output as code as well. The same counts for error messages or sample data when you post something like this.

So you may output the content of the variable $Patients instead of the variable $FileArray at the end of your code. :wink: Where do actually define these variable?

Thank you and I apologize for the newbie mistakes.

I should not have even posted the first part. Right now, I’m only trying to get the output that prints to the screen to export to a csv file. It does output a file but does not contain any of the most important data from the file.

Example of what does output to a file below, which is missing name, dob and person_nbr:

Filename	                              FirstName	      LastName	   DOB	  PersonNbr	FileDate
\\Test\EMR\CCD\0000001.xml					                                       5/17/2022 14:33

Please carefully re-read the last paragraph of my first reply again! :wink: If I got you right you’re using the wrong variable.

It’s nearly impossible for us to figure out what’s wrong without actually having your data. :man_shrugging:t4: If the output of other files is as expected I’d say the issue is with the particular files you miss the expected data. Did you check if the files actually contain the data you’re trying to extract?

You were so correct. Thank you very much for catching that. The second part of this needed to actually get the script to only copy the most recent file based on a date is being stopped because of some type of date formatting issue.

Sample file is below. Sample error from trying to get the EffectiveDate to be included (currently commented out in the original code I included) is below that.

<?xml version="1.0"?><?xml-stylesheet type='text/xsl'>
<ClinicalDocument xmlns:voc="urn:hl7-org:v3/voc" xmlns:sdtc="urn:hl7-org:sdtc" xmlns="urn:hl7-org:v3" xmlns:mif="urn:hl7-org:v3/mif" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<realmCode code="US"/>
	<typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>
	<templateId root="2.16.840.1.113883.10.20.22.1.1"/>
	<templateId root="2.16.840.1.113883.10.20.22.1.1" extension="2015-08-01"/>
	<templateId root="2.16.840.1.113883.10.20.22.1.2"/>
	<templateId root="2.16.840.1.113883.10.20.22.1.2" extension="2015-08-01"/>
	<id root="2.16.840.1.113883.3.109.3.4414.3.1.1.80210.2.2.1" extension="4745a0dd-339a-4a5c-b3bd-f71e5c3fc350"/>
		<title>CCD (C-CDA R2.1) (Date Range: 2022-07-17 00:00:00 to 2022-07-19 00:00:00)</title>
	<effectiveTime value="20220718173505-0700"/>
	<confidentialityCode code="N" displayName="Normal Sharing" codeSystem="2.16.840.1.113883.5.25" codeSystemName="HL7 Confidentiality"/>
	<languageCode code="en-US"/>
	<recordTarget>
		<patientRole>
			<id extension="27197" root="2.16.840.1.113883.3.109.3.4414.3.1.1.80210.2.1"/>
			<addr>
				<streetAddressLine>113 Hello St</streetAddressLine>
				<city>SomeCity</city>
				<state>CA</state>
				<postalCode>91234</postalCode>
				<country>US</country>
			</addr>
			<telecom value="mailto:test@gmail.com"/>
			<patient>
				<name use="L">
					<family>ZZTest</family>
					<given qualifier="CL">Adele</given>
				</name>
				<administrativeGenderCode code="F" displayName="Female" codeSystem="2.16.840.1.113883.5.1" codeSystemName="HL7 AdministrativeGender"/>
				<birthTime value="19840913"/>
				<raceCode code="2028-9" displayName="Asian" codeSystem="2.16.840.1.113883.6.238" codeSystemName="CDC Race and Ethnicitys"/>
				<ethnicGroupCode code="2186-5" displayName="Not Hispanic or Latino" codeSystem="2.16.840.1.113883.6.238" codeSystemName="CDC Race and Ethnicitys"/>
				<languageCommunication>
					<templateId root="2.16.840.1.113883.3.88.11.32.2"/>
					<languageCode code="en"/>
					<preferenceInd value="true"/>
				</languageCommunication>
			</patient>
			<providerOrganization>
				<id root="2.16.840.1.113883.3.109.3.4414.3.1.1.80210.2.2.1" extension="0001"/>
				<id root="2.16.840.1.113883.4.6" extension="5551234567"/>
				<name>SomeCity Comm Health Center</name>
				<telecom use="WP" value="tel:+1-5551234567"/>
				<addr>
					<streetAddressLine>12345 SomeCity Somestreet</streetAddressLine>
					<city>SomeCity</city>
					<state>CA</state>
					<postalCode>91234-5414</postalCode>
					<country>US</country>
				</addr>
			</providerOrganization>
		</patientRole>
	</recordTarget>
	<author>
		<time value="20220718173505-0700"/>
		<assignedAuthor>
			<id nullFlavor="UNK"/>
			<code code="163W00000X" displayName="Nursing Service Providers : Registered Nurse" codeSystem="2.16.840.1.113883.6.101" codeSystemName="NUCC"/>
			<addr nullFlavor="UNK"/>
			<telecom nullFlavor="UNK"/>
			<assignedAuthoringDevice>
				<manufacturerModelName>SomeVendorNameEHR</manufacturerModelName>
				<softwareName> v5.9.0.124.641731</softwareName>
			</assignedAuthoringDevice>
			<representedOrganization>
				<id root="2.16.840.1.113883.3.109.3.4414.3.1.1.80210.2.2.1" extension="0001"/>
				<id root="2.16.840.1.113883.4.6" extension="15551234567"/>
				<name>SomeCity Comm Health</name>
				<telecom use="WP" value="tel:+1-5551234567"/>
				<addr>
					<streetAddressLine>12345 SomeCity Somestreet</streetAddressLine>
					<city>SomeCity</city>
					<state>CA</state>
					<postalCode>91234-5414</postalCode>
					<country>US</country>
				</addr>
			</representedOrganization>
		</assignedAuthor>
	</author>
	<informant>
		<assignedEntity>
			<id nullFlavor="UNK"/>
			<code code="163W00000X" displayName="Nursing Service Providers : Registered Nurse" codeSystem="2.16.840.1.113883.6.101" codeSystemName="NUCC"/>
			<addr nullFlavor="UNK"/>
			<telecom nullFlavor="UNK"/>
			<assignedPerson>
				<name>
					<family>LastName</family>
					<given>FirstName</given>
				</name>
			</assignedPerson>
			<representedOrganization>
				<id root="2.16.840.1.113883.3.109.3.4414.3.1.1.80210.2.2.1" extension="0001"/>
				<id root="2.16.840.1.113883.4.6" extension="5551234567"/>
				<name>SomeCity Comm Health</name>
				<telecom use="WP" value="tel:+1-5551234567"/>
				<addr>
					<streetAddressLine>12345 SomeCity Somestreet</streetAddressLine>
					<city>SomeCity</city>
					<state>CA</state>
					<postalCode>91234-5414</postalCode>
					<country>US</country>
				</addr>
			</representedOrganization>
		</assignedEntity>
	</informant>
	<custodian>
		<assignedCustodian>
			<representedCustodianOrganization>
				<id root="2.16.840.1.113883.3.109.3.4414.3.1.1.80210.2.2.1" extension="0001"/>
				<id root="2.16.840.1.113883.4.6" extension="5551234567"/>
				<name>SomeCity Comm Health</name>
				<telecom use="WP" value="tel:+1-5551234567"/>
				<addr>
					<streetAddressLine>12345 SomeCity Somestreet</streetAddressLine>
					<city>SomeCity</city>
					<state>CA</state>
					<postalCode>91234-5414</postalCode>
					<country>US</country>
				</addr>
			</representedCustodianOrganization>
		</assignedCustodian>
	</custodian>
	<legalAuthenticator>
		<time value="20220718173505-0700"/>
		<signatureCode code="S"/>
		<assignedEntity>
			<id nullFlavor="UNK"/>
			<code code="163W00000X" displayName="Nursing Service Providers : Registered Nurse" codeSystem="2.16.840.1.113883.6.101" codeSystemName="NUCC"/>
			<addr nullFlavor="UNK"/>
			<telecom use="WP" nullFlavor="UNK"/>
			<assignedPerson>
				<name>
					<given>FirstName</given>
					<family>LastName</family>
				</name>
			</assignedPerson>
			<representedOrganization>
				<id root="2.16.840.1.113883.3.109.3.4414.3.1.1.80210.2.2.1" extension="0001"/>
				<id root="2.16.840.1.113883.4.6" extension="5551234567"/>
				<name>SomeCity Comm Health</name>
				<telecom use="WP" value="tel:+1-5551234567"/>
				<addr>
					<streetAddressLine>12345 SomeCity Somestreet</streetAddressLine>
					<city>SomeCity</city>
					<state>CA</state>
					<postalCode>91234-5414</postalCode>
					<country>US</country>
				</addr>
			</representedOrganization>
		</assignedEntity>
	</legalAuthenticator>
	<participant typeCode="IND">
		<associatedEntity classCode="PRS">
			<code code="COUSN" displayName="COUSN" codeSystem="2.16.840.1.113883.5.111" codeSystemName="HL7RoleCode"/>
			<addr nullFlavor="UNK"/>
			<telecom nullFlavor="UNK"/>
			<associatedPerson>
				<name>
					<family>d</family>
					<given>devin</given>
				</name>
			</associatedPerson>
		</associatedEntity>
	</participant>
	<participant typeCode="IND">
		<associatedEntity classCode="PRS">
			<code code="COUSN" displayName="COUSN" codeSystem="2.16.840.1.113883.5.111" codeSystemName="HL7RoleCode"/>
			<addr nullFlavor="UNK"/>
			<telecom nullFlavor="UNK"/>
			<associatedPerson>
				<name>
					<family>d</family>
					<given>roger</given>
				</name>
			</associatedPerson>
		</associatedEntity>
	</participant>
	<participant typeCode="IND">
		<associatedEntity classCode="PRS">
			<code code="COUSN" displayName="COUSN" codeSystem="2.16.840.1.113883.5.111" codeSystemName="HL7RoleCode"/>
			<addr nullFlavor="UNK"/>
			<telecom nullFlavor="UNK"/>
			<associatedPerson>
				<name>
					<family>d</family>
					<given>shelly</given>
				</name>
			</associatedPerson>
		</associatedEntity>
	</participant>
	<participant typeCode="IND">
		<functionCode code="PCP" codeSystem="2.16.840.1.113883.5.88"/>
		<associatedEntity classCode="CAREGIVER">
			<associatedPerson>
				<name>No Information</name>
			</associatedPerson>
		</associatedEntity>
	</participant>
	<documentationOf typeCode="DOC">
		<serviceEvent classCode="PCPR">
			<effectiveTime>
				<low value="202207181527"/>
				<high value="202207181527"/>
			</effectiveTime>
			<performer typeCode="PRF">
				<time>
					<low value="202207181735"/>
					<high value="202207181735"/>
				</time>
				<assignedEntity>
					<id root="2.16.840.1.113883.4.6" extension="1942839634"/>
					<code code="363LA2100X" displayName="Physician Assistants &amp; Advanced Practice Nursing Providers : Nurse Practitioner : Acute Care" codeSystem="2.16.840.1.113883.6.101" codeSystemName="NUCC"/>
					<addr>
						<streetAddressLine>91234 SomeCity Highway</streetAddressLine>
						<city>SomeCity</city>
						<state>CA</state>
						<postalCode>91234</postalCode>
						<country>US</country>
					</addr>
					<telecom nullFlavor="UNK"/>
					<assignedPerson>
						<name>
							<suffix>ACNP</suffix>
							<given>SomeFName</given>
							<family>SomeLName</family>
						</name>
					</assignedPerson>
				</assignedEntity>
			</performer>
		</serviceEvent>
	</documentationOf>
	<componentOf>
		<encompassingEncounter>
			<id extension="53498bd7-a6bc-4c36-bab4-f3c527d28926" root="2.16.840.1.113883.3.109.3.4414.3.1.1.80210.2.2.1"/>
			<effectiveTime value="202207181527"/>
			<encounterParticipant typeCode="ATND">
				<templateId root="2.16.840.1.113883.10.20.6.2.2"/>
				<time value="202207181527"/>
				<assignedEntity>
					<id root="2.16.840.1.113883.4.6" extension="1942839634"/>
					<code code="363LA2100X" displayName="Physician Assistants &amp; Advanced Practice Nursing Providers : Nurse Practitioner : Acute Care" codeSystem="2.16.840.1.113883.6.101" codeSystemName="NUCC"/>
					<addr>
						<streetAddressLine>91234 SomeCity Highway</streetAddressLine>
						<city>SomeCity</city>
						<state>CA</state>
						<postalCode>91234</postalCode>
						<country>US</country>
					</addr>
					<telecom use="WP" value="tel:+1-5551234567"/>
					<assignedPerson>
						<name>
							<family>SomeLName</family>
							<given>SomeFName</given>
						</name>
					</assignedPerson>
				</assignedEntity>
			</encounterParticipant>
		</encompassingEncounter>
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:6 char:5
+   [PSCustomObject]@{
+   ~~~~~~~~~~~~~~~~~~
    + CategoryInfo         : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatExceptiontype or paste code here

Although I’m far from being an XML expert, I can see that the snippet you posted is incomplete/inconsistent. So I had to strip it down a little to be able to play with it.

[XML]$XML = @'
<?xml version="1.0"?>
<ClinicalDocument>
	<recordTarget>
		<patientRole>
            <id extension="27197" root="2.16.840.1.113883.3.109.3.4414.3.1.1.80210.2.1"/>
			<patient>
				<name use="L">
					<family>ZZTest</family>
					<given qualifier="CL">Adele</given>
				</name>
				<birthTime value="19840913"/>
			</patient>
		</patientRole>
	</recordTarget>
	<componentOf>
		<encompassingEncounter>
			<effectiveTime value="202207181527"/>
		</encompassingEncounter>
   	</componentOf>
</ClinicalDocument>
'@

[PSCustomObject]@{
    FirstName     = $XML.ClinicalDocument.RecordTarget.PatientRole.Patient.Name.Family
    LastName      = $XML.ClinicalDocument.RecordTarget.PatientRole.Patient.Name.Given.InnerText
    PersonNbr     = $XML.ClinicalDocument.RecordTarget.PatientRole.Id.Extension
    BirthDate     = [DateTime]::ParseExact($XML.ClinicalDocument.RecordTarget.PatientRole.Patient.BirthTime.Value, 'yyyyMMdd', $null)
    EffectiveDate = [DateTime]::ParseExact($XML.ClinicalDocument.ComponentOf.EncompassingEncounter.EffectiveTime.Value, 'yyyyMMddHmm', $null)
}

But actually this snippet seems to work as expected and outputs this:

FirstName     : ZZTest
LastName      : Adele
PersonNbr     : 27197
BirthDate     : Do, 13.09.1984 00:00:00
EffectiveDate : Mo, 18.07.2022 15:27:00

I’d suspect that some of your XML files might be corrupt or incomplete or inconsistent. Or are there other supporting files missing?

Thanks so much. You were right. The script worked on most of the files but threw errors on many of them. After a lot of sifting through the xml files I finally found that not all files had the date field expected. This is contrary to what the vendor alluded to as the date to use so I’ve reached out to them. Again thank you very much for your help. :slight_smile: