如何在XML文件中添加元素?

时间:2014-01-14 11:41:01

标签: c# xml file

我应该如何在XElement中添加X?

       XDocument triggerDocument = new XDocument(
                           new XDeclaration("1.0", "utf-8", null));
            XElement triggerRoot = new XElement("config",
            new XElement("maketool-config",
            new XElement("hmi", new XElement("Messages",X))));
            triggerDocument.Add(triggerRoot);
            triggerDocument.Save(Path.Combine(outPath, "_triggers.xml"));

        for (int i = 0; i <= events.Count; i++)
        {
            foreach (var item in events)
            {
                triggerRoot.Add(new XElement("n",
                new XAttribute("page", item.page),
                new XAttribute("sequence", item.sequence),
                new XAttribute("priority", item.priority),
                new XAttribute("errorText", item.errorText)
                ));
            }
        }

所以看起来应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<config schema ="sdk-hmi.xsd">
  <maketool-config>
    <hmi>
      <messages>
        <n page="" sequence="" priority="" errorText="" />
        <n page="" sequence="" priority="" errorText="" />
        <n page="" sequence="" priority="" errorText="" />
        <n page="" sequence="" priority="" errorText="" />
        <n page="" sequence="" priority="" errorText="" />
      </messages>
    </hmi>
  </maketool-config>
</config>

3 个答案:

答案 0 :(得分:2)

您可以将XElement[]IEnumerable<XElement>传递给XElement的构造函数:

var messages = events.Select(item => new XElement("n",
                new XAttribute("page", item.page),
                new XAttribute("sequence", item.sequence),
                new XAttribute("priority", item.priority),
                new XAttribute("errorText", item.errorText)
               ));

XDocument triggerDocument = new XDocument(
                   new XDeclaration("1.0", "utf-8", null));

XElement triggerRoot = new XElement("config",
    new XElement("maketool-config",
    new XElement("hmi",
    new XElement("Messages", messages))) // <<<--- This is the important part.
);
triggerDocument.Add(triggerRoot);

答案 1 :(得分:0)

你可以试试这个:

XDocument triggerDocument = new XDocument(
        new XDeclaration("1.0", "utf-8", null));

XElement triggerRoot = new XElement("config",
new XElement("maketool-config",
new XElement("hmi", new XElement("Messages"))));
triggerDocument.Add(triggerRoot);

XElement msgNode = triggerRoot.Elements("Messages")
                                        .SingleOrDefault();

if (msgNode != null)
{
    foreach (var item in events)
    {
        msgNode.Add(new XElement("n",
        new XAttribute("page", item.page),
        new XAttribute("sequence", item.sequence),
        new XAttribute("priority", item.priority),
        new XAttribute("errorText", item.errorText)
        ));
    }

}

答案 2 :(得分:0)

这可能有助于添加节点......

        //file name
        string filename = @"d:\temp\XMLFile2.xml";

        //create new instance of XmlDocument
        XmlDocument doc = new XmlDocument();

        //load from file
        doc.Load(filename);

        //create node and add value
        XmlNode node = doc.CreateNode(XmlNodeType.Element, "Genre_Genre_Country", null);
        node.InnerText = "this is new node";

        //add to elements collection
        doc.DocumentElement.AppendChild(node);

        //save back
        doc.Save(filename);