使用WCF服务时出现问题
在我的应用程序中,我有一个WCF REST服务,它从我的silverlight客户端进行调用使用WCF服务时出现问题,wcf,linq,Wcf,Linq,在我的应用程序中,我有一个WCF REST服务,它从我的silverlight客户端进行调用 private void btnGetEmployees_Click(object sender, RoutedEventArgs e) { try { WebClient wClient = new WebClient(); wClient.DownloadStringCompleted += new Downlo
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时,我认为这些属性引发了解析问题 您的问题与WCF无关,只与XML解析有关。在测试示例中删除的“属性”是文档的名称空间,解析需要名称空间来标识节点。在您的测试用例中,您要求Linq解析并没有名称空间的元素,而不是具有完全限定名的元素
http://schemas.datacontract.org/2004/07/WCF_REST_Service:Employee
所以严格来说,你的测试和你的生活完全不同
查看此处感谢faester的友好回复,我已经按照您的建议进行了更改,但仍然无法获得数据,我已经进行了以下更改,XDocument xdStudent=XDocument.Parse(e.Result);xns=“”;我还修改了linq查询EmpNo=emp.Element(ns+“EmpNo”).Value,EmpName=emp.Element(ns+“EmpName”).Value非常感谢,正如我建议的那样,我还添加了XNamespace。但现在我收到“对象引用未设置为对象实例”错误:(据我所知,在使用WCF Rest服务时不需要添加代理类,请告诉我我可能错了。希望您已经解决了它,但是您能为异常提供StackTrack吗?
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"