none
xml to html tables RRS feed

  • Question

  • I'm using xDocument to convert xml to an html table I can get the table but I have a nested xml document and it is failing to let me add an extra row for a multiple heading rows in table. 

    Like so

    header

    rowdata1 rowdata2 rowdata3

    rowdataa rowdatab rowdatac

    header2

    rowdata1 rowdata2 rowdata3

    rowdataa rowdatab rowdatac

    header3

    rowdata1 rowdata2 rowdata3

    rowdataa rowdatab rowdatac

    Instead what I get is

    header

    rowdata1 rowdata2 rowdata3

    rowdataa rowdatab rowdatac

    rowdata1 rowdata2 rowdata3

    rowdataa rowdatab rowdatac

    rowdata1 rowdata2 rowdata3

    rowdataa rowdatab rowdatac

    xml

    <Module name="foo">
      <Service id="1">
        <Name>Check_Service_Status_foo</Name>
       ....
      </Service>
      <Service id="2">
        <Name>Check_Service_Status_bar</Name>
        ...
      </Service>
      <Service id="3">
        <Name>thisthat</Name>
      </Service>
      ...
    </Module>
    <Module name="foo2">
      <Service id="4">
        <Name>Check_Service_Status_foo2</Name>
        ...
      </Service>
       ....
    </Module>

    Code c#

     foreach (var rootdescendant in xmlDoc.Descendants("Root"))
            {
                var modulename = rootdescendant.Element("Header").Attribute("name").Value;
                TableRow rwh = new TableRow();
                TableCell cellh = new TableCell();
                cellh.Text = modulename;
                rwh.Cells.Add(cellh);
                tbl.Controls.Add(rwh);
                foreach(var moddescendant in rootdescendant.Descendants("Module"))
                { 
                foreach (var descendant in moddescendant.Descendants("Service"))
                {
                    i++;
                    var server = descendant.Element("Name").Value;
                    TableRow rw = new TableRow();
                    TableCell cell = new TableCell();
                    cell.Text = server;
                    rw.Cells.Add(cell);
                }
            }
            }
            myph.Controls.Add(tbl);

    I've tried to trim this down to the basic elements. Hoping I did not trim to much.  Thank you in advance.

    Wednesday, March 13, 2019 1:03 AM

Answers

  • For anyone looking for something like this moving the code from the rootdescendents loop to the moddescendent code loop appears to have worked.
    • Marked as answer by nashken Wednesday, August 7, 2019 8:27 PM
    Wednesday, March 13, 2019 1:35 AM

All replies

  • For anyone looking for something like this moving the code from the rootdescendents loop to the moddescendent code loop appears to have worked.
    • Marked as answer by nashken Wednesday, August 7, 2019 8:27 PM
    Wednesday, March 13, 2019 1:35 AM
  • Hi nashken,

    Thank you for posting here.

    For your question, you could try the code below. Please note that it need correct format xml.

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

    For more details, please refer to the code project. I test the code, it works well. But the html table is not like the format you provided in description.

    https://www.codeproject.com/Tips/512500/Converting-XML-string-to-HTML-Table

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 13, 2019 4:21 AM
    Moderator