Hi Blame,
Based on your description, I don’t know the definition of your XML file. A simple demo which reads the text file and writes the fields into an xml file is below:
string txtPath = @"log.txt";
List<string> list = new List<string>();
DataTable table1 = new DataTable();
DataColumn dc1 = null;
dc1 = table1.Columns.Add("col1", Type.GetType("System.String"));
dc1 = table1.Columns.Add("col2", Type.GetType("System.Int32"));
dc1 = table1.Columns.Add("col3", Type.GetType("System.Int32"));
dc1 = table1.Columns.Add("col4", Type.GetType("System.Int32"));
using (StreamReader sr = new StreamReader(txtPath))
{
while (sr.Peek() >= 0)
{
list=sr.ReadLine().Split(' ').ToList();
List<string> newlist = new List<string>();
foreach (var item in list)
{
if (!string.IsNullOrEmpty(item))//remove empty
{
newlist.Add(item);
}
}
table1.Rows.Add(newlist[0],int.Parse(newlist[1]),int.Parse(newlist[2]),int.Parse(newlist[3]));
}
}
DataSet ds = new DataSet();
ds.Tables.Add(table1);
ds.WriteXml(@"Product.xml");
If you have any further questions, please feel free to post in this forum.
Best regards,
Leo
No comments:
Post a Comment