How to Delete a XML node on a particular line using C#!!

I am trying to delete a particular text value along with its root in an XML file  on a given line number, using C#. But i do not know how to do it, Please can anyone suggest me a solution!!


Answer this question

How to Delete a XML node on a particular line using C#!!

  • 6stringer9

    This is not possible using XML apis.
    Mark gave you work-around to do that but without using line number.

    Another workaound may be,
    - Use File apis, Open file and get the StreamReader object for that file.
    - StreamReader has "ReadLine" method which give you line from the file as string so till EOF read each line and add that into some variable
    - Then you can compare it with the text you have on that line and if its that line then dont add that to the variable
    - At the end, save that value in varible to the file back

    Again this is weired way but just a work around if you want to try, In this case too, its not possible to delete stuff giving line number.

    Mark's solution would be best choice for XML file

    HTH,



  • Grant Holliday

    Hi,

    you can't really delete by line number since one the data is read into memory there are no line numbers, however you can use XPath and XmlDocument::SelectSingleNode to locate the node if you know what you are searching for i.e. /Root/Books/Book[@Title='hello'] then you can get the nodes parent and delete the child i.e.

    XmlNode selectedNode = .....

    selectedNode.ParentNode.RemoveChild(selectedNode);

    Mark.



  • How to Delete a XML node on a particular line using C#!!