Linq to XML,选择给定节点的所有属性及其值
我有一个 xml 映射文件,看起来像这样,
<colourMappings>
<model name="modelX">
<mapping colour="White" configCode="1"></mapping>
<mapping colour="Aluminium" configCode="2"></mapping>
<mapping colour="Black" configCode="3"></mapping>
<mapping colour="Blue" configCode="4"></mapping>
<mapping colour="White Pearl" configCode="5"></mapping>
<mapping colour="Graphite" configCode="6"></mapping>
<mapping colour="Gunmetal" configCode="7"></mapping>
<mapping colour="Indigo" configCode="8"></mapping>
<mapping colour="Red" configCode="9"></mapping>
</model>
<model name="modelY">
<mapping colour="White" configCode="1" stConfigCode= "xx" dgConfigCode="hj"></mapping>
<mapping colour="Aluminium" configCode="2" stConfigCode= "xy" dgConfigCode="gh"></mapping>
<mapping colour="Black" configCode="3" stConfigCode= "xt" dgConfigCode="fg"></mapping>
<mapping colour="Blue" configCode="4" stConfigCode= "sd" dgConfigCode="fg"></mapping>
<mapping colour="White Pearl" configCode="5" stConfigCode= "df" dgConfigCode="df"></mapping>
<mapping colour="Graphite" configCode="6" stConfigCode= "xc" dgConfigCode="df"></mapping>
<mapping colour="Gunmetal" configCode="7" stConfigCode= "cv" dgConfigCode="cv"></mapping>
<mapping colour="Indigo" configCode="8" stConfigCode= "zx" dgConfigCode="vb"></mapping>
<mapping colour="Red" configCode="9" stConfigCode= "fg" dgConfigCode="cv"></mapping>
</model>
</colourMappings>
我希望能够在给定模型名称和颜色的情况下提取所有属性及其值,
例如
给定 ModelY 和 White,我想得到 configCode="1" stConfigCode= "xx" dgConfigCode="hj" 这可以是任何结构 - 数组、列表,无论
我一直在使用 Linq to XML 但无法获得正确的语法,
XDocument mappings = XDocument.Load(@"D:\colour_mappings.xml");
var q = from c in mappings.Descendants("model")
where (string)c.Attribute("name") == "modelY" && (string)c.Descendants("mapping").Attributes("colour").FirstOrDefault() == "White"
select c.Attributes();
有人知道如何做到这一点吗?
乐意使用任何方法,不一定非得是 Linq
I have an xml mapping file that looks something like this
<colourMappings>
<model name="modelX">
<mapping colour="White" configCode="1"></mapping>
<mapping colour="Aluminium" configCode="2"></mapping>
<mapping colour="Black" configCode="3"></mapping>
<mapping colour="Blue" configCode="4"></mapping>
<mapping colour="White Pearl" configCode="5"></mapping>
<mapping colour="Graphite" configCode="6"></mapping>
<mapping colour="Gunmetal" configCode="7"></mapping>
<mapping colour="Indigo" configCode="8"></mapping>
<mapping colour="Red" configCode="9"></mapping>
</model>
<model name="modelY">
<mapping colour="White" configCode="1" stConfigCode= "xx" dgConfigCode="hj"></mapping>
<mapping colour="Aluminium" configCode="2" stConfigCode= "xy" dgConfigCode="gh"></mapping>
<mapping colour="Black" configCode="3" stConfigCode= "xt" dgConfigCode="fg"></mapping>
<mapping colour="Blue" configCode="4" stConfigCode= "sd" dgConfigCode="fg"></mapping>
<mapping colour="White Pearl" configCode="5" stConfigCode= "df" dgConfigCode="df"></mapping>
<mapping colour="Graphite" configCode="6" stConfigCode= "xc" dgConfigCode="df"></mapping>
<mapping colour="Gunmetal" configCode="7" stConfigCode= "cv" dgConfigCode="cv"></mapping>
<mapping colour="Indigo" configCode="8" stConfigCode= "zx" dgConfigCode="vb"></mapping>
<mapping colour="Red" configCode="9" stConfigCode= "fg" dgConfigCode="cv"></mapping>
</model>
</colourMappings>
I want to be able to pull out all the attributes and their values given a model name and colour
e.g.
given ModelY and White, I'd like to get configCode="1" stConfigCode= "xx" dgConfigCode="hj"
This could be in any structure - array, list, whatever
I have been using Linq to XML but can't get the correct syntax
XDocument mappings = XDocument.Load(@"D:\colour_mappings.xml");
var q = from c in mappings.Descendants("model")
where (string)c.Attribute("name") == "modelY" && (string)c.Descendants("mapping").Attributes("colour").FirstOrDefault() == "White"
select c.Attributes();
anyone know how to do this?
Happy to use any method, doesn't necessarily need to be Linq
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新
总结为一个方法:
Update
Summarized in a method:
由于时间紧迫,我将使用两阶段流程,
但有兴趣了解这是否可以在一个查询中实现
as I am pushed for time I will use a 2 stage process
But would be interested to hear if this is possible in one query
只是因为我喜欢挑战,所以它在一个查询中:
Just because I like a challenge, here it is in one query:
我知道这现在已经很古老了,但我认为在不进行不必要的处理的情况下解决这个问题的方法看起来像这样:
在查询格式中,它将是:
I know this is ancient now, but I think the way to solve this problem without doing more processing than neccessary looks something like this:
In query format, it would be: