2015-04-17 48 views
-1

工程建筑: 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; 
} 
+0

服务的响应似乎是在JSON这是奇怪的,因为你已经定义了ResponseFormat Xml。信息是最新的吗? –

+0

对不起Jérémie,我编辑了回复 – Newbie2015

回答

0

可以使用的datatabledatasetReadXml method那么,网格绑定到它。

public DataTable ReadXml(string xmlString) 
{ 
    // Create the DataTable 
    DataTable dt = new DataTable(); 
    try 
    { 
     //Create the columns with coresponding names 
     dt.Columns.Add("CarId", typeOf(int)); 
     dt.Columns.Add("Make" typeOf(string)); 
     dt.Columns.Add("Model" typeOf(string)); 
     dt.Columns.Add("Year" typeOf(int)); 

     //Using ReadXml to populate the table 
     dt.ReadXml(xmlString); 

     //Return the table 
     return dt; 
    } 
    catch (Exception ex) 
    { 
     //throw some exception 
    } 
} 

您可以在下面的链接找到更多这方面的资料:

DataTable.ReadXml Method

DataSet.ReadXml Method

+0

谢谢Tiberiu,现在就试试 – Newbie2015

+0

为了让答案更好,你应该提供更多的内容而不仅仅是链接。请牢记未来。 –

+0

你说得对。我编辑过它,希望现在好一点。感谢您的反馈。 –