[英]How do you test for null elements in System.Array
我将 excel 文档解析为 System.Array,然后使用此数据开发 DataTable。 问题是并非 System.Array 中的每个元素都包含一个字符串并且是 null。 见下图:
我的问题是如何测试 null 案例。
这是我的方法:
public static System.Data.DataTable ConvertCSVtoDataTable(string strFilePath)
{
System.Data.DataTable dt = new System.Data.DataTable();
StreamReader sr = new StreamReader(strFilePath);
string[] headers = sr.ReadLine().Split(',');
foreach (string header in headers)
dt.Columns.Add(header);
sr.Close();
Microsoft.Office.Interop.Excel.Workbook wb = null;
Microsoft.Office.Interop.Excel.Worksheet ws = null;
Microsoft.Office.Interop.Excel.Application app = null;
int lastRow = 0;
app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
wb = app.Workbooks.Open(strFilePath);
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1];
lastRow = ws.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell).Row;
int currentRow = 0;
for (int index = 2; index <= lastRow; index++)
{
System.Array values = (System.Array)ws.get_Range("A" + index.ToString(), "M" + index.ToString()).Cells.Value;
dt.Rows.Add();
dt.Rows[currentRow]["Plugin ID"] = values.GetValue(1, 1).ToString();
dt.Rows[currentRow]["CVE"] = values.GetValue(1, 2).ToString();
dt.Rows[currentRow]["CVSS"] = values.GetValue(1, 3).ToString();
dt.Rows[currentRow]["Risk"] = values.GetValue(1, 4).ToString();
dt.Rows[currentRow]["Host"] = values.GetValue(1, 5).ToString();
dt.Rows[currentRow]["Protocol"] = values.GetValue(1, 6).ToString();
dt.Rows[currentRow]["Port"] = values.GetValue(1, 7).ToString();
dt.Rows[currentRow]["Name"] = values.GetValue(1, 8).ToString();
dt.Rows[currentRow]["Synopsis"] = values.GetValue(1, 9).ToString();
dt.Rows[currentRow]["Description"] = values.GetValue(1, 10).ToString();
dt.Rows[currentRow]["Solution"] = values.GetValue(1, 11).ToString();
dt.Rows[currentRow]["See Also"] = values.GetValue(1, 12).ToString();
dt.Rows[currentRow]["Plugin Output"] = values.GetValue(1, 13).ToString();
currentRow++;
}
return dt;
}
这是我尝试过但没有用的一些检查
if (!values.Equals(null))
dt.Rows[currentRow]["CVSS"] = values.GetValue(1, 3).ToString();
else
dt.Rows[currentRow]["CVSS"] = "";
看来这不起作用,因为它似乎正在检查是否存在任何元素,而不是该位置的特定元素是否为 null。
if (values.GetValue(1, 3).ToString() == null)
dt.Rows[currentRow]["CVSS"] = "";
else
dt.Rows[currentRow]["CVSS"] = values.GetValue(1, 3).ToString();
这将引发 NullReferenceExecption: Object reference not set to an instance of an object.
values.Equals(null)
将抛出null
,它不会返回true
。anything.ToString()
也会抛出null
,它不会返回null
(或空)字符串引用。您想要做的可能只是:
if(values.GetValue(1, 3) == null) ...
将if (values.GetValue(1, 3).ToString() == null)
更改为
if (values.GetValue(1, 3) == null)
if
,正如您所写的,在值本身为空的情况下,它可以“转换”为if(null.ToString() ...)
。
而且你不能在null
上调用任何东西。
您可以将 null 条件运算符 (?.) 与空合并运算符 (??) 结合使用,如下所示:
dt.Rows[currentRow]["CVSS"] = values.GetValue(1, 3)?.ToString() ?? ""
使用 null 条件运算符 (?.) GetValue(1,3) 仅当它不是 null 时才会被评估。 如果它 null dt.Rows[currentRow]["CVSS"] 将有一个字符串空值 ("")。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.