4

From a DLL I'm getting this XML string (not file):

<?xml version="1.0" encoding="UTF-8"?>
<SerRes RequestID="1" RequestType="GetStatus" OverallResult="ConnectionError">
</SerRes>

what I want to have is the values of RequestID, OverallResult.

dim ID as string = ... (will be 1)
dim Result as string = ...    (will be ConnectionError)

I've tried this, but I'm getting an error that some characters aren't correct.

Dim Result as string
Dim ID as string
Dim sr As New System.IO.StringReader(XMLString)
Dim doc As New Xml.XmlDocument
doc.Load(sr)
Dim reader As New Xml.XmlNodeReader(doc)
While reader.Read()
  Select Case reader.NodeType
    Case Xml.XmlNodeType.Element
    If reader.Name = "SerRes" Then
      Result = reader.GetAttribute("OverallResult")
      ID = reader.GetAttribute("RequestID")
    End If
  End Select
End While

I get the following error on the line doc.Load(sr)

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Illegal characters in path.

It looks like VB chooses .Load(String) instead of .Load(Stream) for some reason.

Can someone see the problem?

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Thomas Dutoit
  • 61
  • 1
  • 9
  • I can't reproduce the error. Please would you make sure that you are using [`Option Strict On`](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-strict-statement) so that all variable types are correct and Visual Studio has picked the correct overload of the Load method. – Andrew Morton May 26 '18 at 15:43
  • 1
    "Illegal characters in path" is an error you'd get with XmlDocument.Load(filename). As you have the XML as a string, you could use the [XmlDocument.LoadXml(String) Method](https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldocument.loadxml?view=netframework-4.7.2). – Andrew Morton May 26 '18 at 17:10
  • 1
    Yes, strict on did the trick. Thanks a lot andrew. – Thomas Dutoit May 26 '18 at 17:39
  • Thanks for the feedback :) I'm just [asking elsewhere](https://meta.stackoverflow.com/q/368669/1115360) about the best way for us to conclude the question - because it was solved by a setting in Visual Studio, it ought to be closed with a reason of "Problem can no longer be reproduced" but I would not want any penalty on you for that, so I may yet write up what was needed in an answer instead. Please check back in an hour or so if you can and see what should be done. Just to keep everything tidy on this site (not that it's that tidy really, lol). – Andrew Morton May 26 '18 at 18:09
  • 2
    @AndrewMorton Perfect! Now the question doesn't need to be closed, an answer could be accepted instead, and this is a perfectly findable and useful question. – ivan_pozdeev May 26 '18 at 19:22
  • 1
    What version of the compiler are you using. Is it by chance one of the early VS2015 versions? I believe the last version in the 2015 series was update 3. The early releases were full of weird bugs. Without a context, this is of little value to the community as it can not be reproduced. – TnTinMn May 27 '18 at 01:49

1 Answers1

6

What has happened is that without the use of Option Strict On the compiler appears to have chosen the wrong overload of the XmlDocument.Load method such that it is trying to read a file.

Enable Option Strict On for it to pick the correct overload to read from a stream.

(It's a Good Idea to have Option Strict On as the default for new projects - there's more pertinent information at Can I set Option Explicit and Option Strict on a Project/Solution level?)


Incidentally, as user Slai helpfully pointed out elsewhere, there are some nifty features of VB.Net XML Axis Properties which you could make use of to reduce the amount of code and improve readability:

Dim X = XElement.Parse(XMLString)
Dim ID$ = X.@RequestID
Dim Result$ = X.@OverallResult

To avoid the information from a comment being lost, user TnTinMn informed us that early VS2015 versions were full of weird bugs, so, if that is what was being used to compile the code, that may be why it got it so wrong.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84