0

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

3
  • You'll need to add a child Year element to Level, not simply update the InnerText of Level. Commented Feb 13, 2020 at 2:40
  • There are multiple nodes that have Year 2019. Any particular logic you want to use to select a particular SID ? Commented Feb 13, 2020 at 3:28
  • I don't want to use SID, no matter the SID. The simply one is, if the <StudentID code="200"> the innertext of all level will updated. @Jawad Commented Feb 13, 2020 at 3:34

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

10 Comments

But I found a little problem, If the $studentID = 200_ABC , This value "200_ABC" can not use. Do you have any idea about it?
how do you plan to use 200_ABC to find a record? $query = "200_ABC"; $studentID = $query.Split('_')[0]; $description = $query.Split('_')[1] ??
No, Its not working. I just make sure again, search by StudentID is not work. I change the StudentID to "200", the level is not change.
You cant change the $studentID to anything other than a number without ;. when you use ;, you are running a new command and powershell is telling you that 200_ABC is not a valid cmdlet. You should enclose the value in double quotes. $query = "200_abc;200_def"
Added an update to my response (with $studentID). Use IF statement to see if the key exists, otherwise keep going.
|

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.