Click here to Skip to main content
12,753,788 members (37,098 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
I have an xml file that hold data that some times needs to be updated, below is an example of my xml file. I am trying to make it so i can edit any of the 4 items just in case i make a mistake in spelling or a location changes. Below is the function i created and it will find the name but once it hits PATH,ARG, or ICON i get the error :
Object reference not set to an instance of an object.

If someone could help me that would be great.

<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
    <ARG> /k cd "C:\"</ARG>

Function EditXML(ByVal sTool As String, ByVal sDisplayName As String, ByVal sLocation As String, ByVal sArg As String, ByVal sIcon As String)
    ' Load the XmlDocument.
    Dim xd As New XmlDocument()

    Dim nod As XmlNode = xd.SelectSingleNode("/AdminTools/App/Name[text()='" & sTool & "']")
    If nod IsNot Nothing Then
        nod.ChildNodes(0).InnerText = sDisplayName
        nod.ChildNodes(1).InnerText = sLocation
        nod.ChildNodes(2).InnerText = sArg
        nod.ChildNodes(3).InnerText = sIcon
        Return True
        Exit Function
        Return False
        Exit Function
    End If
End Function
Posted 19-Nov-12 5:39am
Member 7681169 19-Nov-12 11:01am
i guess, you're digging too deep.
Dim nod As XmlNode = xd.SelectSingleNode("/AdminTools/App")

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is how I do things like this:

Dim xd As New XmlDocument()

If xd.DocumentElement IsNot Nothing Then
    For Each Elem As XmlElement In xd.DocumentElement.ChildNodes
        If Elem.Name = "App" Then
            For Each E As XmlElement In Elem.ChildNodes
                Select Case E.Name
                    Case "Name"
                        E.Value= sDisplayName
                End Select
            Exit For
        End If
End If

Open the document. If it has a document element (AdminTools, in your example), cycle through its child nodes until you find the one named App. Then cycle through its child nodes and assign values based on the name of the element. After you have handled App, exit the outer loop.

This is a bit wordy, but has the advantage of bypassing all primary nodes not named App, including any comment nodes you may want to add. It also does not depend on Name and the other child nodes being in a particular order.

One other issue that might cause problems is that XML is VERY picky about special characters. My rule is that if a node might have anything other than strictly alphanumeric characters, the data gets wrapped with CDATA. It is possible that the special characters could be causing problems, which is why I generally use Value rather than InnerText: with Value, the Framework's inner workings will determine if CDATA is needed or not and do the translation automatically.

Added: This article[^] does a good job of explaining the difference between Value and InnerText, in case you are interested.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170217.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100