嵌套递归XML到CSV(使用Linq)

时间:2016-11-09 22:55:43

标签: c# xml linq csv recursion

如何从List<SPSecurableObject>创建CSV,其中SPSecurableObject是递归的

class SPSecurableObject
{
  public string ObjectName {get;set;}
  public List<RoleAssignments> RoleAssignments {get;set;}
  public List<SPSecurableObject> ChildObjects {get;set;}
}
class RoleAssignments
{
  public string PrincipalType {get;set;}
  public string DisplayName {get;set;}
  public string RoleDefBindings {get;set;}
}

我有XML数据文件(见下文),需要制作CSV文件。开始用Linq编写查询到XML并将数据加载到List<SPSecurableObject>,但现在想不出编写CSV的解决方案应该是这样的:

ObjectName,PrincipalType,DisplayName,RoleDefBindings
New Data,,,
Documents,,,
Documents2,User,John Doe,Limited Access
Documents2,Group,Group1,Full Control

XML:

<SPSecurableObject>
  <ObjectName>New Data</ObjectName>
  <ChildObjects>
    <SPSecurableObject>
      <ObjectName>Documents</ObjectName>
    </SPSecurableObject>
    <SPSecurableObject>
      <ObjectName>Documents2</ObjectName>
      <RoleAssignments>
        <SPRoleAssignment>
          <PrincipalType>User</PrincipalType>
          <Member>
            <User>
              <DisplayName>John Doe</DisplayName>
            </User>
          </Member>
          <RoleDefBindings>
            <RoleName>Limited Access</RoleName>
          </RoleDefBindings>
        </SPRoleAssignment>
        <SPRoleAssignment>
          <PrincipalType>Group</PrincipalType>
          <Member>
            <Group>
              <GroupName>Group1</GroupName>
            </Group>
          </Member>
          <RoleDefBindings>
            <RoleName>Full Control</RoleName>
          </RoleDefBindings>
        </SPRoleAssignment>
      </RoleAssignments>
    </SPSecurableObject>
</ChildObjects>
</SPSecurableObject>            

我的代码List<SPSecurableObject>是:

void Main()
{
    var xml = XDocument.Load (@"C:\temp\data.xml"); 
    var root = xml.Elements("SPSecurableObject");
    List<SPSecurableObject> result = Load(root);

}

List<SPSecurableObject> Load(IEnumerable<XElement> xml)
{
    return xml.Select(x=>new SPSecurableObject(){
                        ObjectName = x.Elements("ObjectName").Select(y=>y.Value).FirstOrDefault(),
                        RoleAssignments=(from ra in x.Elements("RoleAssignments").Elements("SPRoleAssignment") 
                    select new RoleAssignments{
                        PrincipalType=ra.Elements("PrincipalType").Select(y=>y.Value).FirstOrDefault(),
                        DisplayName=ra.Elements("PrincipalType").Select(y=>y.Value).FirstOrDefault() == "Group" ? ra.Elements("Member").Elements("Group").Elements("GroupName").Select(y=>y.Value).FirstOrDefault() : ra.Elements("Member").Elements("User").Elements("DisplayName").Select(y=>y.Value).FirstOrDefault(),
                        RoleDefBindings = ra.Elements("RoleDefBindings").Elements("RoleName").Select(y=>y.Value).FirstOrDefault()
                    }).ToList(),
                    ChildObjects = Load(x.Elements("ChildObjects").Elements("SPSecurableObject"))
}).ToList();
}

由于

1 个答案:

答案 0 :(得分:-1)

试试这个:

var xml = XDocument.Load("data.xml");

var sb = new StringBuilder();

foreach (var node in xml.Descendants("SPSecurableObject"))
{
    var objectName = node.Element("ObjectName").Value;

    if (node.Element("RoleAssignments") == null)
    {
        sb.Append(objectName).AppendLine(",,,");
        continue;
    }

    var lines = node.Descendants("SPRoleAssignment")
        .Select(elem => string.Join(",", elem.DescendantNodes().OfType<XText>()));

    if (lines.Any())
    {
        foreach (var line in lines)
            sb.Append(objectName).Append(',').AppendLine(line);
    }
    else
    {
        sb.Append(objectName).AppendLine(",,,");
    }
}

Console.WriteLine(sb);

这将为您提供所需的结果。