我有一行值作为CSV文件, 我需要找到它们中的每一个是否存在于另一个文件中(XML就此而言)。
我已阅读CSV数据,拆分值并搜索代码有效但输出错误结果(“查找”不存在的值) 我会胃口大开外帮忙。 谢谢Shabi
我的代码
string csvFile = @"Afile.csv";
string[] lines = System.IO.File.ReadAllLines(csvFile);
string namesValues = lines[0];
List<string> allValidNamesValues = namesValues.Split(',').ToList();
bool result = false;
string DeviceName = Phone.Tag("Name").Value();
if (allValidNamesValues.Any(vn => DeviceName.IndexOf(vn, StringComparison.OrdinalIgnoreCase) != -1))
{
Info = string.Format("The Device is supported");
return true;
}
return result;
答案 0 :(得分:0)
我建议你真的想要这样的东西:
实施
- (void)room:(GPGRealTimeRoom *)room didChangeStatus:(GPGRealTimeRoomStatus)status {
if (status == GPGRealTimeRoomStatusDeleted) {
NSLog(@"RoomStatusDeleted");
[self.lobbyDelegate multiPlayerGameWasCanceled];
_roomToTrack = nil;
} else if (status == GPGRealTimeRoomStatusConnecting) {
NSLog(@"RoomStatusConnecting");
} else if (status == GPGRealTimeRoomStatusActive) {
NSLog(@"RoomStatusActive! Game is ready to go");
_roomToTrack = room;
// We may have a view controller up on screen if we're using the
// invite UI
[self.lobbyDelegate readyToStartMultiPlayerGame];
} else if (status == GPGRealTimeRoomStatusAutoMatching) {
NSLog(@"RoomStatusAutoMatching! Waiting for auto-matching to take place");
_roomToTrack = room;
} else if (status == GPGRealTimeRoomStatusInviting) {
NSLog(@"RoomStatusInviting! Waiting for invites to get accepted");
} else {
NSLog(@"Unknown room status %ld", status);
}
}
答案 1 :(得分:0)
我猜这里的主要问题是你只处理文件的第一行(lines[0]
)。您可以使用嵌套循环来处理所有行,也可以使用SelectMany
(LINQ)。例如(并使用ReadLines
来避免一次缓冲所有数据):
static readonly char[] comma = { ',' };
...
if(File.ReadLines(csvFile).SelectMany(x => x.Split(comma)).Any(
vn => DeviceName.IndexOf(vn, StringComparison.OrdinalIgnoreCase) != -1))
{
// whatever
}
请注意,我不相信字符串测试是正确的(对我而言,拥有vn.IndexOf(DeviceName, ...)
似乎更为明显,但是:如果没有示例,很难检查它。
要进行更多正确的解析CSV更复杂;您应该考虑像LumenWorksCsvReader
这样的库