Click here to Skip to main content
Click here to Skip to main content

Converting XML string to HTML Table

, 19 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes about converting an XML string to a HTML table.

Introduction

Many web developers come across the scenario of displaying XML content on a webpage. And sometimes its nice to present it in a tabular format rather than the raw XML view. Below is a small recursive function which can help you converting XML string to a HTML Table string.

Using the code 

This function takes a string (XML content) as input and returns a string (HTML Table). Once we have the output string with the HTML content, it's easy to render it on a web page. For example, we can simply assign the output string to a label on the web page to have the tabular view. 

protected string ConvertXmlToHtmlTable(string xml)
{
  StringBuilder html = new StringBuilder("<table align='center' " + 
     "border='1' class='xmlTable'>\r\n");
  try
  {
      XDocument xDocument = XDocument.Parse(xml);
      XElement root = xDocument.Root;

      var xmlAttributeCollection = root.Elements().Attributes();


      foreach (var ele in root.Elements())
      {
          if (!ele.HasElements)
          {
              string elename = "";
              html.Append("<tr>");

              elename = ele.Name.ToString();

              if (ele.HasAttributes)
              {
                  IEnumerable<XAttribute> attribs = ele.Attributes();
                  foreach (XAttribute attrib in attribs)
                  elename += Environment.NewLine + attrib.Name.ToString() + 
                    "=" + attrib.Value.ToString();
              }

              html.Append("<td>" + elename + "</td>");
              html.Append("<td>" + ele.Value + "</td>");
              html.Append("</tr>");
          }
          else
          {
              string elename = "";
              html.Append("<tr>");

              elename = ele.Name.ToString();

              if (ele.HasAttributes)
              {
                  IEnumerable<XAttribute> attribs = ele.Attributes();
                  foreach (XAttribute attrib in attribs)
                  elename += Environment.NewLine + attrib.Name.ToString() + "=" + attrib.Value.ToString();
              }

              html.Append("<td>" + elename + "</td>");
              html.Append("<td>" + ConvertXmlToHtmlTable(ele.ToString()) + "</td>");
              html.Append("</tr>");
          }
      }

      html.Append("</table>");
  }
  catch (Exception e)
  {
      return xml;
      // Returning the original string incase of error.
  }
  return html.ToString();
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rajesh Kuramdasu
Software Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionWhy? Pinmembercrb900021-Dec-12 4:39 
AnswerRe: Why? PinmemberRajesh Kuramdasu21-Dec-12 5:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 19 Dec 2012
Article Copyright 2012 by Rajesh Kuramdasu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid