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

    How to delete XML Parent node when child node is given


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

    My XML file:

    <Child3>Software Engineer</Child3>
    <Child5>Senior Developer</Child5>

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

    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); }

  • Sign In to post your comments