Skip to main content
Home Forums Silverlight Programming Accessing Web Services with Silverlight Web Service to return XML data
8 replies. Latest Post by boston15 on March 9, 2009.
(0)
boston15
Member
5 points
14 Posts
03-09-2009 12:37 PM |
Hi,
I'm just trying my first web service... I've watched the web service tutorial but I'm still confused. I want to write a web service that will open up a an xml data file and return its contents. I can get this to work outside of a web service but really don't know how to go about doing it using a webservice. I need a web service because my file needs to reside on the server. Here is my code from my xaml.cs...which works but again, I need a web service. I'd like to return all the xml data and bind it to my silverlight datagrid.
DataGrid oGrid;
public Page()
{
InitializeComponent();
InitializeGrid();
}
public void InitializeGrid()
XDocument oDoc = XDocument.Load("Reports.xml");
var myData = from info in oDoc.Descendants("rReport")
select new fishingReport
Number = Convert.ToInt32(info.Element("rNumber").Value),
Date = Convert.ToString(info.Element("rDate").Value),
Time = Convert.ToString(info.Element("rTime").Value),
Clarity = Convert.ToString(info.Element("rClarity").Value),
Weather = Convert.ToString(info.Element("rWeather").Value),
Barometer = Convert.ToString(info.Element("rBarometer").Value),
Report = Convert.ToString(info.Element("Report").Value),
File = Convert.ToString(info.Element("rFile").Value),
Temp = Convert.ToInt32(info.Element("rTemp").Value)
};
oGrid = this.FindName("dg") as DataGrid;
dg.IsReadOnly = true;
oGrid.ItemsSource = myData;
Any help would be appreciated. I've looked for specific examples of doing this but have been unable to find any.
Thanks!
amyo
Contributor
3630 points
495 Posts
03-09-2009 1:06 PM |
You can use Silverlight enable WCF service:
In ASP.Net web Application project:
1. Add new item, Add silverlight enable WCF service
2.In service place an operation contract like below:
[OperationContract] public List<fishingReport> GetAllFishingReport() { XDocument oDoc = XDocument.Load(HttpContext.Current.Server.MapPath("Reports.xml")); var myData = (from info in oDoc.Descendants("rReport") select new fishingReport { Number = Convert.ToInt32(info.Element("rNumber").Value), Date = Convert.ToString(info.Element("rDate").Value), Time = Convert.ToString(info.Element("rTime").Value), Clarity = Convert.ToString(info.Element("rClarity").Value), Weather = Convert.ToString(info.Element("rWeather").Value), Barometer = Convert.ToString(info.Element("rBarometer").Value), Report = Convert.ToString(info.Element("Report").Value), File = Convert.ToString(info.Element("rFile").Value), Temp = Convert.ToInt32(info.Element("rTemp").Value) }).ToList(); return myData; }
3. Make you fishingReport serialized like below:
[DataContract] public class fishingReport { [DataMember] public int Number { get; set; } [DataMember] public string Date { get; set; } [DataMember] public string Time { get; set; } [DataMember] public string Clarity { get; set; } [DataMember] public string Weather { get; set; } [DataMember] public string Barometer { get; set; } [DataMember] public string Report { get; set; } [DataMember] public string File { get; set; } [DataMember] public int Temp { get; set; } }
In silverlight project:
1. Add service reference
2. Then use like below:
public Page() { InitializeComponent(); var myClient = new ServiceReference1.Service1Client(); myClient.GetAllFishingReportCompleted += new EventHandler(myClient_GetAllFishingReportCompleted); myClient.GetAllFishingReportAsync(); } void myClient_GetAllFishingReportCompleted(object sender, SAForumSolve.ServiceReference1.GetAllFishingReportCompletedEventArgs e) { myDataGrid.ItemsSource = e.Result; }
Hope this will help you.
Please Mark as Answer if this helps you.
metal
Participant
1243 points
262 Posts
03-09-2009 1:10 PM |
this is the code from the blind proxy I included with twitternice
as you can see it loads a url and writes it back but you can change it to load your file instead
/// <summary> /// return page requested or an empty string /// </summary> /// <param name="url"></param> /// <returns></returns> [WebMethod] public string httpGet(string url) { string result = ""; WebClient client = new WebClient(); client.Headers.Add("user-agent", "Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)"); Stream data = client.OpenRead(url); StreamReader reader = new StreamReader(data); result = reader.ReadToEnd(); data.Close(); reader.Close(); return result; }
03-09-2009 5:26 PM |
Amyo,
Thanks for your help. However, I'm getting an error trying to view the service in the browser.
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0106: The modifier 'public' is not valid for this itemSource Error: Line 11: { Line 12: [OperationContract] Line 13: public List<fishingReport> GetAllFishingReport(); Line 14: } Line 15:
Line 11: { Line 12: [OperationContract] Line 13: public List<fishingReport> GetAllFishingReport(); Line 14: } Line 15:
Is this because I have a public class in my website called fishingReport already?
Again, thanks for your help so far.
03-09-2009 6:25 PM |
Ok... I found my error there - I had called my List<fishingReport> public in my Interface.
Now I have a new error:
The remote server returned an error: Not Found -->
System.Net.WebException: The remote server returned an error: Not Found -->
It goes on and on like this.
Any ideas? I can view the service in the browser.
03-09-2009 8:05 PM |
I just realized I didn't put down the beginning statement in my error file:
Error: Sys.InvalidOperationException: ManagedRuntimeError #4004 in control #ctl00_ContentPlaceHolder1_Silverlight1: System.Reflection.TargetInvocationException. An exception occurred during the operation, making the result invalid. Check innerException for exception details. --> System.ServiceModel.CommunicationsException: The remote server returned an error: Not Found -->
Also, the grid flashes up for a moment then disappears... then the scripting error shows up.
Any ideas?
03-09-2009 10:48 PM |
You can debug GetAllFishingReport to check if it is called ?
If called then check if XML loded correctly?
03-09-2009 11:29 PM |
amyo: You can debug GetAllFishingReport to check if it is called ? If called then check if XML loded correctly?
GetAllFishingReport is getting called. However, the xml file doesn't load.
'System.Web.HttpContext.Current' is null.
03-09-2009 11:45 PM |
Found the error:
//XDocument oDoc = XDocument.Load(HttpContext.Current.Server.MapPath("Reports.xml"));
I changed the code below to make it open the file. It now works! Thanks for your help!!!
string fileLoc = AppDomain.CurrentDomain.BaseDirectory;
XDocument oDoc = XDocument.Load(fileLoc + "Reports.xml");