gpt4 book ai didi

xml - 错误的重载用于 XmlDocument.Load

转载 作者:数据小太阳 更新时间:2023-10-29 02:03:31 27 4
gpt4 key购买 nike

从 DLL 我得到这个 XML 字符串(不是文件):

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

我想要的是RequestIDOverallResult的值。

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

我试过了,但我收到一些字符不正确的错误。

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

我在 doc.Load(sr) 行出现以下错误

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

出于某种原因,VB 似乎选择了 .Load(String) 而不是 .Load(Stream)

有人能看出问题吗?

最佳答案

发生的事情是没有使用 Option Strict On编译器似乎选择了 wrong overload XmlDocument.Load 方法,以便它尝试读取文件。

启用 Option Strict On 以选择 correct overload从流中读取。

(将 Option Strict On 作为新项目的默认设置是个好主意 - Can I set Option Explicit and Option Strict on a Project/Solution level? 中有更多相关信息)


顺便说一句,作为用户Slai在其他地方有用地指出,VB.Net XML Axis Properties 有一些漂亮的功能您可以利用它来减少代码量并提高可读性:

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

为避免评论中的信息丢失,用户 TnTinMn告诉我们,早期的 VS2015 版本充满了奇怪的错误,所以,如果这是用来编译代码的,那可能就是它出错的原因。

关于xml - 错误的重载用于 XmlDocument.Load,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50543686/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com