Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with deleting record in xml file

Hello,

I'm trying to delete records in my link.xml file based on  the value of the Link_ID. I've tried hard coding the value of Link_ID (3) to test the code, but it doesn't delete the record where Link_ID = 3, can you please help me fix this problem.

Thanks,

Victor
Dim an As String
        an = MsgBox("Are you sure you want to Delete this record?", MsgBoxStyle.YesNo, "Warning before Attempting to Save Data")
        MsgBox(CmbRec.Text)
        MsgBox(DropDownList1.Text)
        Dim x As Integer
        x = 3

        If an = MsgBoxResult.Yes Then
            Dim xml As String = LinkStream.InnerXml
            Dim newline As String = Environment.NewLine
            '  Dim replacement As String = "<Link><ReceiverID>" & x & "</ReceiverID><DonorID>" & DropDownList1.Text & "</DonorID></Link>"
            Dim replacement As String = "<Link><Link_ID>" & 3 & "</Link_ID></Link>"

            LinkStream.InnerXml = xml.Replace(replacement, "")
            LinkStream.Save(Server.MapPath("~/App_Data/Link.xml"))
            Dim cnt As Integer = LinkStream.SelectNodes("Root/Link").Count
            'txtTotalRecords.Text = cnt
            ' btnNext.PerformClick()
            MsgBox("Record was deleted")
        End If
        If an = MsgBoxResult.No Then
            MsgBox("Record was not deleted")
        End If
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Use the remove child method:
http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.removechild.aspx

here is a sample using xpath:
http://bytes.com/topic/net/answers/176553-remove-node
another example using RemoveFromTree method:
http://www.example-code.com/vbdotnet/xmlDelete.asp
Note: For more help, please provide a sample of your xml file
ASKER CERTIFIED SOLUTION
Avatar of KBerger
KBerger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Victor  Charles

ASKER

Hi,

below is an example of my LInk.xml file, can you please help me implemenyt the Xpath approach. For example, I would like to delete:

 <Link>
    <Link_ID>2</Link_ID>
    <ReceiverID>5</ReceiverID>
    <DonorID>3</DonorID>
  </Link>
 
where Link_ID = 2. (2 will be the ID of a Dropdownlist control)

Link.xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <Link>
    <Link_ID>1</Link_ID>
    <ReceiverID>3</ReceiverID>
    <DonorID>2</DonorID>
  </Link>
  <Link>
    <Link_ID>2</Link_ID>
    <ReceiverID>5</ReceiverID>
    <DonorID>3</DonorID>
  </Link>
  <Link>
    <Link_ID>3</Link_ID>
    <ReceiverID>3</ReceiverID>
    <DonorID>4</DonorID>
  </Link>
  <Link>
    <Link_ID>4</Link_ID>
    <ReceiverID>5</ReceiverID>
    <DonorID>
    </DonorID>
  </Link>
  <Link>
    <Link_ID>5</Link_ID>
    <ReceiverID>4</ReceiverID>
    <DonorID>
    </DonorID>
  </Link>
  <Link>
    <Link_ID>6</Link_ID>
    <ReceiverID>5</ReceiverID>
    <DonorID>2</DonorID>
  </Link>
  <Link>
    <Link_ID>7</Link_ID>
    <ReceiverID>6</ReceiverID>
    <DonorID>4</DonorID>
  </Link>
</Root>

Thanks,

Victor
Avatar of KBerger
KBerger

Hi Victor,

see above. My first posting does just this.

Cheers,

Kristof
Kristof,

Can you please send me the VB.NET version.

Thanks,

Victor
Hi,

It worked, but when I tried to include multiple condition with the code below:

Dim x As Integer
        Dim y As Integer
        x = 3
        y = 2
        Dim doc As New XmlDocument()
        doc.Load(Server.MapPath("~/App_Data/Link.xml"))
        Dim node As XmlNode = doc.SelectSingleNode("//Link/Link_ID[text()='" & x & "' and ReceiverID[text()='" & y & "']")
        If node IsNot Nothing Then
            Dim linkNode As XmlNode = node.ParentNode
            linkNode.ParentNode.RemoveChild(linkNode)
        End If
        doc.Save(Server.MapPath("~/App_Data/Link.xml"))

I receive the following error:
'//Link/Link_ID[text()='3' and ReceiverID[text()='2']' has an invalid token.

On Line:

Dim node As XmlNode = doc.SelectSingleNode("//Link/Link_ID[text()='" & x & "' and ReceiverID[text()='" & y & "']")


How do I fix this error?

Thanks,

Victor
Hi,

