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?

0

1 Answer 1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.