Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过读取xml文件从C中的Dataset和Datatables创建HTML表_C#_Xml_C# 4.0 - Fatal编程技术网

C# 如何通过读取xml文件从C中的Dataset和Datatables创建HTML表

C# 如何通过读取xml文件从C中的Dataset和Datatables创建HTML表,c#,xml,c#-4.0,C#,Xml,C# 4.0,我对C语言非常陌生,我正在阅读C语言中的XML文件,希望以表格形式在邮件中发送XML文件数据 XML文件是: <log> <logentry version='123'> <author>Dexter</author> <date>12 July 2017</date> <paths> <path action="M">C:\Desktop</pa

我对C语言非常陌生,我正在阅读C语言中的XML文件,希望以表格形式在邮件中发送XML文件数据

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>

假设您希望我们的邮件使用HTML,您只需要构建一个包含HTML的字符串。循环遍历数据集,并使用标记中数据集的值为每一行创建HTML


尝试一下,如果您有问题,请发布代码并解释您的问题。

以下代码将创建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")
                    });
                }


            }
        }
    }
}
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")
                    });
                }


            }
        }
    }
}