2015-03-03 142 views
0

如何从使用Filehelpers库输出的文本字段中删除可选字段。我使用C#删除写入文本字段的可选字段Filehelpers

例如, 我有一个共享类文件 与属性,如recordnumber,填料,付款,endingspaces

然后,我需要只写recordnumber和支付到文本文件中没有填料。

[FixedLengthRecord(FixedMode.ExactLength)] 
public partial class Person 
{ 
[FieldFixedLength(10)] 
public string FirstName; 
[FieldFixedLength(10)] 
public string LastName; 

[FieldOptional] 
[FieldFixedLength(5)] 
public string Optional1; 

[FieldOptional] 
[FieldFixedLength(5)] 
public string Optional2; 

[FieldOptional] 
[FieldFixedLength(5)] 
public string Optional3; 

}

class Program 
{ 
private static void Main(string[] args) 
{ 
    var engine = new FileHelperEngine<Person>(); 
    Person[] allPersonRecords = GetPersonExportFromDataBase() as Person[];//This will only get the FirstName,LastName,Optional2. No result for Optional1 and Optional3 

    FileHelperEngine enginePerson = new FileHelperEngine(typeof(Person)); 

    enginePerson.AppendToFile(FileName, allPersonRecords); //Write the records to the file 

//Current Output looks like this:John  Lee   title  
//The output which I want is:John  Lee  title 

} 
} 
+1

请包括您的问题的一些代码。 – Alfergon 2015-03-03 11:24:49

+0

你能详细说明你想输出什么,输出数据的来源等吗? – Ceisc 2015-03-03 11:29:15

+0

@Alfergon您好我已经包含了一些代码 – namelessly12 2015-03-04 04:09:33

回答

0

可以使用INotifyWrite属性写入之前拦截线,并修改它。这是一个工作示例。

[DelimitedRecord(",")] 
public partial class Person : INotifyWrite<Person> 
{ 
    public string FirstName; 
    public string LastName; 
    [FieldOptional] 
    public string Optional1; 
    [FieldOptional] 
    public string Optional2; 
    [FieldOptional] 
    public string Optional3; 

    public void BeforeWrite(BeforeWriteEventArgs<Person> e) 
    { 
    } 

    public void AfterWrite(AfterWriteEventArgs<Person> e) 
    { 
     // count the non-optional fields 
     var numberOfNonOptionalFields = typeof(Person).GetFields() 
      .Where(f => !f.GetCustomAttributes(false).Any(a => a is FieldOptionalAttribute)) 
      .Count(); 

     // take only the first n tokens 
     e.RecordLine = String.Join(",", e.RecordLine.Split(',').Take(numberOfNonOptionalFields)); 
    } 
}  

class Program 
{ 
    private static void Main(string[] args) 
    { 
     var engine = new FileHelperEngine<Person>(); 
     var export = engine.WriteString(
        new Person[] { 
         new Person() { 
          FirstName = "Joe", 
          LastName = "Bloggs", 
          Optional1 = "Option 1", 
          Optional2 = "Option 2", 
          Optional3 = "Option 3" 
         } 
        }); 
     Assert.AreEqual("Joe,Bloggs" + Environment.NewLine, export); 
     Console.WriteLine("Export was as expected"); 
     Console.ReadLine(); 
    } 
} 
+0

感谢您的回复,我已经尝试过您的代码,但我无法执行我所需的操作。对不起,能够根据我的上述代码进一步为我提供建议? – namelessly12 2015-03-04 04:15:49

+0

然后修改'AfterWrite()'以取得你需要的部分:'e.RecordLine = e.RecordLine.Substring(0,20)+ e.RecordLine.Substring(25,5)' – shamp00 2015-03-04 10:12:19

+0

别忘了' Person'需要实现'INotifyWrite ',否则'AfterWrite'永远不会被调用。 – shamp00 2015-03-04 10:13:28

0

如果你想只静态地省略可选字段(即从不使用它们和你从来要输出他们),你可以只创建另一个类代表所需的输出格式,然后转换列表从一个对象类型到另一个使用LINQ:

class Program 
{ 
    static void Main(string[] args) 
    { 
     // dummy test data 
     var originalAllPersonRecords = new Person[] 
     { 
      new Person { FirstName = "John", LastName = "Lee", Optional2 = "title" }, 
     };//This will only get the FirstName,LastName,Optional2. No result for Optional1 and Optional3 

     var allPersonRecords = from p in originalAllPersonRecords select new OutputPerson{ FirstName = p.FirstName, LastName = p.LastName, Optional2 = p.Optional2 }; 

     FileHelperEngine enginePerson = new FileHelperEngine(typeof(OutputPerson)); 

     string fileName = "Wherever you want the file to go"; 
     enginePerson.AppendToFile(fileName, allPersonRecords); //Write the records to the file 

     //Current Output looks like this:John  Lee   title  
     //The output which I want is:John  Lee  title 
    } 
} 
//New class added representing the output format 
[FixedLengthRecord(FixedMode.ExactLength)] 
class OutputPerson 
{ 
    [FieldFixedLength(10)] 
    public string FirstName; 

    [FieldFixedLength(10)] 
    public string LastName; 

    [FieldOptional] 
    [FieldFixedLength(5)] 
    public string Optional2; 
} 

[FixedLengthRecord(FixedMode.ExactLength)] 
class Person 
{ 
    [FieldFixedLength(10)] 
    public string FirstName; 
    [FieldFixedLength(10)] 
    public string LastName; 

    [FieldOptional] 
    [FieldFixedLength(5)] 
    public string Optional1; 

    [FieldOptional] 
    [FieldFixedLength(5)] 
    public string Optional2; 

    [FieldOptional] 
    [FieldFixedLength(5)] 
    public string Optional3; 
}