Reputation: 892
I have XML file like this.
?xml version="1.0" encoding="utf-8"?>
<StudentList>
<Class>
<StudentID code="200" description="EDF">
<SID8594>
<RegID>8594</RegID>
<Year>2019</Year>
<MAJOR>
<Level>2.13</Level>
</MAJOR>
<SUBMAJOR>
<Level>2.13</Level>
</SUBMAJOR>
</SID8594>
<SID8593>
<RegID>8593</RegID>
<Year>2019</Year>
<MAJOR>
<Level>2.13</Level>
</MAJOR>
<SUBMAJOR>
<Level>2.13</Level>
</SUBMAJOR>
</SID8593>
<SID8595>
<RegID>8595</RegID>
<Year>2020</Year>
<MAJOR>
<Level>2.20</Level>
</MAJOR>
<SUBMAJOR>
<Level>2.20</Level>
</SUBMAJOR>
</SID8595>
<StudentID code="100" description="ABC">
<SID85AA>
<RegID>85AA</RegID>
<Year>2019</Year>
<SUBMAJOR>
<Level>2.13</Level>
</SUBMAJOR>
</SID85AA>
</Class>
<StudentList>
The first case, I want to update innertext of all "Level", but only element StudentID code="100" . Is it possible to do it?
The second case, I want to update all innertext element "Level", if the element "Year" has innertext equal to "2019" . Is that possible?
Anyone can give me idea please. Thank you
Upvotes: 0
Views: 955
Reputation: 11364
Search By Year only
You can read in the xml document via and then update the required elements.
[xml]$studentList = Get-Content C:\temp\xml.txt
$Nodes2019 = $studentList.StudentList.Class.StudentID.ChildNodes | ? {$_.Year -eq 2019 }
# Change the values of all the elements
$Nodes2019 | ? {$_.MAJOR } | % { $_.MAJOR.LEVEL = "NewValue" }
# Save changes to new file once done.
$studentList.Save("C:\temp\newxml.txt")
Search By StudentID and year
If you want to specify a specific student ID, you can search via this,
$studentID = 200
$year = 2019
[xml]$studentList = Get-Content C:\temp\xml.txt
#Get all the 2019 nodes for specific Student.
$Nodes2019 = ($studentList.StudentList.Class.StudentID | ? {$_.Code -eq $studentID}).ChildNodes | ? {$_.Year -eq $year }
#Update the data you want ***WITH IF STATEMENT**
$Nodes2019 | % { if ($_.MAJOR.LEVEL) { $_.MAJOR.LEVEL = "newValueMajor"} ; if ($_.SUBMAJOR.LEVEL) { $_.SUBMAJOR.LEVEL = "newValueSubMajor" } }
#Save the data
$studentList.Save("C:\temp\newXml.txt")
resulting xml file
<?xml version="1.0" encoding="utf-8"?>
<StudentList>
<Class>
<StudentID code="200" description="EDF">
<SID8594>
<RegID>8594</RegID>
<Year>2019</Year>
<MAJOR>
<Level>newValueMajor</Level>
</MAJOR>
<SUBMAJOR>
<Level>newValueSubMajor</Level>
</SUBMAJOR>
</SID8594>
<SID8593>
<RegID>8593</RegID>
<Year>2019</Year>
<MAJOR>
<Level>newValueMajor</Level>
</MAJOR>
<SUBMAJOR>
<Level>newValueSubMajor</Level>
</SUBMAJOR>
</SID8593>
<SID8595>
<RegID>8595</RegID>
<Year>2020</Year>
<MAJOR>
<Level>2.20</Level>
</MAJOR>
<SUBMAJOR>
<Level>2.20</Level>
</SUBMAJOR>
</SID8595>
</StudentID>
<StudentID code="100" description="ABC">
<SID85AA>
<RegID>85AA</RegID>
<Year>2019</Year>
<SUBMAJOR>
<Level>2.13</Level>
</SUBMAJOR>
</SID85AA>
</StudentID>
</Class>
</StudentList>
For comparison purpose, this is the xml I used.
<?xml version="1.0" encoding="utf-8"?>
<StudentList>
<Class>
<StudentID code="200" description="EDF">
<SID8594>
<RegID>8594</RegID>
<Year>2019</Year>
<MAJOR>
<Level>2.13</Level>
</MAJOR>
<SUBMAJOR>
<Level>2.13</Level>
</SUBMAJOR>
</SID8594>
<SID8593>
<RegID>8593</RegID>
<Year>2019</Year>
<MAJOR>
<Level>2.13</Level>
</MAJOR>
<SUBMAJOR>
<Level>2.13</Level>
</SUBMAJOR>
</SID8593>
<SID8595>
<RegID>8595</RegID>
<Year>2020</Year>
<MAJOR>
<Level>2.20</Level>
</MAJOR>
<SUBMAJOR>
<Level>2.20</Level>
</SUBMAJOR>
</SID8595>
</StudentID>
<StudentID code="100" description="ABC">
<SID85AA>
<RegID>85AA</RegID>
<Year>2019</Year>
<SUBMAJOR>
<Level>2.13</Level>
</SUBMAJOR>
</SID85AA>
</StudentID>
</Class>
</StudentList>
Upvotes: 1