Linq to XML Add element to specific sub tree

后端 未结 3 774
南笙
南笙 2021-01-11 17:08

My XML:


 
  
   
                         
           


        
3条回答
  •  孤独总比滥情好
    2021-01-11 17:31

    You are almost there, your code will be default add the element to the first Customer. You need to search for the attribute id in collection of Customers whose value is 2 -

    document.Element("Bank").Elements("Customer")
            .First(c => (int)c.Attribute("id") == 2).Element("Accounts").Add
                     (
                         new XElement
                             (
                                 "Account", new XAttribute("id", "variable")
                             )
                      );
    

提交回复
热议问题