XML (with namespace) read and write operations

Source: Internet
Author: User

XML file:

<?xml version= "1.0" encoding= "UTF-8"?>
<!--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>

<trigger>
<cron>
<name>EmailBDOTrigger_2</name>
<group>EmailBDOTrigger_2</group>
<job-name>EmailBDOTriggerGroup_2</job-name>
<job-group>EmailBDOJob_2</job-group>
<cron-expression>1-2 1-3 1-3 * *? </cron-expression>
</cron>
</trigger>

</schedule>
</job-scheduling-data>

Background:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Xml;
Using System.Web;

Namespace Cronmaker
{
Class Xmlhandler
{
String xmlpath = @ "... \.. \quartz_jobs.xml ";
String xmlpath = HttpContext.Current.Server.MapPath ("~/quartz_jobs.xml");

Private String Txtcorn;
Public Xmlhandler (String Txtcorn)
{
This.txtcorn = Txtcorn;
}

public void Changnode ()
{
XmlTextReader tReader = new XmlTextReader ("\\quartz_jobs.xml");
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.normalize ();
Xmldoc.load (Xmlpath);
Namespaces in XML
XmlNamespaceManager xnm = new XmlNamespaceManager (xmldoc.nametable);
Xnm. AddNamespace ("Mxh", "Http://quartznet.sourceforge.net/JobSchedulingData");
String XPath = @ "/mxh:job-scheduling-data/mxh:schedule/mxh:trigger/mxh:cron";

XmlNodeList nodes = Xmldoc.selectsinglenode (XPath, xnm). ChildNodes;
foreach (XmlNode item in nodes)
{
XmlElement element = Item as XmlElement;
Console.WriteLine (element. InnerText);
if (element. Name = = "Cron-expression")
Element. InnerText = Txtcorn;
if (item. Name = = "Cron-expression")
Item. InnerText = Txtcorn;
}
Xmldoc.save (Xmlpath);
}
public void AddNode ()
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.normalize ();
Xmldoc.load (Xmlpath);
XmlNamespaceManager xnm = new XmlNamespaceManager (xmldoc.nametable);
Xnm. AddNamespace ("Mxh", "Http://quartznet.sourceforge.net/JobSchedulingData");
String XPath = @ "/mxh:job-scheduling-data/mxh:schedule";
XmlNode root = Xmldoc.selectsinglenode (XPath, xnm);

XmlNode root = Xmldoc.selectsinglenode ("Job-scheduling-data/schedule");

XmlElement trigger = Xmldoc.createelement ("Trigger", XmlDoc.DocumentElement.NamespaceURI);
XmlElement corn = xmldoc.createelement ("cron", XmlDoc.DocumentElement.NamespaceURI);
Join the name node
XmlElement sub1 = xmldoc.createelement ("name", XmlDoc.DocumentElement.NamespaceURI);
Sub1. InnerText = "emailbdotrigger_2";
Corn. AppendChild (SUB1);
Group node
XmlElement sub2 = xmldoc.createelement ("group", XmlDoc.DocumentElement.NamespaceURI);
Sub2. InnerText = "emailbdotrigger_2";
Corn. AppendChild (SUB2);
Job-name node
XmlElement sub3 = xmldoc.createelement ("Job-name", XmlDoc.DocumentElement.NamespaceURI);
Sub3. InnerText = "emailbdotriggergroup_2";
Corn. AppendChild (SUB3);
Job-group node
XmlElement sub4 = xmldoc.createelement ("Job-group", XmlDoc.DocumentElement.NamespaceURI);
Sub4. InnerText = "emailbdojob_2";
Corn. AppendChild (SUB4);
Cron-expression node
XmlElement sub5 = xmldoc.createelement ("Cron-expression", XmlDoc.DocumentElement.NamespaceURI);
Sub5. InnerText = Txtcorn;
Corn. AppendChild (SUB5);

Trigger. AppendChild (corn);
The problem: In Appendchildren, if the parentnode is with the xmlns attribute, the child node will appear xmlns= "" Property if you do not specify xmlns when adding nodes or when you specify NULL
WORKAROUND: Instead of specifying a namespace for a child node, you should specify the same namespace as the parent node
Root. AppendChild (trigger);

Xmldoc.save (Xmlpath);
}

}
}

XML (with namespace) read and write operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.