Hi,

I am trying to add a new xElement to an existing xDocument but it needs to be added to a specific place in the structure.

Here is a snippet of the current structure, you can see that I have added ID xAttributes to the xElement td in order to specify the correct position for adding the new xElement.

from xfile in filePaths
    select new XElement("tr",
    new XElement("td", new XAttribute("ID", 1), new XAttribute("style", "border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;"), new XAttribute("align", "center"), new XAttribute("valign", "middle"),
    new XElement("a", new XAttribute("href", xfile), new XAttribute("target", "_new"), new XElement("img", new XAttribute("src", xfile), new XAttribute("width", "100"), new XAttribute("border", "0"))),
    new XElement("td", new XAttribute("ID", 2), new XAttribute("style", "border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;"), new XAttribute("align", "center"), new XAttribute("valign", "middle"),
    new XElement("b", Path.GetFileNameWithoutExtension(xfile))),
    new XElement("td", new XAttribute("ID", 3), new XAttribute("style", "border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;"), new XAttribute("align", "center"), new XAttribute("valign", "middle")
    )))))))));

I wish to add a new xElement of b directly after the xElement td that has an xAttribute ID=3.

Something along the lines of this

xDocument.Root.Element("td").Add(
                        new XElement("b", DateTaken));

but unfortunately this is not specific to the xElement td with xAttribute ID=3.

Any help would be appreciated.

Kind regards..,

I played around with what you gave me and I think I may have found what you were looking for. Below is what I was playing with. I was able to get it to work. Here's the post that I found on it: http://stackoverflow.com/questions/13333712/linq-to-xml-add-element-to-specific-sub-tree

XDocument doc = new XDocument();
            string path = @"c:\test.xml";

            var d = new XElement("tr",
                       new XElement("td", new XAttribute("ID", 1), new XAttribute("style", "border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;"), new XAttribute("align", "center"), new XAttribute("valign", "middle"),
                       new XElement("a", new XAttribute("href", string.Empty), new XAttribute("target", "_new"), new XElement("img", new XAttribute("src", string.Empty), new XAttribute("width", "100"), new XAttribute("border", "0")))),
                       new XElement("td", new XAttribute("ID", 2), new XAttribute("style", "border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;"), new XAttribute("align", "center"), new XAttribute("valign", "middle"),
                       new XElement("b", string.Empty)),
                       new XElement("td", new XAttribute("ID", 3), new XAttribute("style", "border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;"), new XAttribute("align", "center"), new XAttribute("valign", "middle")
                       ));

            d.Save(path);
            doc = XDocument.Load(path);

            /// THIS IS THE LINE BELOW THAT YOU WANT TO TRY///
            doc.Element("tr").Elements("td").First(c => (int)c.Attribute("ID") == 3).Add(new XElement("b", "DataTaken"));

            doc.Save(path);
This article has been dead for over six months. Start a new discussion instead.