How to display xml returned from server in a DataGridView



Project architecture: WindowsForm - WCF Service - SQLite database


Aim: I want to return a requested cars' details from the database, through the server (using REST) and onto a DataGridView on the client side.


Problem: At present I'm returning an XML string to a text box which includes the details I want...


{"GetCarByIdResult":" </xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element> </xs:schema> 2</CarID>Toyota</Make>Corolla</Model>2007</Year></Car2></DocumentElement></diffgr:diffgram></DataTable>"}


but I can't find a way to put this information into the DataGridView. I've tried XMLReader, XDocument, dgCar.DataBind(); without luck. I'm very new to this so any pointers greatly appreciated. Find my code below. Thanks in advance.


Server side code:


[OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedResponse , UriTemplate = "/GetCarById/{value}")] DataTable GetCarById(string value);


//////////////////////////////////////


public DataTable GetCarById(string value) { string connectionPath = @"Data Source=C:\SQLite\Car2.sqlite;Version=3;"; SQLiteConnection connection = new SQLiteConnection(connectionPath); { connection.Open();



string SQL = "SELECT * FROM Car2 where CarID =" + value;
SQLiteCommand cmd = new SQLiteCommand();

cmd.Connection = connection;
cmd.CommandText = SQL;
cmd.ExecuteNonQuery();

SQLiteDataAdapter adapter = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable("Car2");
adapter.Fill(dt);

return dt;

connection.Close();


Client side code:


private void btnGetCar_Click(object sender, EventArgs e) { HttpClient client = new HttpClient(); HttpResponseMessage wcfResponse = client.GetAsync("http://localhost:56328/Service1.svc/GetCarById/" + txtShowCarById.Text).Result; HttpContent stream = wcfResponse.Content; var data = stream.ReadAsStringAsync();



txtRest.Text = data.Result;
}

No comments:

Post a Comment