0

In my application, I have a WCF REST service which makes 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 of removing 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 these attributes are raising the parsing issue when I am querying the XDocument using LINQ.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Thanks faester for your kind reply, I have made the changes what u suggested , but still i am not able to get the data, I have made below changes, XDocument xdStudent = XDocument.Parse(e.Result); XNamespace ns = "http://schemas.datacontract.org/2004/07/WCF_REST_Service/"; I have modified the linq query as well EmpNo = emp.Element(ns+"EmpNo").Value,EmpName = emp.Element(ns + "EmpName").Value – HariPrasad Katakam Dec 28 '12 at 08:59

1 Answers1

0

Your problem is unrelated to WCF and only concerns your XML parsing. The "attributes" you've removed in your test example is the namespaces for the document, and parsing needs the namespaces to identify nodes. In your test case you asks Linq to parse elements without a namespace, rather than elements with the fully qualified names

http://schemas.datacontract.org/2004/07/WCF_REST_Service:Employee

so your test is strictly speaking completely different from your life scenario.

Take a look here at this question about Linq to XML

Community
  • 1
  • 1
faester
  • 14,886
  • 5
  • 45
  • 56
  • Thanks a lot faester,as suggested I have added the XNamespace as wel. But now i am receiving "Object reference not set to an instance of object" error. :( As per my knowledge when consuming WCF Rest service not need to add proxy class, please let me know i would wrong. – HariPrasad Katakam Dec 28 '12 at 10:11
  • Hope you have solved it already, but could you provide a stacktracke for the exception? – faester Jan 02 '13 at 20:25