Search code examples
c#.netxmlxml-parsingascx

How to display XML string on the ascx page in a grid


I have a object that fetched the xml data stored in db say dataset.xmlcontent . I want to display all the xmlcontent on the UI , this is a string but because of the xml tags embedded in the data it will not be able to parse the data .

Any input how should I proceed??

code in the model fetches the information here :

dataSet=orderRepo.GetOrderItemDatas().Where(o => orderItem.Contains(o.OrderItemId)).Select(o=>o).ToList();

dataSet.Xmlcontent has the XML data stored as string , I am trying to display this on the UI

code in the ascx page :

            <tr>
                <th class="ui-state-default">TN</th>
                <th class="ui-state-default">Provisioning Data</th>
            </tr>
            <%
    if (Model.orderdataExist)
    {
        foreach (SomeObject detail in Model.dataSet)
        {
            %>
            <tr >
                <td><%= detail.TN %></td>
                <td><%= detail.Xmlcontent.ToString() %></td>

            </tr>
            <%
        }
    }
            %>

        </table> 

the output on the UI for this is :

XML content on UI

How can I parse this XML content to show on the UI ??


Solution

  • Put into a datatable and then you can bind the table to your page

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                string xmlResponseFromDb =  "<?xml version=\"1.0\"?>" +
                                                "<Table>" +
                                                   "<DataRow>" +
                                                        "<TN>6462308635</TN>" +
                                                        "<Provision_Data>6462308635services_options_cosmo_callrecordingasc840</Provision_Data>" +
                                                   "</DataRow>" +
                                                   "<DataRow>" +
                                                        "<TN>6462308635</TN>" +
                                                        "<Provision_Data>6462308635services_options_cosmo_callrecordingascspeechanalytics840</Provision_Data>" +
                                                   "</DataRow>" +
                                                "</Table>";
    
                XDocument doc = XDocument.Parse(xmlResponseFromDb);
    
                DataTable dt = new DataTable();
                dt.Columns.Add("TN", typeof(string));
                dt.Columns.Add("Provision_Data", typeof(string));
    
                foreach (XElement row in doc.Descendants("DataRow"))
                {
                    dt.Rows.Add(new object[] { (string)row.Element("TN"), (string)row.Element("Provision_Data") });
                }
    
            }
        }
    
    }