Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

c# - Show xml result of WebSerive in DataGridView

I write an application that checks serial number history with Windows From with my company web-service. The results returned from the Web-service are as follows:

<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>

Can someone help me to show the results of datagirdview? temporarily I can only display it with richTextBox Below is the code I use:

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 + "
CustomerName:" + CustomerName + "
" + resultz + "
";
        lbSerial.Text = "Serial: " + SerialNumber;
        
    }));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use DataSet.ReadXml and show the result in DataGridView.

For example:

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];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...