文件解析获取具体信息
File parsing to get specific information
我有这样的文件:
-- Name John Smith, PhD
[20,00] Title : Software Engineer
[20,00] Employee Id : 20307
[20,00] Level : 41
[20,00] Start Date : 04/21/2014
[20,00] Org : Security
每个文件仅包含 1 人的条目。我需要从此文件中提取名称、标题和级别,然后创建并填充以下 class:
的 object
public class Person
{
public string Name { get; set; }
public string Title { get; set; }
public string Level { get; set; }
}
一种方法是创建一个需要匹配的字符串列表:
List<string> properties = new List<string> { "Name", "Title", "Level" };
然后逐行读取文件并尝试找到匹配项:
properties.Any(x => line.Contains(x))
如果找到匹配项,我会进行一些字符串拆分和解析以获得所需的值。但这将涉及大量的手工工作。有没有一种方法可以将字符串映射到 class 的变量并进行解析?
我的意思是这样的:
Person person = new Person();
Dictionary<string, Object> FieldToDataMember = new Dictionary<string, Object>()
{
{"Name", person.Name},
{"Title", person.Title},
{"Level", person.Level}
};
现在我逐行读取文件,如果它匹配其中一个键,我进行解析并直接更新相应变量的值。这样,我就不需要先找到是否有匹配项,然后再次检查它匹配的是哪个字符串,以便能够将其放入正确的变量中。这样的事情可能吗?
感谢您的帮助。谢谢!
编辑:我还想退出循环(foreach(文件中的字符串行))并在找到我要查找的所有属性后停止进一步读取文件。
使用 属性 名称字符串集合执行此操作的一种方法是使用反射来获取 属性 并设置值。与直接设置属性相比,这需要额外的开销,但它比您要求的代码行少。
我们可以使用字典或元组列表(或自定义 class)将文件中的字符串映射到实际的 属性 名称(例如 "Start Date"
和 StartDate
).
这是一个示例,其中我添加了一个 public static Person FromFile(string filePath)
方法,该方法将接受一个文件路径和 return 一个新的 Person
,其属性从文件的内容中设置。
它的工作原理是首先确定字符串数组中的任何 属性 名称是否包含在文件行中。如果是,那么它会根据您的文件示例使用一些逻辑来获取 属性 的值,然后使用反射来设置 Person
对象的 属性 值:
public class Person
{
public string Name { get; set; }
public string Title { get; set; }
public string Level { get; set; }
public string StartDate { get; set; }
private class FileToPropertyMap
{
public string FileValue { get; }
public string PropertyName { get; }
public bool IsSet { get; set; }
public FileToPropertyMap(string fileValue, string propertyName)
{
FileValue = fileValue;
PropertyName = propertyName;
}
}
public static Person FromFile(string filePath)
{
if (!File.Exists(filePath)) throw new FileNotFoundException(nameof(filePath));
var person = new Person();
var propertyMap = new List<FileToPropertyMap>
{
new FileToPropertyMap("Name", "Name"),
new FileToPropertyMap("Title", "Title"),
new FileToPropertyMap("Level", "Level"),
new FileToPropertyMap("Start Date", "StartDate"),
};
foreach (var line in File.ReadLines(filePath))
{
// Find a match for one of the properties
var match = propertyMap.FirstOrDefault(p => line.Contains(p.FileValue));
if (match == null) continue;
// Get the value of the property from the file line
var value = line.Substring(line.IndexOf(match.FileValue) +
match.FileValue.Length).Trim();
if (value.Contains(':')) value = value.Split(':')[1].Trim();
// Set the property value using reflection
person.GetType().GetProperty(match.PropertyName).SetValue(person, value);
// Mark this property as "IsSet"
match.IsSet = true;
// If we've set all the properties, exit the loop
if (propertyMap.All(p => p.IsSet)) break;
}
return person;
}
}
在使用中,这看起来像:
Person myPerson = Person.FromFile("@c:\Public\PeopleFiles\JohnSmith.txt");
尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.IO;
namespace ConsoleApplication167
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
List<Person> people = new List<Person>();
StreamReader reader = new StreamReader(FILENAME);
string line = "";
Person person = null;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0)
{
if (line.StartsWith("-- Name"))
{
person = new Person();
people.Add(person);
person.Name = line.Replace("-- Name", "").Trim();
}
else
{
string pattern = "](?'key'[^:]+):(?'value'.*)";
Match match = Regex.Match(line, pattern);
string key = match.Groups["key"].Value.Trim();
string value = match.Groups["value"].Value.Trim();
switch (key)
{
case "Title" :
person.Title = value;
break;
case "Level":
person.Level = value;
break;
}
}
}
}
}
}
public class Person
{
public string Name { get; set; }
public string Title { get; set; }
public string Level { get; set; }
}
}
我有这样的文件:
-- Name John Smith, PhD
[20,00] Title : Software Engineer
[20,00] Employee Id : 20307
[20,00] Level : 41
[20,00] Start Date : 04/21/2014
[20,00] Org : Security
每个文件仅包含 1 人的条目。我需要从此文件中提取名称、标题和级别,然后创建并填充以下 class:
的 objectpublic class Person
{
public string Name { get; set; }
public string Title { get; set; }
public string Level { get; set; }
}
一种方法是创建一个需要匹配的字符串列表:
List<string> properties = new List<string> { "Name", "Title", "Level" };
然后逐行读取文件并尝试找到匹配项:
properties.Any(x => line.Contains(x))
如果找到匹配项,我会进行一些字符串拆分和解析以获得所需的值。但这将涉及大量的手工工作。有没有一种方法可以将字符串映射到 class 的变量并进行解析?
我的意思是这样的:
Person person = new Person();
Dictionary<string, Object> FieldToDataMember = new Dictionary<string, Object>()
{
{"Name", person.Name},
{"Title", person.Title},
{"Level", person.Level}
};
现在我逐行读取文件,如果它匹配其中一个键,我进行解析并直接更新相应变量的值。这样,我就不需要先找到是否有匹配项,然后再次检查它匹配的是哪个字符串,以便能够将其放入正确的变量中。这样的事情可能吗?
感谢您的帮助。谢谢!
编辑:我还想退出循环(foreach(文件中的字符串行))并在找到我要查找的所有属性后停止进一步读取文件。
使用 属性 名称字符串集合执行此操作的一种方法是使用反射来获取 属性 并设置值。与直接设置属性相比,这需要额外的开销,但它比您要求的代码行少。
我们可以使用字典或元组列表(或自定义 class)将文件中的字符串映射到实际的 属性 名称(例如 "Start Date"
和 StartDate
).
这是一个示例,其中我添加了一个 public static Person FromFile(string filePath)
方法,该方法将接受一个文件路径和 return 一个新的 Person
,其属性从文件的内容中设置。
它的工作原理是首先确定字符串数组中的任何 属性 名称是否包含在文件行中。如果是,那么它会根据您的文件示例使用一些逻辑来获取 属性 的值,然后使用反射来设置 Person
对象的 属性 值:
public class Person
{
public string Name { get; set; }
public string Title { get; set; }
public string Level { get; set; }
public string StartDate { get; set; }
private class FileToPropertyMap
{
public string FileValue { get; }
public string PropertyName { get; }
public bool IsSet { get; set; }
public FileToPropertyMap(string fileValue, string propertyName)
{
FileValue = fileValue;
PropertyName = propertyName;
}
}
public static Person FromFile(string filePath)
{
if (!File.Exists(filePath)) throw new FileNotFoundException(nameof(filePath));
var person = new Person();
var propertyMap = new List<FileToPropertyMap>
{
new FileToPropertyMap("Name", "Name"),
new FileToPropertyMap("Title", "Title"),
new FileToPropertyMap("Level", "Level"),
new FileToPropertyMap("Start Date", "StartDate"),
};
foreach (var line in File.ReadLines(filePath))
{
// Find a match for one of the properties
var match = propertyMap.FirstOrDefault(p => line.Contains(p.FileValue));
if (match == null) continue;
// Get the value of the property from the file line
var value = line.Substring(line.IndexOf(match.FileValue) +
match.FileValue.Length).Trim();
if (value.Contains(':')) value = value.Split(':')[1].Trim();
// Set the property value using reflection
person.GetType().GetProperty(match.PropertyName).SetValue(person, value);
// Mark this property as "IsSet"
match.IsSet = true;
// If we've set all the properties, exit the loop
if (propertyMap.All(p => p.IsSet)) break;
}
return person;
}
}
在使用中,这看起来像:
Person myPerson = Person.FromFile("@c:\Public\PeopleFiles\JohnSmith.txt");
尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.IO;
namespace ConsoleApplication167
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
List<Person> people = new List<Person>();
StreamReader reader = new StreamReader(FILENAME);
string line = "";
Person person = null;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0)
{
if (line.StartsWith("-- Name"))
{
person = new Person();
people.Add(person);
person.Name = line.Replace("-- Name", "").Trim();
}
else
{
string pattern = "](?'key'[^:]+):(?'value'.*)";
Match match = Regex.Match(line, pattern);
string key = match.Groups["key"].Value.Trim();
string value = match.Groups["value"].Value.Trim();
switch (key)
{
case "Title" :
person.Title = value;
break;
case "Level":
person.Level = value;
break;
}
}
}
}
}
}
public class Person
{
public string Name { get; set; }
public string Title { get; set; }
public string Level { get; set; }
}
}