Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > BOOK: Visual Basic 2005 with .NET 3.0 Programmer's Reference ISBN: 978-0-470-13705-5
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Visual Basic 2005 with .NET 3.0 Programmer's Reference ISBN: 978-0-470-13705-5
This is the forum to discuss the Wrox book Visual Basic 2005 with .NET 3.0 Programmer's Reference by Rod Stephens; ISBN: 9780470137055
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2005 with .NET 3.0 Programmer's Reference ISBN: 978-0-470-13705-5 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 5th, 2010, 02:40 AM
Registered User
Points: 79, Level: 1
Points: 79, Level: 1 Points: 79, Level: 1 Points: 79, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation how to add nodes to an existing xml file using vb.net

hi friends,
I have an xml file and i add a node to it using the following code.

LoadXmlDoc.Load("first.XML")
LoadXmlDoc_2.Load("second.XML")
Dim NodeToCreateOrReplace_2 As XmlNodeList = LoadXmlDoc_2.GetElementsByTagName("SB")
Dim NodeToCreateOrReplace As XmlNodeList = LoadXmlDoc.GetElementsByTagName("SB")
Dim newnode As XmlNode, currNode
'XmlNodeList NodeToCreateOrReplace = LoadXmlDoc.GetElementsByTagName(XmlTagNameTxtBx.Te xt);
Try
For Each selectednode As XmlNode In NodeToCreateOrReplace
Dim node As XmlNode = selectednode.SelectSingleNode("SBN")
For Each selectednode1 As XmlNode In LoadXmlDoc_2
Dim node_2 As XmlNode = selectednode.SelectSingleNode("SBN")
If node.InnerText = node_2.InnerText Then
' newnode = LoadXmlDoc.CreateElement("SB")
Dim docFrag As XmlDocumentFragment = doc.CreateDocumentFragment()
docFrag.InnerXml = selectednode1.OuterXml
currNode = LoadXmlDoc.DocumentElement("SBS").SelectSingleNode ("SB")
currNode.InsertAfter(LoadXmlDoc.ImportNode(docFrag , True), currNode.lastChild)

End If
Next
Next


im getting error "The element list has changed. The enumeration operation failed to continue."
please help.
Reply With Quote
  #2 (permalink)  
Old January 5th, 2010, 09:53 AM
Rod Stephens's Avatar
Wrox Author
Points: 2,992, Level: 22
Points: 2,992, Level: 22 Points: 2,992, Level: 22 Points: 2,992, Level: 22
Activity: 30%
Activity: 30% Activity: 30% Activity: 30%
 
Join Date: Jan 2006
Location: , , .
Posts: 599
Thanks: 1
Thanked 84 Times in 83 Posts
Default

I think that error means that you are modifying the structure over which you are looping. For Each gets confused if the list of objects it's looping over changes while it is trying to loop.

You can avoid that if you convert the For Each loop into a For i loop. Or you can make a separate collection holding the nodes over which you need to loop and then loop through that collection instead of the one that you are modifying.

What are you trying to make the code do?
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
add 5 years to date in three nodes in xml document Boris1 XSLT 4 November 25th, 2009 10:18 AM
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
XML DOM :modifying the existing nodes attributes i sharmasourabh54 XML 1 February 14th, 2005 04:04 AM
javascript - add element to existing xml? willgotoo XML 4 October 8th, 2004 09:33 AM
Add and Remove Nodes in XML document. tutul128 XML 3 March 1st, 2004 09:17 AM



All times are GMT -4. The time now is 09:34 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.