tags:

views:

83

answers:

1

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", ???????
+1  A: 

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>
Andrew Hare
Thanks. The array was actually nested deeper in another class that I'm writing to XML, so the problem was bigger than it appears, but this got me there. I kind of a had one of those smack head moments when it clicked.
Jagd