Victor Charles
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.Map Path("~/Ap p_Data/Lin k.xml"))
Dim cnt As Integer = LinkStream.SelectNodes("Ro ot/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
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.Map
Dim cnt As Integer = LinkStream.SelectNodes("Ro
'txtTotalRecords.Text = cnt
' btnNext.PerformClick()
MsgBox("Record was deleted")
End If
If an = MsgBoxResult.No Then
MsgBox("Record was not deleted")
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
Hi Victor,
see above. My first posting does just this.
Cheers,
Kristof
see above. My first posting does just this.
Cheers,
Kristof
ASKER
Kristof,
Can you please send me the VB.NET version.
Thanks,
Victor
Can you please send me the VB.NET version.
Thanks,
Victor
ASKER
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("//Li nk/Link_ID [text()='" & x & "' and ReceiverID[text()='" & y & "']")
If node IsNot Nothing Then
Dim linkNode As XmlNode = node.ParentNode
linkNode.ParentNode.Remove Child(link Node)
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("//Li nk/Link_ID [text()='" & x & "' and ReceiverID[text()='" & y & "']")
How do I fix this error?
Thanks,
Victor
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("~
Dim node As XmlNode = doc.SelectSingleNode("//Li
If node IsNot Nothing Then
Dim linkNode As XmlNode = node.ParentNode
linkNode.ParentNode.Remove
End If
doc.Save(Server.MapPath("~
I receive the following error:
'//Link/Link_ID[text()='3'
On Line:
Dim node As XmlNode = doc.SelectSingleNode("//Li
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.Current Domain.Bas eDirectory + "links.xml");
XmlNode node = doc.SelectSingleNode("//Li nk[@id='3' and @receiverid='2']");
if (node != null)
{
node.ParentNode.RemoveChil d(node);
}
doc.Save(AppDomain.Current Domain.Bas eDirectory + "links.xml");
Cheers,
Kristof
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.Current
XmlNode node = doc.SelectSingleNode("//Li
if (node != null)
{
node.ParentNode.RemoveChil
}
doc.Save(AppDomain.Current
Cheers,
Kristof
ASKER
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.MapPat h("~/App_D ata/Donor. xml"))
xdLink.Load(Server.MapPath ("~/App_Da ta/Link.xm l"))
xdReceiver.Load(Server.Map Path("~/Ap p_Data/Rec eiver.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[Recei verID='" & C1Country.SelectedItem.Val ue & "']")
' 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("L ink_ID").I nnerText
ReceiverID = xnLink.SelectSingleNode("R eceiverID" ).InnerTex t
DonorID = xnLink.SelectSingleNode("D onorID").I nnerText
' Perform another similar search based upon the Donor XML file
For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root /Donor[Don orID='" & 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").Inn erText
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("/R oot/Receiv er[Receive rID='" & ReceiverID & "']")
' Extract the ReceiverID and Name values from the current node
Dim ReceiverID2 As String
Dim Name As String
ReceiverID2 = xnReceiver.SelectSingleNod e("Receive rID").Inne rText
Name = xnReceiver.SelectSingleNod e("Name"). InnerText
dtReceiver.Rows.Add({Recei verID2, 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[R eceiverID= '" & ReceiverID & "']")
' Extract the ReceiverID and Name values from the current node
Dim LinkID2 As String
LinkID2 = xnLink_ID.SelectSingleNode ("Link_ID" ).InnerTex t
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.DataTextFiel d = "Name"
DropDownList1.DataValueFie ld = "DonorID"
DropDownList1.DataBind()
DropDownList2.DataSource = dtLinkID
DropDownList2.DataTextFiel d = "Link_ID"
DropDownList2.DataValueFie ld = "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("//Li nk/Link_ID [text()='" & DropDownList2.Text & "']")
If node IsNot Nothing Then
Dim linkNode As XmlNode = node.ParentNode
linkNode.ParentNode.Remove Child(link Node)
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.
<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.MapPat
xdLink.Load(Server.MapPath
xdReceiver.Load(Server.Map
' 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/
' 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("L
ReceiverID = xnLink.SelectSingleNode("R
DonorID = xnLink.SelectSingleNode("D
' Perform another similar search based upon the Donor XML file
For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root
' Extract the DonorID and Name values from the current node
Dim DonorID2 As String
Dim Name As String
DonorID2 = xnDonor.SelectSingleNode("
Name = xnDonor.SelectSingleNode("
dtDonor.Rows.Add({DonorID2
Next
' Perform yet another practically identical search on the Receiver XML file
For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/R
' Extract the ReceiverID and Name values from the current node
Dim ReceiverID2 As String
Dim Name As String
ReceiverID2 = xnReceiver.SelectSingleNod
Name = xnReceiver.SelectSingleNod
dtReceiver.Rows.Add({Recei
Next
' Perform yet another practically identical search on the Receiver XML file
For Each xnLink_ID As Xml.XmlNode In xdLink.SelectNodes("/Root/
' Extract the ReceiverID and Name values from the current node
Dim LinkID2 As String
LinkID2 = xnLink_ID.SelectSingleNode
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.DataTextFiel
DropDownList1.DataValueFie
DropDownList1.DataBind()
DropDownList2.DataSource = dtLinkID
DropDownList2.DataTextFiel
DropDownList2.DataValueFie
DropDownList2.DataBind()
My code than to delete the reord would be:
MsgBox(DropDownList2.Text)
Dim doc As New XmlDocument()
doc.Load(Server.MapPath("~
Dim node As XmlNode = doc.SelectSingleNode("//Li
If node IsNot Nothing Then
Dim linkNode As XmlNode = node.ParentNode
linkNode.ParentNode.Remove
End If
doc.Save(Server.MapPath("~
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("//Li nk/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.SelectedValu e instead of DropDownList2.Text
Regards,
Kristof
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("//Li
If I understand correctly, DropDownList2.Text is null.
Well. Is it configured to be a DropDownList or a ComboBox?
Try using DropDownList2.SelectedValu
Regards,
Kristof
ASKER
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[R eceiverID= '" & ReceiverID & "']")
' Extract Link_ID and Name from the current node
Dim LinkID2 As String
LinkID2 = xnLink_ID.SelectSingleNode ("Link_ID" ).InnerTex t
dtLinkID.Rows.Add({LinkID2 })
Next
DropDownList2.DataSource = dtLinkID
DropDownList2.DataTextFiel d = "Link_ID"
DropDownList2.DataValueFie ld = "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
' Perform yet another practically identical search on the Link XML file
For Each xnLink_ID As Xml.XmlNode In xdLink.SelectNodes("/Root/
' Extract Link_ID and Name from the current node
Dim LinkID2 As String
LinkID2 = xnLink_ID.SelectSingleNode
dtLinkID.Rows.Add({LinkID2
Next
DropDownList2.DataSource = dtLinkID
DropDownList2.DataTextFiel
DropDownList2.DataValueFie
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
ASKER
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[Recei verID='" & C1Country.SelectedItem.Val ue & "']")
' 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("L ink_ID").I nnerText
ReceiverID = xnLink.SelectSingleNode("R eceiverID" ).InnerTex t
DonorID = xnLink.SelectSingleNode("D onorID").I nnerText
' Perform another similar search based upon the Donor XML file
For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root /Donor[Don orID='" & 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").Inn erText
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("/R oot/Receiv er[Receive rID='" & ReceiverID & "']")
' Extract the ReceiverID and Name values from the current node
Dim ReceiverID2 As String
Dim Name As String
ReceiverID2 = xnReceiver.SelectSingleNod e("Receive rID").Inne rText
Name = xnReceiver.SelectSingleNod e("Name"). InnerText
dtReceiver.Rows.Add({Recei verID2, Name})
MsgBox("AAA")
MsgBox(dtReceiver.Rows.Cou nt)
Next
' Extract the Link_ID avalues from the current node
Dim LinkID2 As String
LinkID2 = xnLink.SelectSingleNode("L ink_ID").I nnerText
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
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/
' 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("L
ReceiverID = xnLink.SelectSingleNode("R
DonorID = xnLink.SelectSingleNode("D
' Perform another similar search based upon the Donor XML file
For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root
' Extract the DonorID and Name values from the current node
Dim DonorID2 As String
Dim Name As String
DonorID2 = xnDonor.SelectSingleNode("
Name = xnDonor.SelectSingleNode("
dtDonor.Rows.Add({DonorID2
Next
' Perform yet another practically identical search on the Receiver XML file
For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/R
' Extract the ReceiverID and Name values from the current node
Dim ReceiverID2 As String
Dim Name As String
ReceiverID2 = xnReceiver.SelectSingleNod
Name = xnReceiver.SelectSingleNod
dtReceiver.Rows.Add({Recei
MsgBox("AAA")
MsgBox(dtReceiver.Rows.Cou
Next
' Extract the Link_ID avalues from the current node
Dim LinkID2 As String
LinkID2 = xnLink.SelectSingleNode("L
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
ASKER
Thank You!
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