我如何在C#中编写xml节点

时间:2016-02-12 08:46:21

标签: c# xml

我想在标签" Engine"下的xml文件中写文字。但是现在它可以在标签#34; Service"这是我的代码

        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>

3 个答案:

答案 0 :(得分:1)

您未在代码中使用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元素,那么只需使用loop:

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

答案 1 :(得分: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); 

答案 2 :(得分:0)

我喜欢XML Linq

app-name<=255