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

本文关键字:使用 Linq CSV 递归 XML 嵌套 | 更新日期: 2023-09-27 18:17:15

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

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();
}

谢谢

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

试试这个:

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);