this won't work, as my XPath-Qery returns the Link_ID node (this is why I select the parent afterwards).
What You want to achieve is to select the Link-Node directly, dependant on 2 child-nodes.
Your approach would be feasable, if your XML was formatted in a different way.

<Links>
<Link id="3" receiverid="2"/>
</Link<>

This way you could :

            XmlDocument doc = new XmlDocument();
            doc.Load(AppDomain.CurrentDomain.BaseDirectory + "links.xml");
            XmlNode node = doc.SelectSingleNode("//Link[@id='3' and @receiverid='2']");
            if (node != null)
            {
                node.ParentNode.RemoveChild(node);
            }
            doc.Save(AppDomain.CurrentDomain.BaseDirectory + "links.xml");

Cheers,

Kristof
Will I need to change my for the existing format?
<Links>
<Link id>=3</Link id>
<receiverid>2</receiverid/>
</Links>

I hard coded the example that I sent you, but the project will need to use thye Link_ID of the records founds from filtering the data for where Receiver_ID = CmRec.SelectedValue.Item. How would I include the value of the Link_ID found to the equation, if I can do data, I can just use the Link_ID data to delete the record, since there will always be a unique Link_ID. Below is the code that I'm using to filter the data, I now need to retreive the Link_ID value and include it in Dropdownlist2 to include in my code to delete the records selected.

  '  If IsPostBack = True Then
        ' create a new XML Document object for each XML file
        Dim xdDonor As New Xml.XmlDocument
        Dim xdLink As New Xml.XmlDocument
        Dim xdReceiver As New Xml.XmlDocument

        ' load all of the XML documents
        xdDonor.Load(Server.MapPath("~/App_Data/Donor.xml"))
        xdLink.Load(Server.MapPath("~/App_Data/Link.xml"))
        xdReceiver.Load(Server.MapPath("~/App_Data/Receiver.xml"))

        ' This outer loop will iterate over the Link file where the ReceiverID node is equal to our search criteria
        For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/Link[ReceiverID='" & C1Country.SelectedItem.Value & "']")

            ' Extract the ReceiverID and DonorID values from the current node
            Dim ReceiverID As String
            Dim DonorID As String
            Dim Link_ID As String

            Link_ID = xnLink.SelectSingleNode("Link_ID").InnerText
            ReceiverID = xnLink.SelectSingleNode("ReceiverID").InnerText
            DonorID = xnLink.SelectSingleNode("DonorID").InnerText

            ' Perform another similar search based upon the Donor XML file
            For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root/Donor[DonorID='" & DonorID & "']")

                ' Extract the DonorID and Name values from the current node
                Dim DonorID2 As String
                Dim Name As String

                DonorID2 = xnDonor.SelectSingleNode("DonorID").InnerText
                Name = xnDonor.SelectSingleNode("Name").InnerText
                dtDonor.Rows.Add({DonorID2, Name})
            Next

            ' Perform yet another practically identical search on the Receiver XML file
            For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/Root/Receiver[ReceiverID='" & ReceiverID & "']")
                ' Extract the ReceiverID and Name values from the current node
                Dim ReceiverID2 As String
                Dim Name As String

                ReceiverID2 = xnReceiver.SelectSingleNode("ReceiverID").InnerText
                Name = xnReceiver.SelectSingleNode("Name").InnerText
                dtReceiver.Rows.Add({ReceiverID2, Name})

            Next

            ' Perform yet another practically identical search on the Receiver XML file
            For Each xnLink_ID As Xml.XmlNode In xdLink.SelectNodes("/Root/Receiver[ReceiverID='" & ReceiverID & "']")
                ' Extract the ReceiverID and Name values from the current node
                Dim LinkID2 As String

                LinkID2 = xnLink_ID.SelectSingleNode("Link_ID").InnerText
                dtLinkID.Rows.Add({LinkID2})

            Next
        Next
       
        CmbDonor.DataSource = dtDonor
        CmbDonor.DataTextField = "Name"
        CmbDonor.DataValueField = "DonorID"
        CmbDonor.DataBind()

        CmbRec.DataSource = dtReceiver
        CmbRec.DataTextField = "Name"
        CmbRec.DataValueField = "ReceiverID"
        CmbRec.DataBind()
 
        'C1Combobox Contro
        CmbDonor.DataSource = dtDonor
        CmbDonor.DataMember = "Name"
        CmbDonor.DataTextField = "Name"
        CmbDonor.DataValueField = "DonorID"
        CmbDonor.DataBind()

        DropDownList1.DataSource = dtDonor
        DropDownList1.DataTextField = "Name"
        DropDownList1.DataValueField = "DonorID"
        DropDownList1.DataBind()

        DropDownList2.DataSource = dtLinkID
        DropDownList2.DataTextField = "Link_ID"
        DropDownList2.DataValueField = "Link_ID"
        DropDownList2.DataBind()


