我的程序从字符串构建一个html文件。我使用xDocument(使用System.Xml;使用System.Xml.Linq)
一切都很好用,但在某些时候我想添加一个显示数字的循环来完成while循环(1,2,3 ......)我在代码中标记了这个位置(这个地方为号)
var table_dynamic10 = new XElement("tr");
using (SqlConnection conn = new SqlConnection(conString))
{
string query = string.Format("{0}{1}'", "SELECT ...");
using (SqlCommand cmd = new SqlCommand(query , conn))
{
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
table_dynamic10.Add(
new XElement("tr",
new XElement("td", new XAttribute("class", "tg-baqh")), **THIS PLACE FOR NUMBER**
new XElement("td", new XAttribute("class", "tg-baqh"), reader["SQL_Value"]),
new XElement("td", new XAttribute("class", "tg-baqh"))));
}
}
}
}
var xDocument = new XDocument(
new XDocumentType("html", null, null, null),
new XElement("html",
new XElement("head"),
new XElement("body",
new XElement("table", table_dynamic10))));
答案 0 :(得分:0)
int index = 1;
while (reader.Read())
{
table_dynamic10.Add(
new XElement("tr",
new XElement("td", new object[] {new XAttribute("class", "tg-baqh"),new XAttribute("index", index++)}),
new XElement("td", new XAttribute("class", "tg-baqh"), reader["SQL_Value"]),
new XElement("td", new XAttribute("class", "tg-baqh"))));