Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics 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 October 1st, 2007, 02:46 PM
Authorized User
 
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help deleting multiple XML nodes

Sorry for the bother, but I did a search of the forums and did not find an exact solution to my problem. Google wasn't much help either.

I need to loop through an XML structure and delete multiple nodes where an attribute, EnclosureID="1".

The problem with my current code is that the first element is deleted but not the rest. I am brand new to XML so I am obviously doing something wrong. I will post my code and part of the XML below.

I would really appreciate some help with this.

Partial XML:
<?xml version="1.0" encoding="utf-8"?>

<EnclosureSeriesLinks>
  <EnclosureSeries EnclosureID="1" SeriesID="1" SeriesName="0000 Frameless Series" />
  <EnclosureSeries EnclosureID="1" SeriesID="4" SeriesName="6000 Series" />
  <EnclosureSeries EnclosureID="1" SeriesID="6" SeriesName="9000 Series" />
  <EnclosureSeries EnclosureID="1" SeriesID="7" SeriesName="F000 Frameless Series" />
  <EnclosureSeries EnclosureID="1" SeriesID="10" SeriesName="P000 Provincial Series" />
  <EnclosureSeries EnclosureID="1" SeriesID="11" SeriesName="Q000 Frameless Series" />
  <EnclosureSeries EnclosureID="1" SeriesID="12" SeriesName="Steam Series" />
  <EnclosureSeries EnclosureID="2" SeriesID="1" SeriesName="0000 Frameless Series" />
  <EnclosureSeries EnclosureID="2" SeriesID="4" SeriesName="6000 Series" />
</EnclosureSeriesLinks>


Current Code:
Dim EnclosureSeries As XmlDocument = New XmlDocument
            Dim esindex As Integer = 0
            EnclosureSeries.Load("c:\csd2007data\enclosureseri es.xml")
            For Each myNode As XmlNode In EnclosureSeries.DocumentElement.ChildNodes
                If myNode.Attributes("EnclosureID").Value.ToString = sdeleted Then
                    myNode.ParentNode.RemoveChild(EnclosureSeries.Docu mentElement.ChildNodes(esindex))
                    'MsgBox(myNode.Attributes("EnclosureID").Value.ToS tring)
                End If
                esindex += 1
            Next
            EnclosureSeries.Save("c:\csd2007data\enclosureseri es.xml")



Only the very first record is deleted and I want to delete all items where EnclosureID="1".

The resulting XML is
<EnclosureSeries EnclosureID="1" SeriesID="4" SeriesName="6000 Series" />
  <EnclosureSeries EnclosureID="1" SeriesID="6" SeriesName="9000 Series" />
  <EnclosureSeries EnclosureID="1" SeriesID="7" SeriesName="F000 Frameless Series" />
  <EnclosureSeries EnclosureID="1" SeriesID="10" SeriesName="P000 Provincial Series" />
  <EnclosureSeries EnclosureID="1" SeriesID="11" SeriesName="Q000 Frameless Series" />
  <EnclosureSeries EnclosureID="1" SeriesID="12" SeriesName="Steam Series" />
  <EnclosureSeries EnclosureID="2" SeriesID="1" SeriesName="0000 Frameless Series" />
  <EnclosureSeries EnclosureID="2" SeriesID="4" SeriesName="6000 Series" />
</EnclosureSeriesLinks>

 
Old October 1st, 2007, 04:45 PM
Authorized User
 
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nevermind I figured it out. The code snippet is below, in case anyone else needs it:

Dim doc As XmlDocument = New XmlDocument
        doc.Load("c:\csd2007data\enclosureseries.xml")
        Dim nodelist As XmlNodeList
        Dim root As XmlNode = doc.DocumentElement
        nodelist = root.SelectNodes("//EnclosureSeries[@EnclosureID=1]")
        For Each es As XmlNode In nodelist
            es.ParentNode.RemoveChild(es)
        Next
        doc.Save("c:\csd2007data\enclosureseries.xml")






Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting multiple rows in data view or grid deepakbachelor SQL Server 2005 1 December 7th, 2007 02:19 PM
TreeView deleting Nodes Apocolypse2005 Beginning VB 6 3 July 11th, 2007 04:16 AM
*solved* applying same template to multiple nodes Brian Campbell XSLT 3 October 13th, 2006 06:56 PM
Deleting multiple records from the same table jscrogg Beginning VB 6 3 June 12th, 2006 05:30 AM
Display nodes from xml nambati XSLT 2 September 17th, 2004 07:57 AM





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