Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WCF LINQ
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;
}
 
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>
 
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"
 
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.
Posted 27-Dec-12 21:20pm
Comments
Oleksandr Kulchytskyi at 28-Dec-12 3:32am
   
Yep of course , they cause the parsing issue , because of you forget to add xml namespaces to your xdoc

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
}
  Permalink  
Comments
HariPrasad katakam at 28-Dec-12 5:09am
   
Thanks Oleksandr Kulchytskyi,
 
I have given the XNamespace.now i am receiving "Object reference not set to an instance of object" error. :( As per my knowledge when consuming rest service need not to add proxy class, please let me know i would wrong.
Oleksandr Kulchytskyi at 28-Dec-12 5:14am
   
Yep, of course you can get rid of creating proxy class...
One of the possible reasons why you cannot receive data it to need specify some additional argument in header request via web client.
 
Also i would reccomend to add aditional check point:
void wClient_OpenReadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if(e.Error!=null)
{
// log error state ....
return;
}
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;
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid