c# AXL XElement Data to datagridview

579 views Asked by At

I'm busy with a application that extract data from Cisco CUCM to Datagridview with AXL/SOAP.

I get only the last record in the datagridview if I put the info to a combobox I get the compleet list. The info what I extract with AXL is: SEP0014A815DB0D sk0000101 SEP0022555E7E26 AKijkindevegte SEP0018BAC8C6C5 BT101139

This is my code:

            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());
                    }

Please Help Whats do I wrong.

1

There are 1 answers

7
Charles Mager On BEST ANSWER

You're creating a new DataTable for each element and assigning this to your DataGridView.DataSource. Predictably, this will result in setting a source containing the last item.

Move the creation of your DataTable outside the loop and add the rows within it. Then assign your DataSource once after you've finished populating it.

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;