Linq to XML 查询连接父子列表
我有一个包含两个列表的 xml 文件,形成父子关系,如下所示:
<代码><类别>
<类别 id="10000">类别 1
<类别 id="10100">类别 2
<类别 id="10101">类别 3 ...<代码><操作>
<操作名称=“操作 1”><类别><类别 id=10100/><类别 id=10102/>
<操作名称=“操作2”><类别><类别id=10101/><类别id=10103/>
...
每个操作至少具有主列表中的一个类别。 我试图在查询中使用父类别列表中的名称来扩展每个操作中的类别,因此输出结构将如下所示:
类动作{
公共字符串 ActionName;
公共列表<类别
>类别;
}类别类别{
公共字符串类别名称;
公共 int 类别 ID;
}
迷失了,请帮助。
I have an xml file with two lists, forming parent-child relations, like this:
<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>
Each Action has at least one category from master list.
I am trying in my query to expand categories in each action with their names from parent category list, so the output structure would be something like this:
class Action {
public string ActionName;
public List<Category
> Categories;
}class Category {
public string CategoryName;
public int CategoryId;
}
I am completely lost, please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以首先创建一个列表来存储唯一的类别对象,然后在创建操作对象时引用。
C# 代码示例:
以 XML 示例作为
示例输出
如果您更喜欢使用 join 子句,则可以按如下方式更改代码:
You could first create a List to store unique Category objects you then reference when creating Action objects.
Example C# code:
With the XML sample being
that sample outputs
If you prefer to use the join clause the code could be changed as follows: