如何从XML加载子元素列表?

时间:2012-11-01 20:54:51

标签: c# xml list xelement

我需要在每个查询中列出lineitems。下面我只查询'值但不列出它的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>

2 个答案:

答案 0 :(得分: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; }
}

答案 1 :(得分: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();