Returning line from XML file...

lidds

Well-known member
Joined
Oct 19, 2004
Messages
122
Programming Experience
Beginner
I have a file called setting.xml which contains the following:

VB.NET:
<?xml version="1.0" encoding="utf-8"?>
<settings 

licenseServiceType="1"

protectedStorageFile="%AppDomainAppPath%App_Data\storage.psf"
addressSigned="tgAAAIv1/CTaEM0BkLI9HW0ozQEZAGh0dHA6Ly8xMC4xMjguNTQuNDk6ODA5MS96FnzeHrzBS3YydgZ/nSOa8zEGLKNhas0PomOdV/JBhBP9HLq09G6MUb6uwoJ4ogQ="  
customerDeploymentLicenseCode="dgAAAJEL0iTaEM0BkLI9HW00"

>

</settings>

What I am trying to do is return the information for the "customerDeploymentLicenseCode" line e.g. dgAAAJEL0iTaEM0BkLI9HW00

I have done the following code, but for some reason it is not matching/finding that line and therefore returns nothing. I can't seem to figure out why not, is it possible for someon to point out my mistake?

VB.NET:
    Public Function GetServerLicenseCode() As String 
	Dim settingsFilePath As String = GetRealFilePath("%AppDomainAppPath%App_Data\settings.xml")
        Dim objFileName As FileStream = New FileStream(settingsFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim objFileRead As StreamReader = New StreamReader(objFileName)
        Dim customerLicCode As String = ""
        Do While (objFileRead.Peek() > -1)
            If objFileRead.ReadLine().IndexOf("customerDeploymentLicenseCode") <> -1 Then
                '    customerLicCode = objFileRead.ReadLine().Replace("customerDeploymentLicenseCode=", "")
                customerLicCode = objFileRead.ReadLine()
                Exit Do
            End If
        Loop

        Return customerLicCode
    End Function

Thanks

Simon
 
ReadLine reads a line and returns it, you have already read that line, so next call reads next line. Read the line into a variable, check the content and handle that line.

Maybe also it would be a good idea to handle Xml data using some of the .Net Xml tools? It would be more consistent and less error prone.
Not sure how accurate your .Net 2 profile is, but in .Net 4 I would use this code if that was the only value I wanted:
Dim licenseCode= XDocument.Load(settingsFilePath).<settings>.@customerDeploymentLicenseCode
 
Back
Top