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.