使用 LINQ to xml 过滤固定元素集,其中不存在元素具有默认值
<?xml version="1.0" encoding="UTF-8"?>
<root>
<level1 name="abc1" type="xyz">
<data name="A" count="23" avg="56" />
<data name="B" count="67" avg="18" />
<data name="C" count="42" avg="23" />
</level1>
<level1 name="abc2" type="xyz">
<data name="B" count="56" avg="29" />
<data name="C" count="23" avg="64" />
<data name="E" count="78" avg="21" />
</level1>
</root>
如果过滤器列表是{A,C,E,F}并且想要使用列表中具有名称属性值的所有数据来过滤上述文档。如果找不到匹配的元素,我仍然希望有一个具有该名称的数据元素,并且该元素的所有其他属性设置为某些默认值:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<level1 name="abc1" type="xyz">
<data name="A" count="23" avg="56" />
<data name="C" count="42" avg="23" />
<data name="E" count="0" avg="0" />
<data name="F" count="0" avg="0" />
</level1>
<level1 name="abc2" type="xyz">
<data name="A" count="0" avg="0" />
<data name="C" count="23" avg="64" />
<data name="E" count="78" avg="21" />
<data name="F" count="0" avg="0" />
</level1>
</root>
此外,我还可以根据选择标准仅选择我想要的属性吗。
<?xml version="1.0" encoding="UTF-8"?>
<root>
<level1 name="abc1" type="xyz">
<data name="A" count="23" avg="56" />
<data name="B" count="67" avg="18" />
<data name="C" count="42" avg="23" />
</level1>
<level1 name="abc2" type="xyz">
<data name="B" count="56" avg="29" />
<data name="C" count="23" avg="64" />
<data name="E" count="78" avg="21" />
</level1>
</root>
If the filter list is {A,C,E,F} and want to filter the above document with all the data that have their name attribute's value in the list. If a matching element is not found I would still want to have a data element with that name and all other attributes of that elements set to some default values as:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<level1 name="abc1" type="xyz">
<data name="A" count="23" avg="56" />
<data name="C" count="42" avg="23" />
<data name="E" count="0" avg="0" />
<data name="F" count="0" avg="0" />
</level1>
<level1 name="abc2" type="xyz">
<data name="A" count="0" avg="0" />
<data name="C" count="23" avg="64" />
<data name="E" count="78" avg="21" />
<data name="F" count="0" avg="0" />
</level1>
</root>
Additionally, can I also select only the attributes that I want based on a selection criteria.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是一些 C# 示例代码,可生成您发布的输出:
Here is some C# sample code that produces the output you posted: