?
Solved

Help with deleting record in xml file

Posted on 2011-10-12
12
Medium Priority
?
282 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:vcharles
  • 7
  • 4
12 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 36954957
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
0
 
LVL 3

Accepted Solution

by:
KBerger earned 2000 total points
ID: 36955124
Hi,

removing nodes by treating an xml-file like a text-file is not recommended.
Try this (C#, I hope you get it anyway)

private void ReplaceLinkNode(string linkId)
{
            XmlDocument doc = new XmlDocument();
            doc.Load(AppDomain.CurrentDomain.BaseDirectory + "links.xml");
            XmlNode node = doc.SelectSingleNode("//Link/Link_ID[text()='"+linkId+"']");
            if (node != null)
            {
                XmlNode linkNode = node.ParentNode;
                linkNode.ParentNode.RemoveChild(linkNode);
            }
            doc.Save(AppDomain.CurrentDomain.BaseDirectory + "links.xml");
}

Hope that helps,

-Kristof
0
 

Author Comment

by:vcharles
ID: 36955140
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:KBerger
ID: 36955148
Hi Victor,

see above. My first posting does just this.

Cheers,

Kristof
0
 

Author Comment

by:vcharles
ID: 36955723
Kristof,

Can you please send me the VB.NET version.

Thanks,

Victor
0
 

Author Comment

by:vcharles
ID: 36955863
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
0
 
LVL 3

Expert Comment

by:KBerger
ID: 36956229
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
0
 

Author Comment

by:vcharles
ID: 36956554
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.
0
 
LVL 3

Expert Comment

by:KBerger
ID: 36956625
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
0
 

Author Comment

by:vcharles
ID: 36956734
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
0
 

Author Comment

by:vcharles
ID: 36956975
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
0
 

Author Closing Comment

by:vcharles
ID: 36958543
Thank You!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question