0

我是 XPath 的新手。我阅读了整个W3Schools 教程。我想获取<schedule>文档的所有节点。我可以得到我的文档的所有子元素,child::*但只要我添加<schedule>如下,我得到零结果:

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

这是我的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">
  <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 to simply 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>
  </schedule>
</job-scheduling-data>

我最终想要实现的是获得与字符串匹配的所有<name>s <job>

4

2 回答 2

3

那是因为您的 XML 具有默认命名空间:

xmlns="http://quartznet.sourceforge.net/JobSchedulingData"

注册一个指向默认命名空间的前缀,然后使用该前缀和元素的本地名称来引用命名空间中的元素:

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

.NET fiddle demo

于 2014-10-03T13:27:32.043 回答
0

您可以使用以下代码查找所有计划元素。

XmlDocument dom = new XmlDocument(); 
dom.Load(textBoxFilePath.Text);
XmlNodeList jobElements = dom.GetElementsByTagName("schedule");
于 2014-10-03T13:25:21.663 回答