0

嗨,任何人都可以帮助进行 linq 查询,通常我会像这样从客户端填充我的数据网格,以获取对 web 服务的 GET 请求:

    {
        string uri = "http://localhost:8002/Service/Customer";
        XDocument xDoc = XDocument.Load(uri);
        var customer = xDoc.Descendants("Customer")
            .Select(n => new
            {
                CustomerID = n.Element("CustomerID").Value,
                Firstname = n.Element("FirstName").Value,
                Surname = n.Element("LastName").Value,
                Age = n.Element("Age").Value,
                //Time = DateTime.Parse(n.Element("TimeAdded").Value)
            })
            .ToList();

        dataGrid1.ItemsSource = customer;
    }

哪个工作正常,但现在我已将客户链接到雇用日期,我的 xml 看起来像这样:

<ArrayOfCustomer>
<Customer>
<CustomerID>1</CustomerID>
<FirstName>G</FirstName>
<LastName>Graam</LastName>
<Age>27</Age>
      <CustomerHireDate>
      <HireDate>
      <HireFromDate>15.07.2012</HireFromDate>
      <HireToDate>29.07.2012</HireToDate>
      </HireDate>
      </CustomerHireDate>
</Customer>
</ArrayOfCustomer>

到目前为止,我坚持以下尝试用后代的后代填充数据网格的方法:

            string uriShowCarHires = "http://localhost:8002/Service/Customer/{anything}";
            string Uri = uriShowCarHires.Replace("{anything}", textBox1.Text);
            XDocument xDoc = XDocument.Load(Uri);
            foreach (var node in xDoc.Descendants("Customer"))
            {

              \\..... how do you get the descendants of descendants for each n.element?
            }

我不确定这是否会按照我的想法填充数据网格,我希望避免在其中一个数据网格单元格中获取“数组”。我正在寻找这样的输出:

Name etc | HireFromDate | HireToDate
G           09.12.2012    01.01.2013

如果有人可以帮助将不胜感激谢谢

4

1 回答 1

1

我认为您正在寻找这样的东西(它不是最有效的,但它应该可以工作):

var customer = xDoc.Descendants("Customer")
        .Select(n => new
        {
            Firstname = n.Element("FirstName").Value,
            Surname = n.Element("LastName").Value,
            HireFromDate = n.Element("CustomerHireDate")
                            .Descendents("HireDate").First()
                            .Descendents("HireFromDate").First().Value,
            HireFromDate = n.Element("CustomerHireDate")
                            .Descendents("HireDate").First()
                            .Descendents("HireToDate").First().Value
        })
        .ToList();
于 2012-07-26T21:04:10.297 回答