Disasterkid Disasterkid - 1 year ago 124
C# Question

SelectNodes does not return the child values

I am new to XPath. I read the entire W3Schools tutorial. I would like to get all the

nodes of my document. I can get all the child elements of my document with
but as soon as I add
like the following, I get zero results:

XmlDocument dom = new XmlDocument();
XmlNodeList jobElements = dom.DocumentElement.SelectNodes("child::schedule");

This is my xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This file contains job definitions in schema version 2.0 format -->
<job-scheduling-data version="2.0" xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<job-type>Quartz.Server.ArgumentReceiverJob, Quartz.Server</job-type>
<description>Simple trigger to simply fire sample job</description>
<job-type>Quartz.Server.BatchJob, Quartz.Server</job-type>
<cron-expression>0/15 * * * * ?</cron-expression>

What I would ultimately like to achieve is to get all the
s of the
s that match a string.

Answer Source

That's because your XML has default namespace :


Register a prefix that points to default namespace, then use that prefix along with the element's local name to reference an element in namespace :

XmlDocument dom = new XmlDocument(); 
XmlNamespaceManager nsManager = new XmlNamespaceManager(dom.NameTable);
nsManager.AddNamespace("d", dom.DocumentElement.NamespaceURI);
XmlNodeList jobElements = dom.DocumentElement.SelectNodes("child::d:schedule", nsManager);

[.NET fiddle demo]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download