C# 如何用C编写xml节点#

C# 如何用C编写xml节点#,c#,xml,C#,Xml,我想在xml文件中的标记“引擎”下写入文本,但现在它可以在标记“服务”下写入。这是我的代码 XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlElement root = doc.DocumentElement; XmlNodeList elemList = root.GetElementsByTagName("Engine"); for

我想在xml文件中的标记“引擎”下写入文本,但现在它可以在标记“服务”下写入。这是我的代码

        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); 
            }
XmlDocument doc=新的XmlDocument();
doc.Load(文件名);
XmlElement根=doc.DocumentElement;
XmlNodeList elemList=root.GetElementsByTagName(“引擎”);
for(int i=0;i
这是xml文件

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

XmlDocument doc=新的XmlDocument();
doc.Load(文件名);
XmlElement根=doc.DocumentElement;
XmlNodeList elemList=root.GetElementsByTagName(“引擎”);
for(int i=0;i
您没有在代码中使用
引擎
元素。您只需将
Host
元素添加到xml根目录的最后一个子目录(
Service
)。还要记住,如果您有多个
引擎
元素,那么您将附加几个具有完全相同值的
主机
元素

首先,我建议你使用。例如,将
主机
元素添加到第一个(如果有)
引擎
元素如下所示:

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>
我喜欢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);
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xml=“”+
"" +
"" +
"" +
"";
XDocument doc=XDocument.Parse(xml);
foreach(文档子体中的XElement引擎(“引擎”))
{
object[]newNode={new-XElement(“主机”),new-XAttribute(“名称”,“本地主机”)),new-XElement(“值”)};
engine.Add(newNode);
}
}
}
}

请分享您的XML文件我如何添加XML文件对不起,我只是个新手here@kanabut我们不需要完整的xml,只需要它的一部分来显示您正在处理的结构。您可以编辑您的问题并复制粘贴示例xml。另外,还不清楚xml中有多少
Engine
元素
foreach(var engine in xdoc.Root.Descendants("Engine"))
  // add host to engine here
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);
            }

        }
    }
}