如何动态填充 XElement(linq to xml)

How to dynamically populate XElement (linq to xml)

有没有办法动态添加新的 XElement 来形成子节点,如下例所示?

XElement xEl = new XElement(
                new XElement("Root",
                  // ** Is there a way I can do this:  
                 //  for(MyObject mObj in myObjects) {
                 //     if (IsXmlObj(mObj)){
                 //         new XElement(mObj.Name, mObj.Value);
                 //       }
                 //   }
                );

我想遍历对象列表以形成子节点。

如果我现在将迭代部分修改为:

 //  for(MyObject mObj in myObjects) {
                 //     if (IsXmlObj(mObj)){
                 //         if (mObject.Name=="Small"){ mObject.Name="Big";}
                 //         new XElement(mObj.Name, mObj.Value);
                 //       }
                 //   }

这样使用Select

var xEl = new XElement("Root",myObjects.Where(mObj=>IsXmlObj(mObj))
                                       .Select(mObj=> new XElement(mObj.Name, mObj.Value)));