使用WCF Rest服务时出现问题 [英] Problem When consuming WCF Rest service

查看:123
本文介绍了使用WCF Rest服务时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中我有WCF Rest服务,我正在从我的silverlight客户端拨打电话。

In My application I Have WCF Rest serivce and i am making call from my silverlight client.

 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结果。以下是示例结果



I am able to get the POX result from e.Result . Below is sample results

<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(不是来自服务),如下所示,并且能够获得值。





But When I am Querying the XDocument Using LINQ I am Not receiving the result. I for testing purpose i have loaded the XDocument manually (Not from the service) as below and able to get values.

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





我做的唯一更改是。我已从根标签中删除了属性





The only change i made is .I have removed the attributes from the root tag

xmlns="http://schemas.datacontract.org/2004/07/WCF_REST_Service" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"





我认为当我使用Linq查询XDoument时,这些属性导致解析问题。



请帮助我预先感谢



热烈的问候,

Hari。



I think thiese attributes are causing the parsing issue when i am querying the XDoument Using Linq.

Please help me out. Thanks in Advance

Warm regards,
Hari.

推荐答案

void wClient_OpenReadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
   XNamespace ns = "http://schemas.datacontract.org/2004/07/WCF_REST_Service";
   XDocument xdStudent = XDocument.Parse(e.Result);
   var Result = (from emp in xdStudent.Descendants(ns +"Employee")
                              select new Employee
                              {
                                 EmpNo = emp.Element("EmpNo").Value,
                                 EmpName = emp.Element("EmpName").Value
                              }
                              ).ToList();

                dgData.ItemsSource = Result;
}


这篇关于使用WCF Rest服务时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