[英]How to create HTML Table from Dataset and Datatables in C# by reading xml file
I am very new to C#, I am reading XML file in C# and want to send xml file data as tabular form in mail. 我是C#的新手,正在C#中读取XML文件,并希望将XML文件数据以表格形式发送到邮件中。
XML File is: XML文件是:
<log>
<logentry version='123'>
<author>Dexter</author>
<date>12 July 2017</date>
<paths>
<path action="M">C:\Desktop</path>
<path action="N">C:\Documents\test.txt</path>
</paths>
<msg>Added New file</msg>
</logentry>
<logentry version='124'>
<author>Dexter2</author>
<date>11 July 2017</date>
<paths>
<path action="M">C:\Desktop\Test\mail.cp</path>
</paths>
<msg>Added New file in test folder</msg>
</logentry>
</log>
And I am expecting table as: 我期望表为:
And code: 和代码:
string filePath = "Log.xml";
DataSet ds = new DataSet();
ds.ReadXml(filePath);
The following code will create the DataTable. 以下代码将创建DataTable。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("version", typeof(int));
dt.Columns.Add("date", typeof(DateTime));
dt.Columns.Add("author", typeof(string));
dt.Columns.Add("msg", typeof(string));
dt.Columns.Add("paths", typeof(string));
dt.Columns.Add("action", typeof(string));
XDocument doc = XDocument.Load(FILENAME);
foreach(XElement logentry in doc.Descendants("logentry"))
{
foreach(XElement path in logentry.Descendants("path"))
{
dt.Rows.Add(new object[] {
(int)logentry.Attribute("version"),
(DateTime)logentry.Element("date"),
(string)logentry.Element("author"),
(string)logentry.Element("msg"),
(string)path.FirstNode.ToString(),
(string)path.Attribute("action")
});
}
}
}
}
}
Assuming you want HTML for our mail, you just need to build a string containing the HTML. 假设您希望我们的邮件使用HTML,则只需要构建一个包含HTML的字符串。 Loop through your data set, and for each row, create the HTML, using the values from the data set in the <td>
tags. 遍历数据集,并使用<td>
标记中数据集的值为每一行创建HTML。
Try this out, and if you have problems, post your code and explain the problem you are having with it. 尝试一下,如果有问题,请发布代码并解释所遇到的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.