我现在正在创建一个使用XDocument
从字符串构建html文档的程序。 (XElement.Parse)
我正在使用System.Xml并使用System.Xml.Linq;
举个好例子,我粘贴了一段代码:
...
StringBuilder table_dynamic10 = new StringBuilder();
table_dynamic10.Append("<tr><td class=\"tg-baqh\">" + *VALUE1* + "</td><td class=\"tg-baqh\">" + *VALUE2* + "</td></tr>");
...
var xDocument = new XDocument(
...
XElement.Parse(table_dynamic10.ToString())
...
*VALUE1*
和*VALUE2*
将是SQL数据库中的值。其中有几个,这就是为什么我要loop
使用table_dynamic10
而我{{{{ 1}}只允许显示。
我不知道如何解决这个问题。任何人都可以帮助我吗?
答案 0 :(得分:1)
意图有点不清楚,但也许您正在寻找的是含糊不清这样的东西:
public class WritesRowsOfHtml
{
public void BeginHtmlDocument(StringBuilder document)
{
document.Append("<html><body>");
}
public void WriteTable(StringBuilder document, IEnumerable<ThingContainingValues> things)
{
document.Append("<table>");
foreach (var thing in things)
{
document.AppendFormat("<tr><td class=\"tg-baqh\">{0}</td><td class=\"tg-baqh\">{1}</td></tr>",
thing.Value1, thing.Value2);
}
document.Append("</table>");
}
public void EndHtmlDocument(StringBuilder document)
{
document.Append("</body></html>");
}
}
一个单独的方法不必创建整个文档。您可以分段完成,然后当您将所有内容写入StringBuilder
时,您可以从该字符串构建XML文档。 (那就是你真的需要一个XML文档。我不确定你为什么需要它。)
这实际上取决于文档的复杂程度。您可以在一种方法中完成整个过程,包括打开和关闭标记。但如果它很复杂并且您不想编写巨型方法,那么您可以像这样拆分它。