C# html表格标记与结束正文标记不匹配
几天前,我刚从Java过渡到C语言,我正在编写一些代码来练习。此当前程序读取xml文件并将内容输出到html表中。我使用Visual Studio IDE。 我发现错误:“table”开始标记与“body”的结束标记不匹配。这是我的密码:C# html表格标记与结束正文标记不匹配,c#,C#,几天前,我刚从Java过渡到C语言,我正在编写一些代码来练习。此当前程序读取xml文件并将内容输出到html表中。我使用Visual Studio IDE。 我发现错误:“table”开始标记与“body”的结束标记不匹配。这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.IO; using Sys
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实践,那就太好了
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>");
}
}
}
谢谢,看在上帝的份上,也不要写自己的Html或xml。您将遇到各种各样的问题,从编码到错误匹配标记。使用为您执行所有格式设置
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>");
}
}
}
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;
}
请注意,这不是问题的原因:我想您希望每个产品有一行,所以请将您的tr创建移动到product foreach中。您可以发布XML的外观吗?还有,为什么会有一个看起来永远不会结束的while循环?并将您得到的结果发布。@gunr217…我不知道如何在仍然有节点的情况下保持porgram循环。输出没有运行,因为我遇到了运行时错误。Gambardella,MatthewXML开发人员指南Computer44.952000-10-01深入了解如何使用XML创建应用程序。@TimMedora…是的,谢谢你…还有其他想法issue@gunr2171...The第4行位置2上的“表格”开始标记与“正文”的结束标记不匹配。第461行,位置3。即使在agility pack…@xciutionx之后,仍然会收到此消息,请使用您获得的确切html输出更新您的问题。