My code than to delete the reord would be:

  MsgBox(DropDownList2.Text) ****Returning Blank Value
        Dim doc As New XmlDocument()
        doc.Load(Server.MapPath("~/App_Data/Link.xml"))
        Dim node As XmlNode = doc.SelectSingleNode("//Link/Link_ID[text()='" & DropDownList2.Text & "']")
        If node IsNot Nothing Then
            Dim linkNode As XmlNode = node.ParentNode
            linkNode.ParentNode.RemoveChild(linkNode)
        End If
        doc.Save(Server.MapPath("~/App_Data/Link.xml"))

Thaks,

Victor


The problem is, I'm unable to pass the Link_ID value to DropdownList2, the value of the control is blank, I know there must be an easier way to achieve this.
Hi again,

I'm totally confused. I don't see, what your point is. You asked about deleting a given node from an XML-File. I think we cleared that out.
Are you now opening another question (concerning the comboboxes) ?

Is your problem this line ?
 Dim node As XmlNode = doc.SelectSingleNode("//Link/Link_ID[text()='" & DropDownList2.Text & "']")

If I understand correctly, DropDownList2.Text is null.
Well. Is it configured to be a DropDownList or a ComboBox?
Try using DropDownList2.SelectedValue instead of DropDownList2.Text

Regards,

Kristof
Sorry for confusing matters, I need to delete a given node in an xml file based on the Link_ID found after filtering the data, my approach is to past the Link_ID value from Link.xml after filtering the data to a Dopdownlist and use the DropdownList in your equation to delete the node, but the dropdownlist is not loaded with the Link_ID when I filter the data. I think the problem is in this part of the code:

' Perform yet another practically identical search on the Link XML file
            For Each xnLink_ID As Xml.XmlNode In xdLink.SelectNodes("/Root/Receiver[ReceiverID='" & ReceiverID & "']")
                ' Extract Link_ID and Name from the current node
                Dim LinkID2 As String
                LinkID2 = xnLink_ID.SelectSingleNode("Link_ID").InnerText
                dtLinkID.Rows.Add({LinkID2})
            Next

      DropDownList2.DataSource = dtLinkID
        DropDownList2.DataTextField = "Link_ID"
        DropDownList2.DataValueField = "Link_ID"
        DropDownList2.DataBind()

The DropdowmList shoud then load the data with the Link_ID, but it returns a blank value.
Any ideas why I'm unable to pass the Link_ID to the dDropdownList?
Thanks,
Victor
Hi,

 It worked with the following code, challenge will be when I have multiple Link_IDs after filtering the data, to keep track of the correct Link_ID to delete the correct node as Press the Next or Previous buttons.

 For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/Link[ReceiverID='" & C1Country.SelectedItem.Value & "']")

            ' Extract the ReceiverID and DonorID values from the current node
            Dim ReceiverID As String
            Dim DonorID As String
            Dim Link_ID As String

            Link_ID = xnLink.SelectSingleNode("Link_ID").InnerText
            ReceiverID = xnLink.SelectSingleNode("ReceiverID").InnerText
            DonorID = xnLink.SelectSingleNode("DonorID").InnerText

            ' Perform another similar search based upon the Donor XML file
            For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root/Donor[DonorID='" & DonorID & "']")

                ' Extract the DonorID and Name values from the current node
                Dim DonorID2 As String
                Dim Name As String

                DonorID2 = xnDonor.SelectSingleNode("DonorID").InnerText
                Name = xnDonor.SelectSingleNode("Name").InnerText
                dtDonor.Rows.Add({DonorID2, Name})
            Next

            ' Perform yet another practically identical search on the Receiver XML file
            For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/Root/Receiver[ReceiverID='" & ReceiverID & "']")
                ' Extract the ReceiverID and Name values from the current node
                Dim ReceiverID2 As String
                Dim Name As String

                ReceiverID2 = xnReceiver.SelectSingleNode("ReceiverID").InnerText
                Name = xnReceiver.SelectSingleNode("Name").InnerText
                dtReceiver.Rows.Add({ReceiverID2, Name})
                MsgBox("AAA")
                MsgBox(dtReceiver.Rows.Count)
            Next

            ' Extract the Link_ID avalues from the current node
            Dim LinkID2 As String
            LinkID2 = xnLink.SelectSingleNode("Link_ID").InnerText
            dtLinkID.Rows.Add({LinkID2})
        Next

This is the best solution I have to achieve this, but I would appreciate a better approach than passing the Link_ID values to a Dropdown list in order to use them in code to delete the data.

Thanks,

Victor
Thank You!