XDocument.Descendants() not returning any elements

痞子三分冷 提交于 2019-11-26 16:59:21

问题


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">
  <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!


回答1:


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)

...



回答2:


You are not including the namespace from the parent element:

XNameSpace ns = "http://schemas.datacontract.org/2004/07/Employees.Entities";
foreach (XElement element in xdoc.Descendants(ns + "Employee")
{
    ...
}


来源:https://stackoverflow.com/questions/604680/xdocument-descendants-not-returning-any-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!