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

时间:2013-06-08 02:58:49

标签: c#

我最近几天前开始使用C#(从Java过渡)并且正在编写一些代码来练习。当前程序读取xml文件并在html表中输出内容。我使用Visual Studios IDE。 我收到错误:'table'start标记与'body'的结束标记不匹配。这是我的代码:

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

***另外,如果有人可以推荐任何程序来编写练习C#,那就太棒了。

由于

1 个答案:

答案 0 :(得分:2)

请为了上帝的爱,不要写自己的Html(或xml)。您将遇到各种问题,从编码到不匹配的标签。使用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;
}

然后你可以这样称呼它:

XElement sourceXML = XElement.Load(Path.GetFullPath(outFile));
string html = CreateHTML(sourceXML);