根据父节点的属性获取子节点
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码有几个问题。首先,编译器抱怨的事情是,正如 @MizardX 提到的,您使用 fo.Element("Value") 就好像它是一个序列一样。您可能想要的是编写
let e = fo.Element("Value")
(或者完全跳过这部分并直接编写select fo.Element("Value").Value)。
另一个问题是您的 XML 使用了命名空间,但您没有。这意味着您应该创建一个
XNamespace
对象并在有元素名称的任何地方使用它。另外,按照代码的编写方式,
AircraftType
是一个字符串序列。我想这不是你想要的。看到您想要对
FieldName
的不同值执行相同的操作,您可能希望将其放入一个方法中。解决了上述所有问题后,代码应如下所示:
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 writelet e = fo.Element("Value")
(or skip this part completely and directly writeselect 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:
fo.Element("Value")
返回一个XElement
对象。您想要的可能是fo.Elements("Value")
(注意复数“s”)。错误消息抱怨它不知道如何迭代
XElement
对象。您没有得到任何结果的原因是 XML 文件正在使用命名空间。要查找默认命名空间之外的元素,需要在节点名称之前添加命名空间前缀。
我还注意到您没有使用 fos 变量,因此该循环是不必要的。
fs.Decendants()
已经为您提供了正确的结果。fo.Element("Value")
returns anXElement
-object. What you want is probablyfo.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.