Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
<AdminTools>
  <App>
    <Name>CMD</Name>
    <Path>C:\Windows\System32\cmd.exe</Path>
    <ARG> /k cd "C:\"</ARG>
    <Icon>50</Icon>
  </App>
</AdminTools>



VB
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()
    xd.Load(SynLaunchXML)


    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
        xd.Save(SynLaunchXML)
        Return True
        Exit Function
    Else
        Return False
        Exit Function
    End If
End Function
Posted
Comments
Love 2 code 19-Nov-12 11:01am    
Hi,
i guess, you're digging too deep.
Try
Dim nod As XmlNode = xd.SelectSingleNode("/AdminTools/App")
Greets
T.

1 solution

This is how I do things like this:

VB
Dim xd As New XmlDocument()
xd.Load(SynLaunchXML)

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
            Next
            Exit For
        End If
    Next
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.
 
Share this answer
 
v3

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