How to resolve the algorithm Create an HTML table step by step in the C# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Create an HTML table step by step in the C# programming language

Table of Contents

Problem Statement

Create an HTML table.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Create an HTML table step by step in the C# programming language

First source code:

  • It uses a StringBuilder to create an HTML table with 3 rows and 4 columns.
  • The header row contains the column headers "X", "Y", and "Z".
  • The body rows contain random numbers between 0 and 9999.

Second source code:

  • It uses LINQ to XML to create an XML representation of the same table.
  • The header row contains the column headers "X", "Y", and "Z".
  • The body rows contain the numbers 0 to 3.

Comparison:

  • Both source codes create a table with 3 rows and 4 columns.
  • The first source code uses a StringBuilder to create an HTML table, while the second source code uses LINQ to XML to create an XML representation of the table.
  • The first source code is simpler and easier to understand, while the second source code is more concise and uses LINQ to XML, which is a powerful tool for working with XML data.

Source code in the csharp programming language

using System;
using System.Text;

namespace prog
{
	class MainClass
	{		
		public static void Main (string[] args)
		{
			StringBuilder s = new StringBuilder();
			Random rnd = new Random();
			
			s.AppendLine("<table>");
			s.AppendLine("<thead align = \"right\">");
			s.Append("<tr><th></th>");
			for(int i=0; i<3; i++)
				s.Append("<td>" + "XYZ"[i] + "</td>");
			s.AppendLine("</tr>");
			s.AppendLine("</thead>");
			s.AppendLine("<tbody align = \"right\">");
			for( int i=0; i<3; i++ )
			{
				s.Append("<tr><td>"+i+"</td>");
				for( int j=0; j<3; j++ )
					s.Append("<td>"+rnd.Next(10000)+"</td>");				
				s.AppendLine("</tr>");
			}
			s.AppendLine("</tbody>");
			s.AppendLine("</table>");
			
			Console.WriteLine( s );
		}
	}
}


using System;
using System.Text;
using System.Xml;

namespace N
{
	public class T
	{
		public static void Main()
		{
			var headers = new [] { "", "X", "Y", "Z" };
			
			var cols = headers.Select(name =>
				new XElement(
					"th",
					name,
					new XAttribute("text-align", "center")
				)
			);
		
			var rows = Enumerable.Range(0, 4).Select(ri =>
				new XElement(
					"tr",
					new XElement("td", ri),
					Enumerable.Range(0, 4).Select(ci =>
						new XElement(
							"td",
							ci,
							new XAttribute("text-align", "center")
						)
					)
				)
			);
				
			var xml = new XElement(
				"table", 
				new XElement(
					"thead", 
					new XElement("tr",    cols),
					new XElement("tbody", rows)
				)
			);
			
			Console.WriteLine(xml.ToString());
		}
	}
}


  

You may also check:How to resolve the algorithm Loops/Do-while step by step in the Spin programming language
You may also check:How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Groovy programming language
You may also check:How to resolve the algorithm Events step by step in the Nim programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Java programming language