XElement => Add children nodes at run time

后端 未结 3 448
感动是毒
感动是毒 2020-12-24 11:57

So let\'s assume this is what i want to achieve:


  AAAA
  BBBB
  
     

        
相关标签:
3条回答
  • 2020-12-24 12:05
    var children = new XElement("children");
    XElement x = new XElement("root",
                      new XElement("name", "AAA"),
                      new XElement("last", "BBB"),
                      children);
    
    foreach (Children c in family)
    {
        children.Add(new XElement("child", 
                  new XElement("name", "XXX"),
                  new XElement("last", "TTT")); 
    } 
    
    0 讨论(0)
  • 2020-12-24 12:13
     XElement root = new XElement("root",
                      new XElement("name", "AAA"),
                      new XElement("last", "BBB"));
    
    XElement children = new XElement("children");
    
    foreach (Children c in family)
    {
        children.Add(new XElement("child", 
                  new XElement("name", c.Name),
                  new XElement("last", c.Last)); 
    }
    root.Add(children);
    
    0 讨论(0)
  • 2020-12-24 12:28

    Try this:

    var x = new XElement("root",
                 new XElement("name", "AAA"),
                 new XElement("last", "BBB"),
                 new XElement("children",
                     from c in family
                     select new XElement("child",
                                 new XElement("name", "XXX"),
                                 new XElement("last", "TTT")
                            )
                 )
            );
    
    0 讨论(0)
提交回复
热议问题