代码之家  ›  专栏  ›  技术社区  ›  Jagd Dai

Linq到Xml和创建元素

  •  0
  • Jagd Dai  · 技术社区  · 17 年前

    我有一个字符串数组:

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

    我知道这大致是

    XElement xEle = new XElement("Authors",
    from a in authors
    select new XElement("Authors", ???????
    
    1 回复  |  直到 17 年前
        1
  •  1
  •   Andrew Hare    17 年前

    试试这样的:

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

    这将创建一个 XElement

    <Authors>
      <Author>Charles Dickens</Author>
      <Author>Robert Jordan</Author>
      <Author>Robert Ludlum</Author>
    </Authors>
    
    推荐文章