locked
LING TO XML DELETE ITEN RRS feed

  • Question

  • Hi Iam tryed to delete a iten of my XML file, but I need to to select in my query select Element and Attribute to filter and delete de element.
    I try this way but not work. 
    How to make this?

    1       If File.Exists(My.Computer.FileSystem.CurrentDirectory & "\compras.xml") Then  
    2             Dim XML As XElementXElement = XElement.Load(My.Computer.FileSystem.CurrentDirectory & "\compras.xml")  
    3             Try  
    4                 Dim source As IEnumerable(Of XElement) = From b In XML.Elements("Compras") _  
    5                                                          Select b Where b.Element("Codigo").Value = vCodigo.Trim _  
    6                                                          And b.Attribute("TipoTransacao").Value = TipoTransacao 
    7  
    8                 source.Remove()  
    9                 XML.Save(My.Computer.FileSystem.CurrentDirectory & "\compras.xml")  
    10                 Return True  
    11             Catch ex As Exception  
    12                 Throw ex  
    13             End Try  
    14         end if
    15            
     
    The source object return nothing or other error..
    Friday, July 4, 2008 9:05 PM

Answers

  • Hello Marco.
    I am resolv this problem.

         Dim XML As XElementXElement = XElement.Load(My.Computer.FileSystem.CurrentDirectory & "\compras.xml")  
                Try '   
                    Dim source = From c In XML...<Compras> _  
                                        Where c.@Codigo = IDMediaVOD _  
                                        And c.<TipoTransacao>.Value = TipoTransacao.ToString  
     
                    source.Remove()  
                    XML.Save(My.Computer.FileSystem.CurrentDirectory & "\compras.xml")  
                    Return True  
                Catch ex As Exception  
                    Throw ex  
                End Try 
    • Proposed as answer by FSQ Thursday, July 10, 2008 12:23 PM
    • Marked as answer by Marco Zhou Friday, July 11, 2008 10:14 AM
    Thursday, July 10, 2008 12:23 PM

All replies

  • Could you please provide a small, complete and ready-to-run example to demonstrate the issue you are encountering?

     

    Thanks

    Wednesday, July 9, 2008 5:03 AM
  • Hello Marco.
    I am resolv this problem.

         Dim XML As XElementXElement = XElement.Load(My.Computer.FileSystem.CurrentDirectory & "\compras.xml")  
                Try '   
                    Dim source = From c In XML...<Compras> _  
                                        Where c.@Codigo = IDMediaVOD _  
                                        And c.<TipoTransacao>.Value = TipoTransacao.ToString  
     
                    source.Remove()  
                    XML.Save(My.Computer.FileSystem.CurrentDirectory & "\compras.xml")  
                    Return True  
                Catch ex As Exception  
                    Throw ex  
                End Try 
    • Proposed as answer by FSQ Thursday, July 10, 2008 12:23 PM
    • Marked as answer by Marco Zhou Friday, July 11, 2008 10:14 AM
    Thursday, July 10, 2008 12:23 PM