简体   繁体   English

html表标记不匹配关闭body标记

[英]html table tag not matching closing body tag

I recently started C# a couple days ago (transitioning from Java) and am writing some codes to practice. 我最近几天前开始使用C#(从Java过渡)并且正在编写一些代码来练习。 This current program reads an xml file and outputs the content in an html table. 当前程序读取xml文件并在html表中输出内容。 I use Visual Studios IDE. 我使用Visual Studios IDE。 I get the error: The 'table' start tag does not match the end tag of 'body'. 我收到错误:'table'start标记与'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. ***此外,如果有人可以推荐任何程序来编写练习C#,那就太棒了。

Thanks 谢谢

Please, for the love of god, don't write your own Html (or xml either). 请为了上帝的爱,不要写自己的Html(或xml)。 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. 使用HTML Agility Pack为您执行所有格式设置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM