0

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

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#,那就太好了。

谢谢

4

1 回答 1

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);
于 2013-06-08T03:50:08.827 回答