Click here to Skip to main content
15,561,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
XML
<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.

C#
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

XML
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
Comments
Oleksandr Kulchytskyi 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

C#
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;
}
 
Share this answer
 
Comments
HariPrasad katakam 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900