工程建築: WindowsForm - WCF服務 - SQLite數據庫如何顯示XML從服務器在一個DataGridView返回
目的: 我想從數據庫返回請求車的詳細信息,通過服務器(使用REST)和客戶端的DataGridView。
問題: 目前我返回XML字符串到一個文本框,其中包括我想要的細節...
<GetCarByIdResponse xmlns="http://tempuri.org/"><GetCarByIdResult>
<xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Car2" msdata:UseCurrentLocale="true">
<xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="Car2"><xs:complexType><xs:sequence>
<xs:element name="CarID" type="xs:long" minOccurs="0"/><xs:element name="Make" type="xs:string" minOccurs="0"/>
<xs:element name="Model" type="xs:string" minOccurs="0"/><xs:element name="Year" type="xs:long" minOccurs="0"/>
</xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType> </xs:element></xs:schema>
<diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<DocumentElement xmlns=""><Car2 diffgr:id="Car21" msdata:rowOrder="0"> <CarID>2</CarID>
<Make>Toyota</Make><Model>Corolla</Model><Year>2007</Year></Car2> </DocumentElement></diffgr:diffgram></GetCarByIdResult></GetCarByIdResponse>
,但我不能找到一種方法來把這個信息到DataGridView中。我試過XMLReader
,XDocument
,dgCar.DataBind();
沒有運氣。我很新,所以任何指針非常讚賞。在下面找到我的代碼。提前致謝。
服務器端代碼:
[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();
客戶端代碼:
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;
}
服務的響應似乎是在JSON這是奇怪的,因爲你已經定義了ResponseFormat Xml。信息是最新的嗎? –
對不起Jérémie,我編輯了回覆 – Newbie2015