Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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.-->
<AdminTools>
  <App>
    <Name>CMD</Name>
    <Path>C:\Windows\System32\cmd.exe</Path>
    <ARG> /k cd "C:\"</ARG>
    <Icon>50</Icon>
  </App&gt
</AdminTools>
 

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 19-Nov-12 5:39am
Comments
Member 7681169 at 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

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

Solution 1

This is how I do things like this:
 
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.
  Permalink  
v3

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
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