0

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.

XML File is:

<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:

enter image description here

And code:

 string filePath = "Log.xml";
            DataSet ds = new DataSet();
            ds.ReadXml(filePath);

2 Answers 2

1

The following code will create the 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")
                    });
                }


            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you want HTML for our mail, you just need to build a string containing the HTML. Loop through your data set, and for each row, create the HTML, using the values from the data set in the <td> tags.

Try this out, and if you have problems, post your code and explain the problem you are having with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.