从字符串“<fetch version='1.0' ><entity na”到类型 'Double' 的转换无效。”

Conversion from string "<fetch version='1.0' ><entity na" to type 'Double' is not valid."

我正在从临时字符串变量中的 ms crm 中读取一个字符串属性,然后将其转换为整数类型的内存代码,以便我需要将其作为整数类型传递给 xml,但这听起来很奇怪,我不是使用字符串或其两倍抛出以下异常 从字符串“<fetch version='1.0' ><entity na”到类型 'Double' 的转换无效。 请帮助

问题在于您尝试使用 + 运算符将一堆字符串与一个整数 memcod 连接在一起的方式。

此代码在运行时产生相同的异常:

Conversion from string "<foo>" to type 'Double' is not valid.

Dim n as Integer = 0
Dim test As String = "<foo>" +
        n +
        "</foo>"

显然,VB 看到那里的整数并认为您正在尝试进行算术运算。我猜它认为您想要 double 因为它无法猜测您可能还想要什么。例如,这个奇怪的代码将 test 设置为 "11"That's a string equal to "11":

    Dim n As Integer = 0
    Dim test As String = "5" +
        n +
        "6"

您可以通过两种方式解决此问题。

一个,use VB's backwards compatible dedicated string concatenation operator而不是+

    Dim test As String = "<foo>" &
        n &
        "</foo>"

两个,explicitly stringify n:

    Dim test As String = "<foo>" +
        n.ToString() +
        "</foo>"