Поиск только по году
Вы можете прочитать в документе xml через и затем обновить необходимые элементы.
[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")
Поиск по StudentID и год
Если вы хотите указать конкретный c идентификатор студента, вы можете выполнить поиск по этому номеру,
$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")
, получив 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>
Для сравнения, это xml Я использовал.
<?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>