几天前我最近开始使用 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#,那就太好了。
谢谢