Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

silverlight - XDocument.Descendants() not returning any elements

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...