2016-06-28 5 views
0

私はLinq to XMLを使用して、関連付けられているグループのリストも持っている従業員のリストからファイルを作成しようとしています。しかし、私はそれらのグループといくつかの条件付きチェックを行う必要があります。私は従業員を作成するいくつかのコードを持っていますが、どのようにグループをループし、要素を条件付きで追加するのか分かりません。LinqからXMLへのノードの条件付き追加の使い方を理解できませんか?

XMLがどのようにフォーマットされるのかの例です。複数の属性を持つ従業員1人あたり1つのノードが必要です。次に、関連付けられている各グループの従業員の下にあるノード。それが私が問題を抱えている部分です。

<CXIXML> 
    <Directives> 
    <Updates> 
     <Emp tasEmpID="00123" lName="Doe" fName="John" empStatus="A" classification="S" /> 
     <Group groupId="1"> 
     <Group groupId="5"> 
     <Group groupId="12"> 
     <Emp tasEmpID="00456" lName="Smith" fName="Jane" empStatus="A" classification="S" /> 
     <Group groupId="1"> 
    </Updates> 
    </Directives> 
</CXIXML> 

私は現在、Linq to XMLを使用して従業員ファイルを作成しています。これはうまくいきました。最初は、従業員のXMLファイルを作成し、従業員リストをループバックし、属性内のIDに基づいて従業員ノードを見つけ、グループ内のグループを検索してグループノードを追加しました。

私は、各従業員オブジェクトが既にClockGroupリストに関連付けられているグループを持っているという事実に加えて、私が作成する必要がある従業員を持っているので、これを行う簡単な方法が必要であるように感じます。

public class Employee 
{ 
    public string EmployeeId { get; set; } 
    public string AdminEnrollFlag { get; set; } 
    public string EmployeeLastName { get; set; } 
    public string EmployeeFirstName { get; set; } 
    public string EmployeeWorkStat { get; set; } 
    public string EmployeeCo { get; set; } 

    public List<GroupAssociation> ClockGroup { get; set; } 
} 

public class GroupAssociation 
{ 
    public int emp_id { get; set; } 
    public int group_id { get; set; } 
} 


private static void CreateExportFile(List<Employee> fileEmployees, string fileCount) 
{ 
    string fileLocation = ConfigurationManager.AppSettings["FileLocation"]; 

    XDocument xmlDoc = new XDocument(
     new XDeclaration("1.0", "utf-8", "yes"), 
     new XElement("CXIXML", 
      new XElement("Directives", 
      new XElement("Updates", 
       from emp in fileEmployees 
       select new XElement("Emp", 
            new XAttribute("tasEmpID", emp.EmployeeId.PadLeft(8, '0')), 
            new XAttribute("lName", emp.EmployeeLastName), 
            new XAttribute("fName", emp.EmployeeFirstName),         
            new XAttribute("empStatus", SetWorkStat(emp.EmployeeWorkStat)), 
            new XAttribute("classification", SetClassification(emp.AdminWebAccess, emp.AdminEnrollFlag)))))) 
    ); 


    string formatedDate = System.DateTime.Now.ToString("yyyyMMdd_hhmm_"); 
    string filePath = fileLocation + "VTC_" + formatedDate + fileCount.PadLeft(3, '0') + ".xml"; 
    xmlDoc.Save(filePath); 
} 


<CXIXML> 
    <Directives> 
    <Updates> 
     <Emp tasEmpID="00123" lName="Doe" fName="John" empStatus="A" classification="S" /> 
     <Emp tasEmpID="00456" lName="Smith" fName="Jane" empStatus="A" classification="S" /> 
    </Updates> 
    </Directives> 
</CXIXML> 

ここではグループを持つキッカーです。私はグループを見るためにいくつかの条件があります。グループ関連が0件ある場合は、groupIdが特定の番号であるノードを書き出す必要があります(例:グループ関連なしのGroupId = 2)。また、管理者権限を持っているかどうかを調べる必要があります。特定のgroupId(例:GroupId = 1)がなければ、すべてのグループを書き出す必要があります。

これはすべてLinqとXMLで行うことができると思いますか?または、私のリストに基づいてノードを書き出すXmlDocumentを使用する必要がありますか?

答えて

1

XDocumentは、XmlDocumentと非常によく似た方法で使用できますので、変更する必要はありません。

私はおそらくこのような何かをしたい:それは、従業員ごとに好きなよう

var employeeUpdates = fileEmployees.SelectMany(CreateEmployeeElements); 

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"), 
    new XElement("CXIXML", 
     new XElement("Directives", 
      new XElement("Updates", employeeUpdates) 
     ) 
    ) 
); 

そしてCreateEmployeeElementsは、できるだけ多くの要素を返すことができます(私は細部のほとんどを残してきた):

private static IEnumerable<XElement> CreateEmployeeElements(Employee employee) 
{ 
    yield return new XElement("Emp", /* content */); 

    if (employee.ClockGroup.Count == 0) 
    { 
     yield return new XElement("Group", new XAttribute("groupId", 2)); 
    } 

    // and so on for more element conditions 
} 
+0

驚くばかり!正確に私が必要としたもの。ありがとう! – Caverman

関連する問題