0

I have a CSV file with the values as below:

Age , Status

29 ,    0
41 ,    1
44,     1
27,     0
60,     1

The XML is as below:

<office>
  <staff branch="Torrance" Type="Implementation">
    <employee>
        <Name>Raj Parpani</Name>
        <function>Implementation</function>
        <age>29</age>
    </employee>
    <employee>
        <Name>Kevin Woo</Name>
        <function>Consultant</function>
        <age>41</age>
    </employee>
  </staff>
  <staff branch="Irvine" Type="Operations">
   <employee>
    <Name>David Woo</Name>
    <function>Data</function>
    <age>42</age>
   </employee>
   </staff>
 </office>

If XML age is equal to the age in CSV, I have to append the status attribute for that age from the csv to the employee. I have tried the code as below:

ORIGINAL CODE

$csv = Import-Csv 'C:\Users\rparpani\Desktop\test2.csv' | Select-Object "Age","Status"
$xml = New-Object XML
$xml.Load("C:\Users\rparpani\Desktop\test.xml")

$nodes = $xml.SelectNodes("/office/staff/employee")

Foreach($row in $csv)
{
    foreach($node in $nodes)
    {

    if($node.age -eq  $row.age)
    {
    $node.SetAttribute("status", $row.Status);
    }


    }

}

Can someone please suggest how to change this to do what I want it to do


MODIFIED CODE

    $csv = Import-Csv 'C:\Users\rparpani\Desktop\test2.csv' | Select-Object "Age","Status"
    $xml = New-Object XML
    $xml.Load("C:\Users\rparpani\Desktop\test.xml")

    $nodes = $xml.SelectNodes("/office/staff/employee")


    foreach($row in $csv) {
      foreach($node in $nodes) {
        if ($node.age -eq $row.age) {
          if ($node.status) {
            $node.status.innerText = $row.Status
          }
          else {
            $status = $xml.CreateNode("element", "status", "")
            $status.InnerText = $row.status
            $node.AppendChild($status)
          }
        }
      }
    }

    $xml.Save("C:\Users\rparpani\Desktop\test.xml")
5
  • What if there are two employees with same age? You should be comparing with unique variable instead of age that can be same across multiple people Commented Feb 14, 2020 at 0:00
  • If age 49 has status 1 on CSV, and there are two employees on xml with that age, we would append status 1 to that employee Commented Feb 14, 2020 at 0:02
  • So all employees with age 41 will have status 1 Commented Feb 14, 2020 at 0:07
  • yes @Jawad, Everyone with Age 41 will have status 1 Commented Feb 14, 2020 at 0:09
  • Please do not change the entirety of your question / code. If you need to add something to original post, please add that separately Commented Feb 14, 2020 at 2:10

1 Answer 1

1

Following code does what you are looking for.

  1. You cannot use SetAttribute to create a new element with a value/InnerText. You need to create an element and append it to the node you are on. In your example xml, staff has two attributes, branch and Type that can be updated with SetAttribute method.
  2. If Status already exists, update it with new value.. otherwise create a new element.

CSV Content

Age,Status
29,0
41,1
44,1
27,0
60,1

Script to add attribute to Node `Employee

foreach($row in $csv) {
  foreach($node in $nodes) {
    if ($node.age -eq $row.age) {
      if ($row.status -eq "0") {
        $node.SetAttribute("status", "Hired")
      }
      else {
        $node.SetAttribute("status", "Fired")
      }
    }
  }
}

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

8 Comments

I tried your code but it says innertext cannot be found on object: $node.status.innerText = $row.Status
Cannot set "status" because only strings can be used as values to set XmlNode properties.
your algorithm creates a new node. I just want to add the attribute like "<employee Status="0"> where 0 should be not fired and 1 is fired so output will be "<employee Status="Not Fired">
@RajParpani Added the code up top to show you how to add an atribute using a variable. See if this helps
when it reads the status 1, it is suppose to set the status in xml as "hired". When it reads the status 0, it is suppose to set the status in xml to "Fired". Would i have to have another if?
|

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.