Remove node from XML file using Power Shell scripting
<class>
<class_id>3S</class_id>
<class_pk type="integer">3900</class_pk>
<course>Homeroom (G3)</course>
<course_id type="integer">1137</course_id>
<course_type>Homeroom</course_type>
<description>Homeroom G3 (Stoll)</description>
<group_fk type="integer">15159</group_fk>
<primary_grade_level>Grade 3</primary_grade_level>
<school_level>PYP</school_level>
<school_year type="integer">2015</school_year>
<status>Active</status>
<subject>Homeroom</subject>
<teacher_fk type="integer">2805</teacher_fk>
<teacher_full_name>Jessica Stoll</teacher_full_name>
<update_date type="datetime">2015-09-03T02:01:00-05:00</update_date>
<teachers type="array">
  <teacher>
    <person_fk type="integer">2740</person_fk>
    <role>Teacher's Aide</role>
    <role_id type="integer">3</role_id>
    <teacher_name>Tamara Bondarchuk</teacher_name>
    <update_grades type="boolean">true</update_grades>
    <view_grades type="boolean">true</view_grades>
  </teacher>
  <teacher>
    <person_fk type="integer">2805</person_fk>
    <role>Primary Teacher</role>
    <role_id type="integer">1</role_id>
    <teacher_name>Jessica Stoll</teacher_name>
    <update_grades type="boolean">true</update_grades>
    <view_grades type="boolean">true</view_grades>
  </teacher>
</teachers>
<meeting_times type="array">
  <meeting_time>
    <block>3rd period Primary</block>
    <block_abbreviation>3PP</block_abbreviation>
    <day>Monday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>4th period Primary</block>
    <block_abbreviation>4PP</block_abbreviation>
    <day>Monday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>5th period Primary</block>
    <block_abbreviation>5PP</block_abbreviation>
    <day>Monday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>8th period Primary</block>
    <block_abbreviation>8PP</block_abbreviation>
    <day>Monday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>9th period Primary</block>
    <block_abbreviation>9PP</block_abbreviation>
    <day>Monday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>Homeroom Primary E</block>
    <block_abbreviation>HMP</block_abbreviation>
    <day>Monday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>2nd period Primary</block>
    <block_abbreviation>2PP</block_abbreviation>
    <day>Tuesday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>3rd period Primary</block>
    <block_abbreviation>3PP</block_abbreviation>
    <day>Tuesday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>4th period Primary</block>
    <block_abbreviation>4PP</block_abbreviation>
    <day>Tuesday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>5th period Primary</block>
    <block_abbreviation>5PP</block_abbreviation>
    <day>Tuesday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>8th period Primary</block>
    <block_abbreviation>8PP</block_abbreviation>
    <day>Tuesday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>9th period Primary</block>
    <block_abbreviation>9PP</block_abbreviation>
    <day>Tuesday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>Homeroom Primary E</block>
    <block_abbreviation>HMP</block_abbreviation>
    <day>Tuesday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>2nd period Primary</block>
    <block_abbreviation>2PP</block_abbreviation>
    <day>Wednesday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>3rd period Primary</block>
    <block_abbreviation>3PP</block_abbreviation>
    <day>Wednesday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>4th period Primary</block>
    <block_abbreviation>4PP</block_abbreviation>
    <day>Wednesday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>5th period Primary</block>
    <block_abbreviation>5PP</block_abbreviation>
    <day>Wednesday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>Homeroom Primary E</block>
    <block_abbreviation>HMP</block_abbreviation>
    <day>Wednesday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>1st period Primary</block>
    <block_abbreviation>1PP</block_abbreviation>
    <day>Thursday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>3rd period Primary</block>
    <block_abbreviation>3PP</block_abbreviation>
    <day>Thursday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>4th period Primary</block>
    <block_abbreviation>4PP</block_abbreviation>
    <day>Thursday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>5th period Primary</block>
    <block_abbreviation>5PP</block_abbreviation>
    <day>Thursday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>8th period Primary</block>
    <block_abbreviation>8PP</block_abbreviation>
    <day>Thursday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>9th period Primary</block>
    <block_abbreviation>9PP</block_abbreviation>
    <day>Thursday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>Homeroom Primary E</block>
    <block_abbreviation>HMP</block_abbreviation>
    <day>Thursday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>1st period Primary</block>
    <block_abbreviation>1PP</block_abbreviation>
    <day>Friday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>2nd period Primary</block>
    <block_abbreviation>2PP</block_abbreviation>
    <day>Friday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>3rd period Primary</block>
    <block_abbreviation>3PP</block_abbreviation>
    <day>Friday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>4th period Primary</block>
    <block_abbreviation>4PP</block_abbreviation>
    <day>Friday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>5th period Primary</block>
    <block_abbreviation>5PP</block_abbreviation>
    <day>Friday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>8th period Primary</block>
    <block_abbreviation>8PP</block_abbreviation>
    <day>Friday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>Homeroom Primary E</block>
    <block_abbreviation>HMP</block_abbreviation>
    <day>Friday</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
  <meeting_time>
    <block>Homeroom Primary M</block>
    <block_abbreviation>HMP</block_abbreviation>
    <day>M-F</day>
    <end_time/>
    <grading_period>ALL</grading_period>
    <room>P212</room>
    <start_time/>
  </meeting_time>
