Results 1 to 3 of 3

Thread: Returning line from XML file...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Returning line from XML file...

    I have a file called setting.xml which contains the following:

    Code:
    <?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?

    Code:
        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

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Returning line from XML file...

    Using VS2010 (if a lower version add line continuations)

    Code:
    Private Sub ReadData()
        Dim Value =
            (
                From T In XDocument.Load("Your File Name goes here")...<settings>
                Select T.@customerDeploymentLicenseCode
            ).FirstOrDefault
    
        If Value IsNot Nothing Then
            MessageBox.Show(Value)
        End If
    
    End Sub

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Returning line from XML file...

    kevininstructor's solution will give you the value for the customerDeploymentLicenseCode attribute you're looking for.

    -----

    The reason your code is having issues is that you're calling ReadLine twice. You call it in your if statement and then once you've determined you're on the correct line you call ReadLine again which from the looks of your sample returns an empty string.

    There's another issue with your code in that if you were to set customerLicCode to the whole line you'd end up with customerDeploymentLicenseCode="dgAAAJEL0iTaEM0BkLI9HW00" in the variable and not the the value you're looking for. You'd need to split the string and get just the value.

    All that being said it's an XML file and you should use the tools you have available rather than trying to reinvent the wheel to parse your file.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width