XML String in DataGridView

I am currently working with a web service to get a report of users on a remote support system.

Pulling my report and getting the result, the following line is returned to me:

<report>
    <header>
        <field id="0">Source</field>
        <field id="1">Session ID</field>
        <field id="2">Date</field>
        <field id="3">Name</field>
        <field id="24">Technician Name</field>
        <field id="25">Technician ID</field>
    </header>
    <data>
        <row>
            <field id="0">Email</field>
            <field id="1">55037806</field>
            <field id="2">4/13/2010 2:28:06 AM</field>
            <field id="3">Bill Gates</field>
            <field id="24">John</field>
            <field id="25">1821852</field>
        </row>
        <row>
            <field id="0">Telephone</field>
            <field id="1">55034548</field>
            <field id="2">4/13/2010 12:59:44 AM</field>
            <field id="3">Steve Jobs</field>
            <field id="24">John</field>
            <field id="25">1821852</field>
        </row>
    </data>
</report>

      

After getting this row, I need to take it and display the actual data in the datagridview. I tried to put it in XMLDocument and then read this, but it doesn't seem to work. Just interested in a different set of eyes :) Application is written in C # in VS2010.

+2


a source to share


1 answer


You can use Linq for XML to create DataTable

with column names from an XML file and then set that table like DataSource

DataGridView

:

        string xml = theWebService.TheMethod();
        XDocument doc = XDocument.Parse(xml);
        var queryHeaders =
            from field in doc.Root.Element("header").Elements("field")
            select new
            {
                Id = field.Attribute("id").Value,
                Name = field.Value
            };

        var headers = queryHeaders.ToDictionary(f => f.Id, f => f.Name);

        DataTable table = new DataTable();
        foreach (var kvp in headers)
        {
            table.Columns.Add(kvp.Value);
        }

        Func<XElement, DataRow> rowGenerator =
            element =>
            {
                var row = table.NewRow();
                foreach (var field in element.Elements("field"))
                {
                    string fieldId = field.Attribute("id").Value;
                    string columnName = headers[fieldId];
                    string value = field.Value;
                    row[columnName] = value;
                }
                return row;
            };

        var rows =
            from rowElement in doc.Root.Element("data").Elements("row")
            select rowGenerator(rowElement);

        foreach (var row in rows)
        {
            table.Rows.Add(row);
        }

        dataGridView.AutoGenerateColumns = true;
        dataGridView.DataSource = table;

      



Please note that the above code only works if all fields have different names. If it is not, you can use the field id as name DataColumn

and change the DGV headers with the actual field names

+2


a source







All Articles