|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > How do I delete an element? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|