简体   繁体   English

如何动态填充XElement(Linq到xml)

[英]How to dynamically populate XElement (linq to xml)

Is there a way I can dynamically add a new XElement to form child nodes as in the example below? 有没有一种方法可以动态添加新的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);
                 //       }
                 //   }
                );

I would like to iterate through an object list to form the sub nodes. 我想遍历对象列表以形成子节点。

What if I now modify the iterating part to become: 如果我现在将迭代部分修改为:

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

Use a Select this way: 使用“ Select方式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM