将 xmlElement 添加到 XmlNode 中
add xmlElement into a XmlNode
如何将元素添加到 XmlNode。
var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
XmlNode nodes = xmlDoc.SelectSingleNode("/configuration/schedulers");
XML 示例:
<schedulers>
<Scheduler name="test1" alert="4" timerType="type1" cronExpression="0/10 * * * * ?">
<property name="customerName" value="customerA" />
</Scheduler>
<Scheduler name="test2" alert="3" timerType="type2" cronExpression="0/15 * * * * ?" />
<Scheduler name="test3" maxFailureAlert="3" timerType="Type3" cronExpression="0/20 * * * * ?" />
我想添加新的调度程序
<schedulers>
<Scheduler name="test1" alert="4" timerType="type1" cronExpression="0/10 * * * * ?">
<property name="customerName" value="COMMON_MODEL" />
</Scheduler>
<Scheduler name="test2" alert="3" timerType="type2" cronExpression="0/15 * * * * ?" />
<Scheduler name="test3" maxFailureAlert="3" timerType="Type3" cronExpression="0/20 * * * * ?" />
<Scheduler name="test4" maxFailureAlert="3" timerType="Type3" cronExpression="0/50 * * * * ?" />
</schedulers>
您可以使用XmlDocument.CreateElement
方法创建一个元素:
var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
XmlNode nodes = xmlDoc.SelectSingleNode("/configuration/schedulers");
var newElement = xmlDoc.CreateElement("Scheduler");
然后,您可以使用 SetAttribute
:
设置任何属性
newElement.SetAttribute("name", "test4");
newElement.SetAttribute("maxFailureAlert", "3");
newElement.SetAttribute("timerType", "Type3");
newElement.SetAttribute("cronExpression", "0/50 * * * * ?");
并将新元素添加到现有元素:
nodes.AppendChild(newElement);
别忘了保存文档:
xmlDoc.Save(filePath);
如何将元素添加到 XmlNode。
var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
XmlNode nodes = xmlDoc.SelectSingleNode("/configuration/schedulers");
XML 示例:
<schedulers>
<Scheduler name="test1" alert="4" timerType="type1" cronExpression="0/10 * * * * ?">
<property name="customerName" value="customerA" />
</Scheduler>
<Scheduler name="test2" alert="3" timerType="type2" cronExpression="0/15 * * * * ?" />
<Scheduler name="test3" maxFailureAlert="3" timerType="Type3" cronExpression="0/20 * * * * ?" />
我想添加新的调度程序
<schedulers>
<Scheduler name="test1" alert="4" timerType="type1" cronExpression="0/10 * * * * ?">
<property name="customerName" value="COMMON_MODEL" />
</Scheduler>
<Scheduler name="test2" alert="3" timerType="type2" cronExpression="0/15 * * * * ?" />
<Scheduler name="test3" maxFailureAlert="3" timerType="Type3" cronExpression="0/20 * * * * ?" />
<Scheduler name="test4" maxFailureAlert="3" timerType="Type3" cronExpression="0/50 * * * * ?" />
</schedulers>
您可以使用XmlDocument.CreateElement
方法创建一个元素:
var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
XmlNode nodes = xmlDoc.SelectSingleNode("/configuration/schedulers");
var newElement = xmlDoc.CreateElement("Scheduler");
然后,您可以使用 SetAttribute
:
newElement.SetAttribute("name", "test4");
newElement.SetAttribute("maxFailureAlert", "3");
newElement.SetAttribute("timerType", "Type3");
newElement.SetAttribute("cronExpression", "0/50 * * * * ?");
并将新元素添加到现有元素:
nodes.AppendChild(newElement);
别忘了保存文档:
xmlDoc.Save(filePath);