Jagd
Jagd

Reputation: 7306

Linq to Xml and creating elements

I have a string array:

string[] authors = new string[3];
authors[0] = "Charles Dickens";
authors[1] = "Robert Jordan";
authors[2] = "Robert Ludlum";

I am using Linq to XML to read and write XML to a given XML file, but I cannot figure out how to use the XElement class to create XML that represents my authors array.

I know it's something along the lines of

XElement xEle = new XElement("Authors",
from a in authors
select new XElement("Authors", ???????

Upvotes: 0

Views: 159

Answers (1)

Andrew Hare
Andrew Hare

Reputation: 351748

Try something like this:

XElement xEle = new XElement("Authors",
        from a in authors
        select new XElement("Author", a));

That will create an XElement with the following XML content:

<Authors>
  <Author>Charles Dickens</Author>
  <Author>Robert Jordan</Author>
  <Author>Robert Ludlum</Author>
</Authors>

Upvotes: 1

Related Questions