You must Sign In to post a response.
  • Category: XML

    How to add a child node in xml using linq

    <Employees>
    <Employee>
    <Id>1<Id>
    <Name>E1<Name>
    </Employee>
    <Employee>
    <Id>1<Id>
    <Name>E1<Name>
    </Employee>
    <Employee>
    <Id>1<Id>
    <Name>E1<Name>
    </Employee>
    </Employees>

    How the above xml i want to add new xelement <Salary><Salary> for all the records. How to do it using xml?
  • #752528
    XDocument _element = XDocument.Load(@"C:\XMLFile1.xml");
    foreach (XElement xe in _element.Element("Employees").Elements())
    {
    xe.Add(new XElement("Salary", "3253425"));

    }
    _element.Save(@"C:\XMLFile1.xml");

    Let me know if this solves your problem

  • #752529
    Hai Karthik,
    You can use the System.XML namespace to use the XPath related classes.
    using System.Xml;

    var xeElements = xDoc.Root.Elements();
    xeElements.Add(new XElement("Name", "Salary"));

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #752579
    try it like




    XElement element = xDoc.XPathSelectElement("//Employee[id=E1]");
    xe.Add(new XElement("Name", "Hello Anil"));

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM


  • Sign In to post your comments