Aida E
Aida E

Reputation: 1100

How to add elements in an XML file?

How should I add X there in XElement ?

       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)
                ));
            }
        }

so it should look like this :

<?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>

Upvotes: 0

Views: 233

Answers (3)

DarkWanderer
DarkWanderer

Reputation: 8866

You can pass an XElement[] or IEnumerable<XElement> to XElement's constructor:

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);

Upvotes: 2

Pradeep D G
Pradeep D G

Reputation: 55

May this will help to add nodes...

        //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);

Upvotes: 0

Mohammad Arshad Alam
Mohammad Arshad Alam

Reputation: 9862

You can try this:

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)
        ));
    }

}

Upvotes: 0

Related Questions