views:

2906

answers:

1

I'm trying to bind a Silverlight DataGrid to the results of a WCF service call. I was not seeing the data displayed in the grid, so when I ran through the debugger, I notice that XDocument.Descendants() was not returning any elements even when I was passing in a valid element name. Here is the XML that is passed back from the service:

<ArrayOfEmployee xmlns="http://schemas.datacontract.org/2004/07/Employees.Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
  <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>

And here is the method I use to load the results into a collection of anonymous objects, using Linq to XMl, and then bind the collection to the grid.

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;
}

Any idea why xml.Descendants("Employee") doesn't return anything?

Thanks!

+14  A: 

The string parameter passed to Descendents is actually implicitly converted to an XName object. An XName represents a fully qualified element name.

The document defines a namespace "i", therefore I believe you need to use the fully qualified name to access Employee. ie. i:Employee, where the prefix "i: actually resolves to the full namespace string.

Have you tried something like:

XName qualifiedName = XName.Get("Employee", "http://www.w3.org/2001/XMLSchema-instance");

var employees = from e in xml.Descendants(qualifiedName)

...
Ash
You were right, I needed to include the namespace. Thanks for the help!
Kevin Babcock