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

发布于 2024-11-06 05:35:06 字数 3426 浏览 0 评论 0原文

我有一个 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 to XML 查询来提取 基于父节点的 FieldName 属性的 Value 节点并将它们放入一个对象中。 Value 或 FormattedReportObject 节点的父节点没有唯一的属性。到目前为止,这是我的代码,因此

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”类型的表达式。调用“SelectMany”时类型推断失败)

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

I have a CrystalReport report in XML (sorry for the verboseness, I cut out most sample data)

<?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>

I am attempting to use a LINQ to XML query to extract the
Value node based on the parent node's FieldName attribute and place those into an object. There is no unique attribute for Value or the parents of FormattedReportObject nodes. So far here is my code to do so

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),
  ....
};

I keep getting errors:

An expression of type 'System.Xml.Linq.XElement' is not allowed in a subsequent from clause in a query expression with source type 'System.Collections.Generic.IEnumerable'. Type inference failed in the call to 'SelectMany')

or if I don't get an error I end up retrieving nothing. Any suggestions would be greatly appreciated on improving my query.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

清晰传感 2024-11-13 05:35:06

您的代码有几个问题。首先,编译器抱怨的事情是,正如 @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();

Your code has several problems. First, the thing the compiler is complaining about is, as @MizardX mentioned, that you are using fo.Element("Value") as if it was a sequence. What you probably want is to write let e = fo.Element("Value") (or skip this part completely and directly write select fo.Element("Value").Value).

Another problem is that your XML is using a namespace, but you aren't. This means that you should create a XNamespace object and use it wherever you have element names.

Also, the way your code is written, AircraftType is a sequence of strings. I assume this is not what you wanted.

And seeing that you want to do the same thing for different values of FieldName, you probably want to make this into a method.

With all the problems mentioned above fixed, the code should look something like this:

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();
她比我温柔 2024-11-13 05:35:06

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();

fo.Element("Value") returns an XElement-object. What you want is probably fo.Elements("Value") (note the plural 's').

The error message was complaining that it didn't know how to iterate over the XElement object.

The reason you are not getting any results, is that the XML-file is using namespaces. To find elements outside the default namespace, you need to prefix the namespace before the node name.

I also noticed that you are not using the fos variable, so that loop is unnecessary. fs.Decendants() is already giving you the correct result.

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文