1

This is the XML-file, I want to get the Xpath for cron-expression, actually I want to change the inner text. I have tried many methods but nothing works and I just want to get the direct answer. I have tried like local-name and XmlnamespaceManager.addnamespace, but it does not work.

<!-- This file contains job definitions in schema version 2.0 format -->

<job-scheduling-data 
xmlns="http://quartznet.sourceforge.net/JobSchedulingData" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">

  <processing-directives>
    <overwrite-existing-data>true</overwrite-existing-data>
  </processing-directives>

  <schedule>

    <job>
        <name>SampleJob</name>
        <group>SampleGroup</group>
        <description>My job</description>
        <job-type>Quartz.Server.SampleJob, Quartz.Server</job-type>
        <durable>true</durable>
        <recover>false</recover>
    </job>

    <trigger>
     <cron>
        <name>SampleTrigger</name>
        <group>SampleGroup</group>
        <description>Simple trigger to simply fire sample job</description>
        <job-name>SampleJob</job-name>
        <job-group>SampleGroup</job-group>
        <misfire-instruction>SmartPolicy</misfire-instruction>
        <cron-expression>0 * 15 * * ?</cron-expression>
      </cron>
    </trigger>
  </schedule>
</job-scheduling-data>

This is my code:

 protected void Button2_Click(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("D:/Chrome/Quartz.NET-2.4.1/src/Quartz.Examples/quartz_jobs.xml");
        XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
        xnm.AddNamespace("h", 
"http://quartznet.sourceforge.net/JobSchedulingData");

   XmlNode XN = doc.SelectSingleNode("h:job-scheduling-data/h:schedule/h:trigger/h:cron/h:cron-expression",xnm);


 //     XmlNode XN = doc.SelectSingleNode("/*[local-name() = 'job-scheduling-data']/*[local-name() = 'schedule']/*[local-name() = 'trigger']/*[local-name() = 'cron']/*[local-name() = 'cron-expression']");
    XN.InnerText = "0";
    doc.Save("D:/Chrome/Quartz.NET-2.4.1/src/Quartz.Examples/quartz_jobs.xml");//
}
Leafstar
  • 25
  • 1
  • 7
  • Why do you have all those `h:` prefixes in your xpath when the elements in your xml don't have it? – NtFreX Jul 10 '17 at 05:36
  • @NtFreX it was the same consequence if i remove those `h:` , i was trying different kinds of approaches because im not familiar with XML files – Leafstar Jul 10 '17 at 05:45
  • When I copy the xml you have in your question and then take the xpath from `SelectSingleNode` without all the `h:` I get the correct element. – NtFreX Jul 10 '17 at 05:56
  • @NtFreX really!! ? can you show me the code ? i have suffered from this for a long time.... – Leafstar Jul 10 '17 at 06:05
  • @NtFreX It's not work with me , i deleted all the `h:`, and it shows `Object reference not set to an instance of an object.` – Leafstar Jul 10 '17 at 06:18
  • I've posted the code which works on my end. Maybe also [this](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) helps. – NtFreX Jul 10 '17 at 06:22

1 Answers1

0

This is how it is working with the xml you posted. In case the element can be found noting will be done to the saved xml.

var doc = new XmlDocument();
doc.Load("D:/Chrome/Quartz.NET-2.4.1/src/Quartz.Examples/quartz_jobs.xml");

var namespaceManager = new XmlNamespaceManager(doc.NameTable);
namespaceManager.AddNamespace("x", "http://quartznet.sourceforge.net/JobSchedulingData");

XmlNode selectedNode = doc.SelectSingleNode("x:job-scheduling-data/x:schedule/x:trigger/x:cron/x:cron-expression", namespaceManager);
if (selectedNode != null)
{
    selectedNode.InnerXml = "0";
    doc.Save("D:/Chrome/Quartz.NET-2.4.1/src/Quartz.Examples/quartz_jobs.xml");
}
NtFreX
  • 10,379
  • 2
  • 43
  • 63
  • can you post the XML file after manipulation. I think you did not change the `InnerText` successfully because if `selectdNode` is null, it will execute the `else` case – Leafstar Jul 10 '17 at 06:39
  • @muxingwang If the element cannot be found nothing will be done. This can only be possible if the read xml is not like the one you posted in your question. – NtFreX Jul 10 '17 at 06:41
  • @muxingwang I meant the xml not the code. Look at the xml file. It must be different to the xml you posted. – NtFreX Jul 10 '17 at 06:45
  • The XML file i posted is with some namespace , i think the namespaces break the normal XPATH. Can you new a XML file and copy my file to it to test your code? – Leafstar Jul 10 '17 at 06:47
  • @muxingwang I had mistakenly taken the default namespace out. I've updated the answer. Like so it works on my end. – NtFreX Jul 10 '17 at 07:17