Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的演员阵容无效?_C#_Xml - Fatal编程技术网

C# 为什么我的演员阵容无效?

C# 为什么我的演员阵容无效?,c#,xml,C#,Xml,我使用一个例程从Xml文件中读取数据,如下所示: <VerificationSample X1 = "1" X3 = "3" ../> 但是,某些属性可能不存在,因此我在该属性读取器函数定义中使用以下例程来处理它: public static object GetAttributeSafe(XmlReader reader, string attributeName, Type objectType) { // .. string value = reader.Ge

我使用一个例程从Xml文件中读取数据,如下所示:

<VerificationSample X1 = "1" X3 = "3" ../>
但是,某些属性可能不存在,因此我在该属性读取器函数定义中使用以下例程来处理它:

public static object GetAttributeSafe(XmlReader reader, string attributeName, Type objectType)
{
    // ..
    string value = reader.GetAttribute(attributeName);

    if (value != null) // attribute exists
    {
        if (objectType != typeof (string))
        {
            var converter = TypeDescriptor.GetConverter(objectType);
            returnValue = converter.ConvertFrom(value);
        }

        else // is already a string and doesn't need to be converted
        {
            return value;
        }
    }
    else // attribute doesn't exist
    {
        return "0";
    }
}

如果属性不存在,应用程序将弹出一个错误:

指定的强制转换无效


我的错误是什么?

如果属性不存在,则返回“0”,然后尝试将其转换为双精度。您需要返回
objectType
的默认值,可能需要执行以下操作

else // attribute doesn't exist
{
    if(objectType.IsValueType)
    {
        return Activator.CreateInstance(objectType);
    }
    return null;
}

如果您试图转换为非不可变的类而不是int或字符串,则只会返回null,这将破坏您的函数;它就在那里,所以所有代码路径都会返回一些东西。

如果该属性不存在,则返回“0”,然后尝试将其转换为双精度。您需要返回
objectType
的默认值,可能需要执行以下操作

else // attribute doesn't exist
{
    if(objectType.IsValueType)
    {
        return Activator.CreateInstance(objectType);
    }
    return null;
}

如果您试图转换为非不可变的类而不是int或字符串,则只会返回null,这将破坏您的函数;它就在那里,所以所有的代码路径都会返回一些东西。

因为您返回的是“0”,这是一个字符串,您不能将其转换为double。改为使用Convert.toDouble(字符串s)。

因为您返回的是字符串“0”,不能将其转换为double。改为使用Convert.toDouble(字符串s)。

如果属性不存在,则方法返回
字符串
“0”
。这不能强制转换为
double
@john,但如果读取器从文件中读取
“0”
,则为字符串。它起作用了。如果我这样做
value=“0”;返回值它也不起作用。为什么?从文件中读取“0”时,将其转换为请求的类型会遇到一些麻烦。当请求的属性不存在时,您不会执行该代码路径。如果该属性不存在,则您的方法将返回
字符串
“0”
。这不能强制转换为
double
@john,但如果读取器从文件中读取
“0”
,则为字符串。它起作用了。如果我这样做
value=“0”;返回值它也不起作用。为什么?从文件中读取“0”时,将其转换为请求的类型会遇到一些麻烦。当请求的属性不存在时,您不会执行该代码路径。但无论如何,我都不想
返回null
,对吗?那么我应该把你的代码放在哪里呢?另外,如果您能向我解释一下为什么如果读取器从文件中读取
“0”
,这是一个字符串,它工作正常,我只是更改了我的
返回“0”
返回Activator.CreateInstance(objectType)
谢谢如果您从文件中读取
“0”
,它将由
转换器进行转换;如果
返回“0”
,则不会返回。此外,您可能需要研究如何将此函数转换为使用泛型
this.X1=FileStructure.GetAttributeSafe(读取器,“X1”)
this.X1=(double)FileStructure.GetAttributeSafe(reader,“X1”,typeof(double))读得好得多
然后我需要将我的读卡器更改为丢弃参数
typeof(double))
?但无论如何我都不想
返回null
,对吗?那么我应该把你的代码放在哪里呢?另外,如果您能向我解释一下为什么如果读取器从文件中读取
“0”
,这是一个字符串,它工作正常,我只是更改了我的
返回“0”
返回Activator.CreateInstance(objectType)
谢谢如果您从文件中读取
“0”
,它将由
转换器进行转换;如果
返回“0”
,则不会返回。此外,您可能需要研究如何将此函数转换为使用泛型
this.X1=FileStructure.GetAttributeSafe(读取器,“X1”)
this.X1=(double)FileStructure.GetAttributeSafe(reader,“X1”,typeof(double))读得好得多然后我需要将我的读卡器更改为丢弃参数
typeof(double))
?我明白了。这是一个混淆,因为它从文件中读取
“0”
s就可以了。你的解决方案奏效了。谢谢但如果不是双人房呢?我需要让它更一般,我明白了。这是一个混淆,因为它从文件中读取
“0”
s就可以了。你的解决方案奏效了。谢谢但如果不是双人房呢?我需要让它通用