任何人都可以帮助查询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?
}
我不确定这是否会以我想的方式填充数据网格,我希望避免在其中一个datagrids单元格中获得“数组”。我正在寻找像这样的输出abit:
Name etc | HireFromDate | HireToDate
G 09.12.2012 01.01.2013
如果有人能提供帮助,将不胜感激
答案 0 :(得分: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();