我需要在每个查询中列出 lineitems。下面我只得到查询的值,而不是它的项目列表。
XElement myElement = XElement.Load("Inquiries.xml");
var query = from s in myElement.Elements("Inquiry")
select new
{
InquiryId = (int)s.Element("InquiryId"),
FirstName = (string)s.Element("FirstName"),
LastName = (string)s.Element("LastName"),
LineItems = from l in myElement.Elements("Inquiry").Elements("LineItems")
select new
{
Quantity = (int)l.Attribute("quantity"),
PartNumber = (string)l.Attribute("partnumber")
}
};
var result = query.ToList();
XML:
<Inquiries>
<Inquiry>
<InquiryId>0</InquiryId>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<LineItems>
<LineItem quantity="2" partnumber="abc"></LineItem>
<LineItem quantity="3" partnumber="abc"></LineItem>
<LineItem quantity="6" partnumber="abc"></LineItem>
</LineItems>
</Inquiry>
<Inquiry>
<InquiryId>1</InquiryId>
<FirstName>Epic</FirstName>
<LastName>Beardman</LastName>
<LineItems>
<LineItem quantity="2" partnumber="abc"></LineItem>
<LineItem quantity="3" partnumber="abc"></LineItem>
<LineItem quantity="6" partnumber="abc"></LineItem>
</LineItems>
</Inquiry>
</Inquiries>