</meeting_times>
  </class>

This is my xml file. I want to delete teachers nodes and meeting_time nodes. 

$xml = Get-Content c:\\mypsi\classes.xml
$node = $xml.classes.class.teachers

    $xml.classes.class.RemoveChild($node)



$xml.save("c:\\mypsi\classes.xml")

This code which it returns this error.

You cannot call a method on a null-valued expression.
At line:16 char:5
+     $xml.classes.class.RemoveChild($node)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Method invocation failed because [System.String] does not contain a method named 'save'.
At line:22 char:1
+ $xml.save("c:\\mypsi\classes.xml")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Please can you help me.

September 12th, 2015 6:22am

[xml]$xml = Get-Contentc:\\mypsi\classes.xml
$teachers=$xml.class.teachers
$xml.class.RemoveChild($teachers)

There is no "classes" in your xml.

Free Windows Admin Tool Kit Click here and download it now
September 12th, 2015 6:38am

By the way.  This is very poorly designed XML.  It uses elements where attributes should be used.
September 12th, 2015 6:42am

Here is a safer way to do this:

[xml]$xml=Get-Content classes.xml
$class3S=$xml.SelectSingleNode('//class[./class_id = "3S"]')
$class3S.RemoveChild($class3S.teachers)
$xml.Save("$pwd\newclasses.xml")
Free Windows Admin Tool Kit Click here and download it now
September 12th, 2015 6:54am

Hi,

Thanks for your answer.

I am sorry that my mistake. Here is my hole file simple map.

<classes>

<class>

<class id>657</class id>

<teachers>

<teacher>

<teacher id>3656</teacher id>

</teacher>

</teachers>

</class>

</classes>

September 12th, 2015 7:09am

Yes. But i receive this file from a Database API. So i must use what i have.
Free Windows Admin Tool Kit Click here and download it now
September 12th, 2015 7:10am

There is more then 1 class and every class has teachers node which i want to remove all
September 12th, 2015 7:11am

$teachers=$xml.SelectNodes('//teachers')

$teachers|%{$_.RemoveAll()}

or ..

$xml.SelectNodes('//teachers') | %{$_.RemoveAll()}
Free Windows Admin Tool Kit Click here and download it now
September 12th, 2015 8:37am

Method invocation failed because [System.String] does not contain a method named 'SelectNodes'.
At line:14 char:1
+ $xml.SelectNodes('//teachers') | %{$_.RemoveAll()}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

i receive this may be i must install some modules?

September 12th, 2015 8:50am

You have to load the XML.  You are just loading a string.
Free Windows Admin Tool Kit Click here and download it now
September 12th, 2015 8:51am

$xml = Get-Content c:\\mypsi\classes.xml
$xml.SelectNodes('//teachers') | %{$_.RemoveAll()}

this my script..

September 12th, 2015 8:53am

$xml = Get-Content c:\\mypsi\classes.xml
$xml.SelectNodes('//teachers') | %{$_.RemoveAll()}

this my script..

You are just loading a string.  You need to load it as XML.

[xml]

Free Windows Admin Tool Kit Click here and download it now
September 12th, 2015 8:54am

Thank You very much. You a good man.

And i am a beginner in Power shell scripting.

Best Regards, 

September 12th, 2015 2:53pm

Thank you.  XML is a big thing.  It takes a bit to learn as does PowerShell.  Now you know more about both than you think.

Free Windows Admin Tool Kit Click here and download it now
September 12th, 2015 3:08pm

one more question. I created a database and tables with columns like XML file attributes.

i want to import XML file directly into SQL but it is little bit tricky. I converted xml to csv and successfully imported data. But i need to take of some data from my project which could be perfect if i could keep them.

If i can import XML file directly i will not loose any data.  

I think you will ask why i loose data. Because Some data in XML file is comments or notes. and they can contain (,:;'"/\) like these characters. when i convert data to csv.. Bomb csv getting unnormal to import :)

thats why or i must find a way to import xml, or i will be using limited data.

 
September 12th, 2015 3:58pm

You can directly store XML in a SQL database.

Start a new question on this as it is a new topic.  You should post in the SQLServer forum for help with storing XML in a database.

Free Windows Admin Tool Kit Click here and download it now
September 12th, 2015 4:03pm

Also note that there is nothing in an XML file that cannot be converted to a CSV field.  Only quotes need to be escaped and they are escaped by another quote.
September 12th, 2015 4:05pm

thanks
Free Windows Admin Tool Kit Click here and download it now
September 12th, 2015 4:11pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics