XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 11th, 2004, 04:55 PM
troy23 troy23 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 7 troy23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How do I delete an element?

I am trying to delete an element and it's children based on an ID that is passed in. I am getting an error. Can someone tell me what is wrong here? Thanks

<Root>
<Element>
<ID>1</ID>
<Name>Personnel</Name>
</Element>
<Element>
<ID>2</ID>
<Name>Sales xxx</Name>
</Element>
<Element>
<ID>3</ID>
<Name>Warehouse z</Name>
</Element>
</Root>

ElementID = Me.txtID.Text

Dim doc = New XmlDocument

doc.Load(Server.MapPath(viewstate("xml_path")))

Dim targetNode As XmlNode = doc.SelectSingleNode("/Element")
Dim nodeList As XmlNodeList = targetNode.SelectNodes(("[@ID=""" + ElementID.ToString() + """]"))

Dim node As XmlNode
For Each node In nodeList
targetNode.RemoveChild(node)
Next
doc.Save(viewstate("xml_path"))


I get an error here:

Dim nodeList As XmlNodeList = targetNode.SelectNodes(("[@ID=""" + ElementID.ToString() + """]"))
Object reference not set to an instance of an object.

Reply With Quote
  #2  
Old January 12th, 2004, 01:24 PM
imbrokn imbrokn is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: NJ
Posts: 428 imbrokn User rank is Corporal (100 - 500 Reputation Level)imbrokn User rank is Corporal (100 - 500 Reputation Level)imbrokn User rank is Corporal (100 - 500 Reputation Level)imbrokn User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 11 h 34 m 8 sec
Reputation Power: 10
Send a message via AIM to imbrokn
this statement:
Dim targetNode As XmlNode = doc.SelectSingleNode("/Element")
is going to select the first element node. Now the target node would be this:
Code:
<Element> 
<ID>1</ID> 
<Name>Personnel</Name> 
</Element> 

After that it looks like you are trying to grab the attribute ID. (that's what @ID is) but you dont' have any attributes with that name. What you should do is just select the elementNode that has the childNode ID = whatever id your searching. Which would be this

Code:
 
' after you load your XML doc into the object doc
set xmlRoot = doc.documentElement
' this is the <root> node
set delNode = xmlRoot.selectSingleNode('Element/[ID = '" & elementID.ToString() &"'")
' that should be the node to delete

xmlRoot.removeChild(delNode)


Hope that helps.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > How do I delete an element?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway