Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Okay so my other question of the day is... how can i read the ID number in xml based on the name field i have. I was just told at work that i need to add id tags which is fine but i m having the hardest time getting the id number. this xml file we have just holds paths to specific locations and when i need to delete one they want to use the id tag to delete the whole thing which is fine. The code below is what i m using to try and read it but i get the error "
Object reference not set to an instance of an object.
All i need is to get the number.

Here is my code:
VB
Dim Rinfo = LVapps.SelectedItems(0).Text

xmlDoc.Load(Application.StartupPath & "\Tools.xml")
Dim xmlContactNodeList As XmlNodeList = xmlDoc.GetElementsByTagName("App")
For i = 0 To xmlContactNodeList.Count - 1
    If xmlContactNodeList.Item(i).Item("Name").InnerText = Rinfo Then
        MsgBox(xmlContactNodeList.Item(i).Item("id").InnerText)
    End If
Next



Here is what the xml group looks like:

XML
- <AdminTools>
- <App id="1">
  <Name>Test2</Name>
  <Path>C:\Users\zachary.shupp\Documents\Tools\Zack's MMC.msc</Path>
  <Icon>1</Icon>
  </App>
  </AdminTools>
Posted

Your
VB
MsgBox(xmlContactNodeList.Item(i).Item("id").InnerText)

There is no "id" node under the Name node so that is where the error is coming from. If you change it to
VB
MsgBox(xmlContactNodeList.Item(i).Item("Path").InnerText)


You will get the messagbox with the Path information.

Edit
But then I reread the question and realized that you want to get the id. The id is an Attribute.

VB
MsgBox(xmlContactNodeList.Item(1).Attributes(0).OuterXml)

shows a message box with id="2"
 
Share this answer
 
v2
Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null/nothing. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property.
For now, either xmlContactNodeList.Item(i).Item("Name") OR xmlContactNodeList.Item(i).Item("id") looks Nothing


Moving to other part, one of the possible way to find the value you are trying to access is using XPath, try:
VB
Dim AdminToolsList As XmlNodeList = ProductXml.SelectNodes("/AdminTools/App/@id")
For Each ToolNode As XmlNode In AdminToolsList 
    MsgBox(ToolNode.InnerText)
Next
 
Share this answer
 
You could try either of these (remember id is an attribute not an element so it is accessed differently)

(Code is untested but should work as is)

VB
Dim appNode As XmlNode = xmlDoc.SelectSingleNode("/AdminTools/App[@id=""" & Identifier & """]")
If Not IsNothing(appNode) Then
'process your node however you wish
End If



Or if you have more than one app node with the same id...

VB
For Each node As XmlNode In xmlDoc.ChildNodes
           If node.HasChildNodes Then
              'Name element is a child of App element
               For Each cnode As XmlNode In node.ChildNodes
                   If cnode.Name = "Name" AndAlso cnode.InnerText = Rinfo Then
                       If node.Attributes.Count > 0 Then
                           For Each attrib As XmlAttribute In node.Attributes
                               If attrib.Name = "id" AndAlso attrib.Value = Identifier Then
                                   'node is the one we want
                               End If
                           Next 'attribute in App node
                       End If
                   End If
               Next 'child element in App node
           End If
       Next 'element in root
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900