Trying to Read XML File from String

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
167
Programming Experience
Beginner
Hello everyone..I hope I can get some help on this. Right now I am sending an API request which has a few strings in it..The request comes back no problem, but it's in XML format.

What I am trying to do is take a part of that response and display it into a log text box. Here is some of my code:

VB.NET:
Dim responsefromserver As String = reader.ReadToEnd 'is the response coming back from the server

Dim doc As XDocument = XDocument.Load(responsefromserver)
Dim result As String
For Each reply As XElement In doc.<information>
    result = reply.<detail>.Value
Next

txtLog.AppendText(name & result & Now & vbCrLf) 'Log file displaying the name and then the result string

This is only a small peice of it but pretty much everything being used for trying to read the xml and then display it in the textbox..I am only getting a squiggly line error on the last line. That error says "Variable 'result' is used before it has been assigned a value. A null reference exception could result at runtime."

Any idea what I am doing wrong here? Any idea where I could find some good examples for loading an XML file from a string (responsefromserver) and then displaying only a certain chunk of it into a textbox?

Thanks in advance!
 
Hi,

The reason for the error on the Result variable is due to the fact that you assign a value to the variable within the For loop and yet the variable is declared outside of the For loop. Due to this the complier interprets that the contents within the for loop may never be executed and so displays the error as a warning when coming to display is contents. The easy way to resolve this is to declare the variable with a default value, being:-

Dim Result As String = String.Empty

In addition, here is an example of how you should be able to read and manipulate the XML file being received from your API:-

VB.NET:
Imports System.IO
Imports System.Xml
 
Public Class Form1
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'you will not need this const in your routine since this is accommodated
    'for by your API
    Const xmlFileName As String = "d:\temp\XMLFile1.xml"
 
    Dim xmlDoc As New XmlDocument()
    'here you would NOT define a stream reader since this is coming from your API
    Dim strmReader As New StreamReader(xmlFileName, System.Text.Encoding.UTF8)
    'Here you would use:-
    'Dim xmlReader As XmlTextReader = New XmlTextReader(Reader)
    Dim xmlReader As XmlTextReader = New XmlTextReader(strmReader)
    Dim Result As String = String.Empty
 
    xmlReader.WhitespaceHandling = WhitespaceHandling.None
    xmlDoc.Load(xmlReader)
 
    'you can select specific nodes to be displayed using SelectNodes
    For Each myNode As XmlNode In xmlDoc.SelectNodes("//sets/set")
      Result += myNode.InnerText & vbCrLf
    Next
    'you can select specific tag elements to be displayed using GetElementsByTagName
    For Each Reply As XmlElement In xmlDoc.GetElementsByTagName("servicename")
      Result += Reply.InnerText & vbCrLf
    Next
    txtLog.AppendText(Name & vbCrLf & Result & Now.ToLongDateString & vbCrLf) 'Log file displaying the name and then the result string
  End Sub
End Class

Hope that helps.

Cheers,

Ian
 
Thanks Ian! This helped me out greatly and I got it working now.

One other question..When coming up in the error log it seams to be adding a line break at the end..I'm using this code
VB.NET:
txtLog.AppendText(name & "  " & Result & "  " & Now & vbCrLf)

It displays the name fine..gets the Result from what you had and displays that fine..but then drops a line and displays the time (Now)..Any idea how I can remove the break so that the time displays on the same line? I guess that is being added in with the XML code?

Cheers
 
Last edited:
Hi,

It sounds like you still have the Carriage Return character at the end of the Result variable. i.e:-

Result += Reply.InnerText & vbCrLf

Just remove the vbCrLf character and all shound be fine.

Cheers,

Ian
 
Back
Top