0

System.Xml.XmlDocument.SelectNodes is not working for Windows Task Scheduler XML. Below is an excerpt of the XML:

<?xml version="1.0" encoding="UTF-8"?>
<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2014-12-03T13:58:05.5136628</Date>
    <Author>ABCCORP\jsmith</Author>
  </RegistrationInfo>
</Task>

The following code does not find the Task element:

[xml]$xml = gc C:\temp\myxml.xml
$xml.SelectNodes("/Task") #returns nothing

But if I remove the namespace from the XML, then it works:

<?xml version="1.0" encoding="UTF-8"?>
<Task version="1.3">
  <RegistrationInfo>
    <Date>2014-12-03T13:58:05.5136628</Date>
    <Author>ABCCORP\jsmith</Author>
  </RegistrationInfo>
</Task>

How can I make this work?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Matthew S
  • 900
  • 3
  • 12
  • 26

1 Answers1

0

A namespace manager is required:

[xml]$xml = gc C:\temp\myxml.xml
$ns = new-object Xml.XmlNamespaceManager $xml.NameTable
$ns.AddNamespace("ns0", "http://schemas.microsoft.com/windows/2004/02/mit/task")
$xml.SelectNodes("ns0:Task", $ns)
Matthew S
  • 900
  • 3
  • 12
  • 26