0

我需要在每个查询中列出 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>
4

2 回答 2

1

这应该有效:

    public void Test()
    {
        var inquiries = XElement.Load("Test.xml").Elements("Inquiry")
            .Select(i => new Inquiry
                        {
                            InquiryId = int.Parse(i.Element("InquiryId").Value),
                            FirstName = i.Element("FirstName").Value,
                            LastName = i.Element("LastName").Value,
                            LineItems = i.Elements("LineItems").Descendants()
                                .Select(li => new LineItem
                                    {
                                        Quantity = int.Parse(li.Attribute("quantity").Value),
                                        PartNumber = li.Attribute("partnumber").Value
                                    }).ToList()
                        }).ToList();
    }
}

public class Inquiry
{
    public int InquiryId { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public List<LineItem> LineItems { get; set; }
}

public class LineItem
{
    public int Quantity { get; set; }

    public string PartNumber { get; set; }
}
于 2012-11-01T21:02:59.507 回答
1
XElement myElement = XElement.Load("Inquiries.xml");
var query = from s in myElement.Elements("Inquiry")
            select new
            {
                InquiryId = Convert.ToInt32(s.Element("InquiryId").Value),
                FirstName = s.Element("FirstName").Value,
                LastName = s.Element("LastName").Value,
                LineItems = (from l in s.Element("LineItems").Elements("LineItem")
                            select new
                            {
                                Quantity = Convert.ToInt32(l.Attribute("quantity").Value),
                                PartNumber = l.Attribute("partnumber").Value
                            }).ToList()

            };

var result = query.ToList();
于 2012-11-01T21:44:25.657 回答