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
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
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.