使用 LINQ to xml 过滤固定元素集,其中不存在元素具有默认值

发布于 2024-10-19 05:46:51 字数 1295 浏览 1 评论 0原文

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2024-10-26 05:46:51

以下是一些 C# 示例代码,可生成您发布的输出:

    XDocument input = XDocument.Load("input.xml");
    XDocument output = new XDocument(
        new XElement(input.Root.Name,
            from level in input.Root.Elements("level1")
            select new XElement("level1",
                level.Attributes(),
                from name in new string[] { "A", "C", "E", "F" }
                let data = level.Elements("data").FirstOrDefault(d => d.Attribute("name").Value == name)
                select (data != null ? data : new XElement("data",
                    new XAttribute("name", name),
                    new XAttribute("count", 0),
                    new XAttribute("avg", 0)
                    )))));
    output.Save("output.xml");

Here is some C# sample code that produces the output you posted:

    XDocument input = XDocument.Load("input.xml");
    XDocument output = new XDocument(
        new XElement(input.Root.Name,
            from level in input.Root.Elements("level1")
            select new XElement("level1",
                level.Attributes(),
                from name in new string[] { "A", "C", "E", "F" }
                let data = level.Elements("data").FirstOrDefault(d => d.Attribute("name").Value == name)
                select (data != null ? data : new XElement("data",
                    new XAttribute("name", name),
                    new XAttribute("count", 0),
                    new XAttribute("avg", 0)
                    )))));
    output.Save("output.xml");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文