c# - Cannot get child nodes of a specific type that contain a specific text -



c# - Cannot get child nodes of a specific type that contain a specific text -

using code below, can receive <job> elements in xml. however, when seek search jobs have kid called <name> , text equals "receiverjob", selectnodes() method returns 0 though job exists.

xmldocument dom = new xmldocument(); dom.load(textboxfilepath.text); xmlnamespacemanager nsmanager = new xmlnamespacemanager(dom.nametable); nsmanager.addnamespace("d", "http://quartznet.sourceforge.net/jobschedulingdata"); xmlnodelist jobelements = dom.documentelement.selectnodes("descendant::d:job[name=receiverjob]", nsmanager);

xml:

<?xml version="1.0" encoding="utf-8"?> <!-- 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"> <processing-directives> <overwrite-existing-data>true</overwrite-existing-data> </processing-directives> <schedule> <job> <name>receiverjob</name> <group>receivergroup</group> <job-type>quartz.server.argumentreceiverjob, quartz.server</job-type> <job-data-map> <entry> <key>receivedargument</key> <value>hamburger</value> </entry> </job-data-map> </job> <trigger> <simple> <name>argumentreceiverjobtrigger</name> <group>argumentreceivergroup</group> <description>simple trigger fire sample job</description> <job-name>receiverjob</job-name> <job-group>receivergroup</job-group> <misfire-instruction>smartpolicy</misfire-instruction> <repeat-count>-1</repeat-count> <repeat-interval>10000</repeat-interval> </simple> </trigger> <job> <name>batchjob</name> <group>batchgroup</group> <job-type>quartz.server.batchjob, quartz.server</job-type> <durable>true</durable> <recover>false</recover> </job> <trigger> <cron> <name>trigger2</name> <group>default</group> <job-name>batchjob</job-name> <job-group>batchgroup</job-group> <cron-expression>0/15 * * * * ?</cron-expression> </cron> </trigger> <job> <name>jobnamexxx</name> </job> <job> <name>jobnamexxx</name> </job> <job> <name>jobnamexxx</name> </job> </schedule> </job-scheduling-data>

i'm not xpath expert, suspect query trying find jobs name element equal receiverjob element. suspect want this:

"descendant::d:job[name/text()='receiverjob']"

it's possible need qualify name element in right namespace:

"descendant::d:job[d:name/text()='receiverjob']"

i strongly consider using linq xml instead, however, simple (imo):

xdocument doc = xdocument.load(textboxfilepath.text); xnamespace ns = "http://quartznet.sourceforge.net/jobschedulingdata"; var jobs = doc.descendants(ns + "job") .where(x => (string) x.element(ns + "name") == "receiverjob");

i'm uncertain xpath syntax, i'm confident linq xml version - in mark of benefit of solution, imo. (of course, xpath expert hadn't used linq xml, opposite might true.)

c# .net xml xpath

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -