Linq to XML查询加入父子列表

时间:2010-11-28 22:45:54

标签: linq-to-xml

我有一个带有两个列表的xml文件,形成父子关系,如下所示:

  

<Categories>
<Category id="10000">Category 1</Category>
<Category id="10100">Category 2</Category>
<Category id="10101">Category 3</Category> ...
</Categories>

     

<Actions>
<Action name="Action 1"><Categories><Category id="10100"/><Category id="10102"/></Categories></Action>
<Action name="Action 2"><Categories><Category id="10101"/><Category id="10103"/></Categories></Action>
...
</Actions>

每个Action至少有一个来自主列表的类别。 我正在尝试在我的查询中使用父类别列表中的名称来扩展每个操作中的类别,因此输出结构将是这样的:

  

班级行动{
      public string ActionName;
      公开列表<Category&gt;分类;
  }

     

班级类别{
      公共字符串CategoryName;
      public int CategoryId;
  }

我完全迷了,请帮帮忙。

1 个答案:

答案 0 :(得分:0)

您可以先创建一个List来存储您在创建Action对象时引用的唯一Category对象。 示例C#代码:

    XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");
    List<Category> referencedCategories =
        (from cat in doc.Root.Element("Categories").Elements("Category")
         where doc.Root.Element("Actions").Elements("Action").Elements("Categories").Elements("Category").Any(c2 => cat.Attribute("id").Value == c2.Attribute("id").Value)
         select new Category()
         {
             CategoryId = (int)cat.Attribute("id"),
             CategoryName = (string)cat
         }).ToList();

    List<Action> actions =
        (from action in doc.Root.Element("Actions").Elements("Action")
         select new Action()
         {
             ActionName = (string)action.Attribute("name"),
             Categories = (from cat in referencedCategories
                           where action.Element("Categories").Elements("Category").Any(cat2 => (int)cat2.Attribute("id") == cat.CategoryId)
                           select cat).ToList()
         }).ToList();

    foreach (Action action in actions)
    {
        Console.WriteLine("Action {0}:", action.ActionName);
        foreach (Category cat in action.Categories)
        {
            Console.WriteLine("\tId: {0}; Name: {1}", cat.CategoryId, cat.CategoryName);
        }
        Console.WriteLine();
    }

XML样本

<Root>
  <Categories>
    <Category id="10000">Category 1</Category>
    <Category id="10100">Category 2</Category>
    <Category id="10101">Category 3</Category>
    <Category id="10102">Category 4</Category>
    <Category id="10103">Category 5</Category>
  </Categories>

  <Actions>
    <Action name="Action 1">
      <Categories>
        <Category id="10100"/>
        <Category id="10102"/>
      </Categories>
    </Action>
    <Action name="Action 2">
      <Categories>
        <Category id="10101"/>
        <Category id="10103"/>
      </Categories>
    </Action>
  </Actions>

</Root>

该样本输出

Action Action 1:
        Id: 10100; Name: Category 2
        Id: 10102; Name: Category 4

Action Action 2:
        Id: 10101; Name: Category 3
        Id: 10103; Name: Category 5

如果您更喜欢使用join子句,则可以按如下方式更改代码:

    XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");
    List<Category> referencedCategories =
        (from cat in doc.Root.Element("Categories").Elements("Category")
         join cat2 in doc.Root.Element("Actions").Elements("Action").Elements("Categories").Elements("Category") on cat.Attribute("id").Value equals cat2.Attribute("id").Value
         select new Category()
         {
             CategoryId = (int)cat.Attribute("id"),
             CategoryName = (string)cat
         }).ToList();

    List<Action> actions =
        (from action in doc.Root.Element("Actions").Elements("Action")
         select new Action()
         {
             ActionName = (string)action.Attribute("name"),
             Categories = (from cat in referencedCategories
                           join cat2 in action.Element("Categories").Elements("Category") on cat.CategoryId equals (int)cat2.Attribute("id") 
                           select cat).ToList()
         }).ToList();