在 DataGridView 中显示 WebSerive 的 xml 个结果

Show xml result of WebSerive in DataGridView

我编写了一个应用程序来检查序列号历史记录 Windows 来自我公司的网络服务。 Web服务返回结果如下:

<NewDataSet>
<TestHistory>
    <StartTime>2021-01-09T17:37:38+07:00</StartTime>
    <StopTime>2021-01-09T17:37:42+07:00</StopTime>
    <TestStatus>P</TestStatus>
    <MachineName>HCMINGAOI06_B</MachineName>
    <Operator />
    <StepOrTestName>AOI_BSI</StepOrTestName>
    <FailureLabel />
    <FailureMessage />
</TestHistory>
</NewDataSet>

有人可以帮我显示datagirdview的结果吗? 暂时只能用richTextBox显示 下面是我使用的代码:

private void Check(object sender, EventArgs e)
{
    check();
}
public void check()
{
    string CustomerName = cbWorkcell.Text;
    string SerialNumber = txtserialnumber.Text;
    string Division = "";
    try
    {
        ServiceMES.MES_TISSoapClient b = new ServiceMES.MES_TISSoapClient();        
        string resultz = b.GetTestHistory(SerialNumber, CustomerName, Division);           
        ShowResult(CustomerName, SerialNumber, resultz);               
    }

    catch { }
}

private void ShowResult(string CustomerName, string SerialNumber, string resultz)
{
  
    this.Invoke(new MethodInvoker(delegate ()
    {
       
        richTextBox1.Text = "Serinumber:" + SerialNumber + "\r\nCustomerName:" + CustomerName + "\r\n" + resultz + "\r\n";
        lbSerial.Text = "Serial: " + SerialNumber;
        
    }));
}

您可以使用 DataSet.ReadXml 并在 DataGridView 中显示结果。

例如:

var xml = @"
<NewDataSet>
<TestHistory>
    <StartTime>2021-01-09T17:37:38+07:00</StartTime>
    <StopTime>2021-01-09T17:37:42+07:00</StopTime>
    <TestStatus>P</TestStatus>
    <MachineName>HCMINGAOI06_B</MachineName>
    <Operator />
    <StepOrTestName>AOI_BSI</StepOrTestName>
    <FailureLabel />
    <FailureMessage />
</TestHistory>
</NewDataSet>
";
using (var textReader = new StringReader(xml))
{
    var ds = new DataSet();
    ds.ReadXml(textReader);
    dataGridView1.DataSource = ds.Tables[0];
}