I have an log file like this..

This is the segment 1
============================

<MAINELEMENT><ELEMENT1>10-10-2013 10:10:22.444</ELEMENT1><ELEMENT2>1111</ELEMENT2>    
   <ELEMENT3>Message 1</ELEMENT3></MAINELEMENT>
<MAINELEMENT><ELEMENT1>10-10-2013 10:10:22.555</ELEMENT1><ELEMENT2>1111</ELEMENT2>   
   <ELEMENT3>Message 2</ELEMENT3></MAINELEMENT>

This is the segment 2
============================

<MAINELEMENT><ELEMENT1>10-11-2012 10:10:22.444</ELEMENT1><ELEMENT2>2222</ELEMENT2>    
   <ELEMENT3>Message 1</ELEMENT3></MAINELEMENT>
<MAINELEMENT><ELEMENT1>10-11-2012 10:10:22.555</ELEMENT1><ELEMENT2>2222</ELEMENT2>   
   <ELEMENT3>Message 2</ELEMENT3></MAINELEMENT>

How can I read this into DataTable excluding the data This is the segment 1 and This is the segment 2 and ====== lines completely.

I would like to have the Datatable as with Columns as "ELEMENT1", "ELEMENT2", "ELEMENT3" and fill the details with the content between those tags in the order of print of line.

It should not change the sequence of the order of records in the table while inserting.

有帮助吗?

解决方案

HtmlAgilityPack seems to be a good tool for what you need:

using HtmlAgilityPack;

class Program
{
    static void Main(string[] args)
    {
        var doc = new HtmlDocument();
        doc.Load("log.txt");
        var dt = new DataTable();
        bool hasColumns = false;
        foreach (HtmlNode row in doc
            .DocumentNode
            .SelectNodes("//mainelement"))
        {
            if (!hasColumns)
            {
                hasColumns = true;
                foreach (var column in row.ChildNodes
                    .Where(node => node.GetType() == typeof(HtmlNode)))
                {
                    dt.Columns.Add(column.Name);
                }
            }
            dt.Rows.Add(row.ChildNodes
                .Where(node => node.GetType() == typeof(HtmlNode))
                .Select(node => node.InnerText).ToArray());
        }
    }
}

其他提示

could do this, where stringData is the data from the file you have

    var array = stringData.Split(new[] { "============================" }, StringSplitOptions.RemoveEmptyEntries);
            var document = new XDocument(new XElement("Root"));
            foreach (var item in array)
            {
                if(!item.Contains("<"))
                    continue;
                var subDocument = XDocument.Parse("<Root>" + item.Substring(0, item.LastIndexOf('>') + 1) + "</Root>");
                foreach (var element in subDocument.Root.Descendants("MAINELEMENT"))
                {
                    document.Root.Add(element);
                }
            }
            var table = new DataTable();
            table.Columns.Add("ELEMENT1");
            table.Columns.Add("ELEMENT2");
            table.Columns.Add("ELEMENT3");
            var rows =
                document.Descendants("MAINELEMENT").Select(el =>
                                                               {
                                                                   var row = table.NewRow();
                                                                   row["ELEMENT1"] = el.Element("ELEMENT1").Value;
                                                                   row["ELEMENT2"] = el.Element("ELEMENT2").Value;
                                                                   row["ELEMENT3"] = el.Element("ELEMENT3").Value;
                                                                   return row;
                                                               });
            foreach (var row in rows)
            {
                table.Rows.Add(row);
            }

            foreach (DataRow dataRow in table.Rows)
            {
                Console.WriteLine("{0},{1},{2}", dataRow["ELEMENT1"], dataRow["ELEMENT2"], dataRow["ELEMENT3"]);
            }

I'm not so sure where you problem is.

You can use XElement for reading the xml and manually creating DataTable. For Reading the XML See Xml Parsing using XElement

Then you can create dynamically the datatable. Heres an example of creating a datatable in code https://sites.google.com/site/bhargavaclub/datatablec

But why do you want to use a DataTable ? There are a lot of downsides...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top