在我的应用程序中,我有一个WCF REST服务,它可以从我的Silverlight客户端进行调用。消费WCF服务时的问题
private void btnGetEmployees_Click(object sender, RoutedEventArgs e)
{
try
{
WebClient wClient = new WebClient();
wClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wClient_OpenReadCompleted);
wClient.DownloadStringAsync(new Uri("http://localhost/DummyService/Service.svc/EmpRest", UriKind.Absolute));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void wClient_OpenReadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XDocument xdStudent = XDocument.Parse(e.Result);
var Result = (from emp in xdStudent.Descendants("Employee")
select new Employee
{
EmpNo = emp.Element("EmpNo").Value,
EmpName = emp.Element("EmpName").Value
}
).ToList();
dgData.ItemsSource = Result;
}
我能够从e.Result获得POX结果。以下是示例结果
<ArrayOfEmployee xmlns="http://schemas.datacontract.org/2004/07/WCF_REST_Service" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Employee>
<EmpName>Emp_1</EmpName>
<EmpNo>101</EmpNo>
</Employee>
<Employee>
<EmpName>Emp_2</EmpName>
<EmpNo>102</EmpNo>
</Employee>
<Employee>
<EmpName>Emp_3</EmpName>
<EmpNo>103</EmpNo>
</Employee>
<Employee>
<EmpName>Emp_4</EmpName>
<EmpNo>104</EmpNo>
</Employee>
<Employee>
<EmpName>Emp_5</EmpName>
<EmpNo>105</EmpNo>
</Employee>
</ArrayOfEmployee>
但是,当我使用LINQ查询XDocument时,我没有收到结果。为了测试目的,我已经手动加载了XDocument(不是来自服务),并且能够获取值。
string xml = @"
<ArrayOfEmployee >
<Employee>
<EmpName>Emp_1</EmpName>
<EmpNo>101</EmpNo>
</Employee>
<Employee>
<EmpName>Emp_2</EmpName>
<EmpNo>102</EmpNo>
</Employee>
<Employee>
<EmpName>Emp_3</EmpName>
<EmpNo>103</EmpNo>
</Employee>
<Employee>
<EmpName>Emp_4</EmpName>
<EmpNo>104</EmpNo>
</Employee>
<Employee>
<EmpName>Emp_5</EmpName>
<EmpNo>105</EmpNo>
</Employee>
</ArrayOfEmployee>";
XDocument xdStudent = XDocument.Parse(xml);
我所做的唯一的变化是从根标签
xmlns="http://schemas.datacontract.org/2004/07/WCF_REST_Service" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
移除的属性,我认为这些属性提高分析问题,当我查询使用LINQ中的XDocument。
感谢faester您的善意回复,我所做的更改什么ü建议,但我仍无法得到的数据,我已经作出更改下方,的XDocument xdStudent =的XDocument。解析(e.Result); XNamespace ns =“http://schemas.datacontract.org/2004/07/WCF_REST_Service/”;我修改了linq查询以及EmpNo = emp.Element(ns +“EmpNo”)。Value,EmpName = emp.Element(ns +“EmpName”)。Value –