2016-02-12 110 views
1

我想要写在标签“引擎”,在XML文件中的文本,但现在它可以在标签的“服务”这里是我的代码编写XML节点我怎么能写在C#

 XmlDocument doc = new XmlDocument(); 
     doc.Load(filename); 
     XmlElement root = doc.DocumentElement; 
     XmlNodeList elemList = root.GetElementsByTagName("Engine"); 
      for (int i = 0; i < elemList.Count; i++) 

      { 
       XmlNode head = doc.CreateNode(XmlNodeType.Element, "Host", null); 
       XmlAttribute na = doc.CreateAttribute("name"); 
       na.Value = "url"; 

       XmlNode nodeTitle = doc.CreateElement("Valve"); 
       XmlAttribute className = doc.CreateAttribute("className"); 
       className.Value = "org.apache.catalina.valves.AccessLogValve"; 

       doc.DocumentElement.LastChild.AppendChild(head); 
       doc.Save(filename); 
      } 

这里是xml文件

<Server> 
     <Service name="Catalina"> 
      <Engine name="Catalina" > 
      <Host name="localhost"> 
       <Valve /> 
      </Host> 
      </Engine> 
     </Service> 
</Server> 
+0

请分享您的XML文件 –

+0

如何添加xml文件对不起,我只是在这里的新手 – kanabut

+0

@ kanabut我们不需要整个XML,但只是其中的一部分,它显示你正在工作的结构。你可以编辑你的问题,并在那里复制粘贴示例xml。另外还不清楚你在xml –

回答

0

你不使用你的代码Engine元素。您只需将Host元素添加到xml根的最后一个子元素(Service)。另请注意,如果您有几个Engine元素,则您将附加几个具有完全相同值的Host元素。

首先,我建议你使用LINQ to XML。例如。加入Host元素第一(如果有的话)Engine元素看起来像:

var xdoc = XDocument.Load(filename); 

var engine = xdoc.Root.Descendants("Engine").FirstOrDefault(); 

if (engine != null) 
{ 
    engine.Add(new XElement("Host", 
     new XAttribute("name", "url"), 
     new XElement("Valve", 
      new XAttribute("className", "org.apache.catalina.valves.AccessLogValve")))); 

    xdoc.Save(filename); 
} 

对于示例XML结果将是

<Server> 
    <Service name="Catalina"> 
    <Engine name="Catalina"> 
     <Host name="localhost"> 
     <Valve /> 
     </Host> 
     <Host name="url"> 
     <Valve className="org.apache.catalina.valves.AccessLogValve" /> 
     </Host> 
    </Engine> 
    </Service> 
</Server> 

如果你想修改每个Engine元素,那么就使用循环:

foreach(var engine in xdoc.Root.Descendants("Engine")) 
    // add host to engine here 
+0

中有多少'Engine'元素非常感谢你 – kanabut

0
XmlDocument doc = new XmlDocument(); 
    doc.Load(filename); 
    XmlElement root = doc.DocumentElement; 
    XmlNodeList elemList = root.GetElementsByTagName("Engine"); 
     for (int i = 0; i < elemList.Count; i++) 

     { 
      XmlNode head = doc.CreateNode(XmlNodeType.Element, "Host", null); 
      XmlAttribute na = doc.CreateAttribute("name"); 
      na.Value = "url"; 

      // nodeTitle is not appended the document: 
      //XmlNode nodeTitle = doc.CreateElement("Valve"); 
      // className is not appended to any node either: 
      //XmlAttribute className = doc.CreateAttribute("className"); 
      //className.Value = "org.apache.catalina.valves.AccessLogValve"; 

      // this line will add your node to the last node of your document: 
      //doc.DocumentElement.LastChild.AppendChild(head); 
      // if you want to add it to every "Engine" node: 
      elemList[i].AppendChild(head); 
      //doc.Save(filename); 
     } 
     // save at the end, when you're done with the document: 
     doc.Save(filename); 
0

我喜欢XML的LINQ

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Server><Service name=\"Catalina\">" + 
       "<Engine name=\"Catalina\"></Engine>" + 
       "<Engine name=\"Catalina\"></Engine>" + 
       "<Engine name=\"Catalina\"></Engine>" + 
       "</Service></Server>"; 

      XDocument doc = XDocument.Parse(xml); 

      foreach(XElement engine in doc.Descendants("Engine")) 
      { 
       object[] newNode = { new XElement("Host", new XAttribute("name", "localhost")), new XElement("Value")}; 
       engine.Add(newNode); 
      } 

     } 
    } 
}