Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 7th, 2010, 02:45 PM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Unhappy How to delete node that matches combobox?

I'm programming in VB, using Visual Studio 2008.
This is a Windows Forms program.

I know I've been asking a lot of questions lately, but I've spent hours experimenting and pouring through the msdn.microsoft help documentation. I don't know why, but it's extremely difficult for me to find answers to seemingly rudimentary questions about xml. If anyone can point me to better resources for xml in VB, that'd be great.

GOAL: I want to delete the <Food> with the <Name> that matches the contents of ComboBox1 when I click Button1.

Objects I'm using:
  • ComboBox1
  • Button1

Here is my xml file (Foods.xml):
Code:
<?xml version="1.0" encoding="utf-8"?>
<Foods>
  <Food>
    <Name>BigMac (McDonald's)</Name>
    <Calories>485</Calories>
    <Fat>21.5</Fat>
  </Food>
  <Food>
    <Name>Burger and fries</Name>
    <Calories>90</Calories>
    <Fat>43</Fat>
  </Food>
</Foods>
Code I've tried (this is a horrible mess!):
Code:
'Dim Food As XElement = XElement.Load("Foods.xml")
        'Food.Descendants("Burger and fries").First().Remove()

        'Dim Match As XNode

        'XElement.Load("Foods.xml").<Food>.First(Function(f) f.<Name>.Value = cmbFoods.Text).Remove()
        'Dim Food As XElement = XElement.Load("Foods.xml").<Food>.FirstOrDefault(Function(f) f.<Name>.Value = cmbFoods.Text)
        'Dim Food As XElement = XElement.Load("Foods.xml")
        'Dim SelectedFood = cmbFoods.Text
        'Food.Remove()
        'Food.Descendants(SelectedFood).First().Remove()
I'm apparently no good at figuring out xml in VB. Please help!

UPDATE

Here is my latest failed attempt:
Code:
Dim Food As XDocument = XDocument.Load("Foods.xml")
        Food.Descendants().Where(Function(f) f.<Name>.Value = cmbFoods.Text).Remove()

Last edited by QuadFather; April 7th, 2010 at 04:31 PM.. Reason: tried some other things, still didn't work
 
Old April 8th, 2010, 03:20 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Some or all of the above may work, but they are only editing the in memory storage. You then need to call XElement.Save to save the changes to file.

So in the last example, simply put the following after them:

Code:
Food.Save("Foods.xml")
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
QuadFather (April 8th, 2010)
 
Old April 8th, 2010, 08:40 AM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Default

I feel silly.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I delete all elements of a combobox blackheat1548 VB How-To 1 February 13th, 2008 03:49 PM
How to delete a xml node with all attributes and s vishnu108mishra XML 2 November 14th, 2007 04:19 AM
Select first child node that matches criteria priya81 XSLT 4 July 3rd, 2007 10:32 AM
Select node whose child value matches Fantmx XSLT 5 February 16th, 2007 04:54 PM
How to delete an element and parent node. crossedge XSLT 1 March 14th, 2006 05:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.