【问题标题】:Can't figure out how to use Linq to XML to do conditional adding of Nodes?不知道如何使用 Linq to XML 进行节点的条件添加?
【发布时间】:2016-06-29 01:12:24
【问题描述】:

我正在尝试使用 Linq to XML 从员工列表中创建一个文件,该员工列表也有与其关联的组列表。但是,我还必须对这些组进行一些条件检查。我有一些代码可以创建员工,但我不知道如何遍历组并有条件地添加元素。

这里是如何格式化 XML 的示例。他们希望每个员工拥有一个具有多个属性的节点。然后是员工下的一个节点,用于与他们关联的每个组。这就是我有问题的部分。

<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 to XML 完成吗?或者,我应该使用 XmlDocument 根据我的 List 写出节点吗?

【问题讨论】:

    标签: c# xml linq


    【解决方案1】:

    您可以以与XmlDocument 非常相似的方式使用XDocument,因此您当然不需要更改它。

    我可能会这样做:

    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
    }
    

    【讨论】:

    • 太棒了!正是我需要的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多