Reputation: 85
I recently started C# a couple days ago (transitioning from Java) and am writing some codes to practice. This current program reads an xml file and outputs the content in an html table. I use Visual Studios IDE. I get the error: The 'table' start tag does not match the end tag of 'body'. Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Linq;
namespace XML
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the file name: ");
String fileName = Console.ReadLine();
Console.WriteLine("Enter output file");
String outFile = Console.ReadLine();
XDocument xml = XDocument.Load(Path.GetFullPath(outFile));
StreamWriter outf = new StreamWriter(Path.GetFullPath(outFile));
outputHTML(xml,outf);
outf.Close();
}
static void outputHTML(XDocument xml, StreamWriter outf)
{
outf.WriteLine("<!DOCTYPE html>");
outf.WriteLine("<html>");
outf.WriteLine("<body>");
outf.WriteLine("<table border='1'>");
while (xml.Root.HasElements)
{
outf.WriteLine("<tr>");
var products = from p in xml.Descendants("book")
select new
{
author = (String)p.Element("author"),
genre = (String)(String)p.Element("genre"),
title = (string)p.Element("title"),
price = (int)p.Element("price"),
publishDate = (String)p.Element("publish_date"),
Descrip = (String)p.Element(" ")
};
foreach (var product in products)
{
outf.WriteLine("<td>");
outf.WriteLine(product.author);
outf.WriteLine("</td>");
outf.WriteLine("<td>");
outf.WriteLine(product.title);
outf.WriteLine("</td>");
outf.WriteLine("<td>");
outf.WriteLine(product.genre);
outf.WriteLine("</td>");
outf.WriteLine("<td>");
outf.WriteLine(product.price);
outf.WriteLine("</td>");
outf.WriteLine("<td>");
outf.WriteLine(product.publishDate);
outf.WriteLine("</td>");
outf.WriteLine("<td>");
outf.WriteLine(product.Descrip);
outf.WriteLine("</td>");
}
outf.WriteLine("</tr>");
}
outf.WriteLine("</table>");
outf.WriteLine("</body>");
outf.WriteLine("</html>");
}
}
}
***Also, if anyone could recommend any programs to write to practice C#, that would be great.
Thanks
Upvotes: 0
Views: 698
Reputation: 17510
Please, for the love of god, don't write your own Html (or xml either). You are going to run into ALL sorts of problems, from encoding to mis-matching tags. Use the HTML Agility Pack
to do all the formatting for you.
public string CreateHTML(XElement sourceXML)
{
//make the Html Agility Pack object
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
//parse through your xml
var products = sourceXML.Descendants("book")
.Select(x => new
{
author = x.Element("author").Value,
genre = x.Element("genre").Value,
title = x.Element("title").Value,
price = x.Element("price").Value,
publishDate = x.Element("publish_date").Value,
descrip = x.Element("description"),
});
//make and populate your table node
HtmlNode tableNode = HtmlNode.CreateNode("<table border='1'>");
foreach (var product in products)
{
tableNode.AppendChild(HtmlNode.CreateNode("<td>" + product.author + "</td>"));
tableNode.AppendChild(HtmlNode.CreateNode("<td>" + product.genre + "</td>"));
....
}
//create the html root and append the table node
doc.DocumentNode.AppendChild(HtmlNode.CreateNode("<html><body>"));
doc.DocumentNode.Element("html").Element("body").AppendChild(tableNode);
return doc.DocumentNode.InnerHtml;
}
You can then call it like this:
XElement sourceXML = XElement.Load(Path.GetFullPath(outFile));
string html = CreateHTML(sourceXML);
Upvotes: 2