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

    How to delete XML Parent node when child node is given

    Hello,

    Can you help me with C# code to delete the Parent Node when child node value is provided

    My XML file:

    <Root>
    <Parent1>
    <Child1>C1</Child1>
    <Child2>C2</Child2>
    <Child3>Software Engineer</Child3>
    </Parent1>
    <Parent2>
    <Child3>Rahim</Child3>
    <Child4>40</Child4>
    <Child5>Senior Developer</Child5>
    </Parent2>


    MY Question is: I will be providing child node then i need to delete its parent node along with its all child nodes

    Regards,
    Pavan Kandukuri
  • #768841
    Please understand below code and try to fix your issue. I am deleting the node who has employee id as 2.

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<company><employee><id>1</id ><name>sa </name></employee><employee><id>2</id ><name>ssa</name></employee></company>");
    XmlElement el = (XmlElement)doc.SelectSingleNode("/company/employee[id=2]");
    if (el != null) { el.ParentNode.RemoveChild(el); }
    doc.Save("D:\\output.xml");


  • Sign In to post your comments