2016-02-12 98 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); 
      } 

     } 
    } 
}