根据父节点的属性获取子节点

本文关键字:获取 子节点 属性 父节点 | 更新日期: 2023-09-27 17:55:58

我有一个XML格式的CrystalReport报告(很抱歉冗长,我删除了大多数示例数据)

<?xml version="1.0" encoding="UTF-8" ?>
<FormattedReport xmlns = 'urn:crystal-reports:schemas' xmlns:xsi = 'http://www.w3.org/2000/10/XMLSchema-instance'>
<FormattedAreaPair Level="0" Type="Report">
<FormattedAreaPair Level="1" Type="Details">
<FormattedArea Type="Details">
<FormattedSections>
<FormattedSection SectionNumber="0">
<FormattedReportObjects>
<FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" FieldName="{AIRCRAFT.Tail Number}"><ObjectName>Field2</ObjectName>
<FormattedValue>C-FBCS</FormattedValue>
<Value>C-FBCS</Value>
</FormattedReportObject>
<FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" FieldName="{AIRCRAFT.Type ID}"><ObjectName>Field8</ObjectName>
<FormattedValue>DHC8</FormattedValue>
<Value>DHC8</Value>
</FormattedReportObject>
<FormattedReportObject xsi:type="CTFormattedField" Type="xsd:unsignedLong" FieldName="{TRIP LEGS.Trip Number}"><ObjectName>Field9</ObjectName>
<FormattedValue>68344</FormattedValue>
<Value>68344.00</Value>
</FormattedReportObject>
</FormattedReportObjects>
</FormattedSection>
</FormattedSections>
</FormattedArea>
</FormattedAreaPair>
<FormattedAreaPair Level="1" Type="Details">
<FormattedArea Type="Details">
<FormattedSections>
<FormattedSection SectionNumber="0">
<FormattedReportObjects>
<FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" FieldName="{AIRCRAFT.Tail Number}"><ObjectName>Field2</ObjectName>
<FormattedValue>C-FBCS</FormattedValue>
<Value>C-FBCS</Value>
</FormattedReportObject>
<FormattedReportObject xsi:type="CTFormattedField" Type="xsd:string" FieldName="{AIRCRAFT.Type ID}"><ObjectName>Field8</ObjectName>
<FormattedValue>DHC8</FormattedValue>
<Value>DHC8</Value>
</FormattedReportObject>
<FormattedReportObject xsi:type="CTFormattedField" Type="xsd:unsignedLong" FieldName="{TRIP LEGS.Trip Number}"><ObjectName>Field9</ObjectName>
<FormattedValue>68344</FormattedValue>
<Value>68344.00</Value>
</FormattedReportObject>
</FormattedReportObjects>
</FormattedSection>
</FormattedSections>
</FormattedArea>
</FormattedAreaPair>
...
</FormattedAreaPair>
</FormattedReport>

我正在尝试使用 LINQ 到 XML 查询来提取基于父节点的 FieldName 属性的值节点,并将其放入对象中。值或格式化报表对象节点的父级没有唯一属性。 到目前为止,这是我

的代码
from fs in xDoc.Descendants("FormattedSection")
select new FlightSchedule
{
  AircraftType = from fos in fs.Descendants("FormattedReportObjects")
                 from fo in fs.Descendants("FormattedReportObject")
                 where fo.Attribute("FieldName").Value.Equals("{AIRCRAFT.Type ID}")
                 from e in fo.Element("Value")
                 select e.Value),
  ....
};

我不断收到错误:

在源类型为"System.Collections.Generic.IEnumerable"的查询表达式的后续 from 子句中不允许使用类型为 'System.Xml.Linq.XElement' 的表达式。类型推断在调用"选择多个"时失败)

或者如果我没有收到错误,我最终什么也没检索到。 任何关于改进我的查询的建议将不胜感激。

根据父节点的属性获取子节点

您的代码有几个问题。首先,编译器抱怨的是,正如@MizardX提到的,你正在使用fo.Element("Value"),就好像它是一个序列一样。您可能想要的是写let e = fo.Element("Value")(或者完全跳过这部分,直接写select fo.Element("Value").Value)。

另一个问题是您的 XML 正在使用命名空间,但您没有。这意味着您应该创建一个XNamespace对象,并在任何有元素名称的地方使用它。

此外,代码的编写方式AircraftType是字符串序列。我想这不是你想要的。

看到你想对不同的FieldName值做同样的事情,你可能想把它变成一种方法。

修复上述所有问题后,代码应如下所示:

static readonly XNamespace ns = XNamespace.Get("urn:crystal-reports:schemas");
string GetFieldValue(XElement fs, string fieldName)
{
    return (from fo in fs.Descendants(ns + "FormattedReportObject")
            where fo.Attribute("FieldName").Value == fieldName
            let e = fo.Element(ns + "Value")
            select e.Value).Single();
}
…
var flts = (from fs in xDoc.Descendants(ns + "FormattedSection")
            select new FlightSchedule
            {
                AircraftType = GetFieldValue(fs, "{AIRCRAFT.Type ID}"),
                …
            }).ToList();

fo.Element("Value")返回一个XElement对象。你想要的可能是fo.Elements("Value")(注意复数"s")。

错误消息抱怨它不知道如何迭代XElement对象。

您没有得到任何结果的原因是 XML 文件正在使用命名空间。若要查找默认命名空间之外的元素,需要在节点名称之前为命名空间添加前缀。

我还注意到您没有使用 fos 变量,因此不需要循环。 fs.Decendants()已经给了你正确的结果。

List<FlightSchedule> flts =
    (from fs in xDoc.Descendants("{urn:crystal-reports:schemas}FormattedSection")
     select new FlightSchedule
     {
         AircraftType =
             (from fo in fs.Descendants("{urn:crystal-reports:schemas}FormattedReportObject")
              where fo.Attribute("FieldName").Value == "{AIRCRAFT.Type ID}"
              from e in fo.Elements("{urn:crystal-reports:schemas}Value")
              select e.Value),
                          ....
     }).ToList();