c# AXL XElement 数据到 datagridview

c# AXL XElement Data to datagridview

我正忙于使用 AXL/SOAP 将数据从 Cisco CUCM 提取到 Datagridview 的应用程序。

如果我将信息放入组合框,我只会得到 datagridview 中的最后一条记录,我会得到完整的列表。 我用 AXL 提取的信息是: SEP0014A815DB0D sk0000101 SEP0022555E7E26 AKijkindevegte SEP0018BAC8C6C5 BT101139

这是我的代码:

            byte[] soapBytes = Encoding.UTF8.GetBytes(soap);

            ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
            HttpWebRequest httpRQ = (HttpWebRequest)HttpWebRequest.Create(string.Format(@"https://XXX.XXX.XXX.XXX:8443/axl/"));

            httpRQ.ProtocolVersion = System.Net.HttpVersion.Version10;
            httpRQ.Credentials = new NetworkCredential("user1", "password");//Callmanager gebruikersnaam / password
            httpRQ.Method = "POST";
            httpRQ.ContentType = "text/xml; charset=utf-8";
            httpRQ.Accept = "text/xml";
            httpRQ.Headers.Add("SOAPAction: 'CUCM:DB ver=" + version + "'");
            httpRQ.ContentLength = soapBytes.Length;

            //Send the xml soap to cucm
            Stream stm = httpRQ.GetRequestStream();
            stm.Write(soapBytes, 0, soapBytes.Length);
            stm.Close();

            //Build the xml response
            XDocument responcedoc = new XDocument();
            HttpWebResponse responce = httpRQ.GetResponse() as HttpWebResponse;
            Stream responcedata = responce.GetResponseStream();
            StreamReader responsereader = new StreamReader(responcedata);
            Logging.Text += "\n---------|AXL Response|---------\n\n";
            XDocument respdoc = XDocument.Load(responsereader);
            Logging.Text += respdoc + "\n";
            soap = null;

            //fill in the combo  

            DevicePhone.Items.Clear();




            foreach (XElement item in respdoc.Descendants("name"))

            {
               // DataSet ds = new DataSet();
                //ds.ReadXml((string)item);

                DevicePhone.Items.Add((string)item);
                string ds;


                    strDevicePhone = ((string)item);
                    ds = ((string)item);
                    label3.Text = strDevicePhone;

                    try
                    {


                        DataTable jmn = new DataTable("respdoc");

                        dataGridView1.DataSource = jmn;

                        jmn.Columns.Add("name");
                        jmn.Columns.Add("userid");

                        jmn.Rows.Add((string)item);

                        return;
                    }
                    catch (Exception e)
                    {
                        //Interaction.MsgBox(ex.ToString());
                    }

请帮忙 我哪里错了。

您正在为每个元素创建一个新的 DataTable 并将其分配给您的 DataGridView.DataSource。可以预见,这将导致设置包含最后一项的源。

DataTable 的创建移到循环外并在其中添加行。然后在完成填充后分配一次 DataSource

var jmn = new DataTable("respdoc");
jmn.Columns.Add("name");
jmn.Columns.Add("userid");

foreach (var item in respdoc.Descendants("item"))
{
    // ...

    jmn.Rows.Add((string)item);

    // ...
}

dataGridView1.DataSource = jmn;