我正在尝试将 Silverlight DataGrid 绑定到 WCF 服务调用的结果。我没有看到网格中显示的数据,所以当我运行调试器时,我注意到 XDocument.Descendants() 没有返回任何元素,即使我传入了一个有效的元素名称。以下是从服务传回的 XML:
<ArrayOfEmployee xmlns="http://schemas.datacontract.org/2004/07/Employees.Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Employee>
<BirthDate>1953-09-02T00:00:00</BirthDate>
<EmployeeNumber>10001</EmployeeNumber>
<FirstName>Georgi</FirstName>
<Gender>M</Gender>
<HireDate>1986-06-26T00:00:00</HireDate>
<LastName>Facello</LastName>
</Employee>
<Employee>
<BirthDate>1964-06-02T00:00:00</BirthDate>
<EmployeeNumber>10002</EmployeeNumber>
<FirstName>Bezalel</FirstName>
<Gender>F</Gender>
<HireDate>1985-11-21T00:00:00</HireDate>
<LastName>Simmel</LastName>
</Employee>
</ArrayOfEmployee>
这是我用来将结果加载到匿名对象集合中的方法,使用 Linq to XMl,然后将集合绑定到网格。
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs args)
{
if (args.Error != null) return;
var xml = XDocument.Parse(args.Result);
var employees = from e in xml.Descendants("Employee")
select new
{
EmployeeNumber = e.Element("EmployeeNumber").Value,
FirstName = e.Element("FirstName").Value,
LastName = e.Element("LastName").Value,
Birthday = e.Element("BirthDate").Value
};
DataGrid.SelectedIndex = -1;
DataGrid.ItemsSource = employees;
}
知道为什么xml.Descendants("Employee")
不返回任何东西吗?
谢谢!