0

Here is my code.

    XElement Response = new XElement("Response",
                                new XElement("RequestId", requestID),
                                new XElement("ResponseId", "E001"),
                                new XElement("Target", target));
    Response.Add(new XElement("templates"));

Now I want to add list of template within templates How do I do that? I use linq to find templates.

     var t = from e1 in wlnResponse.Elements()
                    where e1.Name.ToString() == "templates"
                    select e1;
Dixit Gokhale
  • 601
  • 1
  • 12
  • 32

2 Answers2

3

I'd suggest storing templates XElement in a variable before adding it into your document:

XElement templates = new XElement("templates");
Response.Add(templates );

And then use it to add templates:

var t = from e1 in wlnResponse.Elements()
        where e1.Name.ToString() == "templates"
        select e1;

templates.Add(t.ToArray());
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
0

Have you tried the XElement.Add method?

From MSDN:

This method adds the new content after the existing content of the XContainer.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116