例如文本文件中的内容:
xx大学学生考勤表序号 学号         姓名 学生类型 修课方式 备注
1 CST08008 aa 计算机  正常
2 CST08013 bb 计算机  正常 利用c#读取该文本文件后,提取其中的数据写成xml格式
如:-<考勤>
xx大学学生考勤表 
-<成员信息> 
<序号>1</序号> 
<学号>CST08008</学号> 
<姓名>aa</姓名> 
<学生类型>计算机</学生类型> 
<修课方式>正常</修课方式> 
<备注/>
 </成员信息>
 -<成员信息>
 <序号>2</序号>
 <学号>CST08013</学号>
 <姓名>bb</姓名>
 <学生类型>计算机</学生类型>
 <修课方式>正常</修课方式>
 <备注/> 
</成员信息>
</考勤>求高手帮帮忙~

解决方案 »

  1.   

    首先去百度查查怎么读取文本文件,再次去百度查查怎么操作XML.
    百度不行问Google.
      

  2.   

    执行步骤:
    1、有下面这个函数读取txt的数据:  public static List<String[]> ReadTxt(string filePathName)
      {
      List<String[]> ls = new List<String[]>();
      StreamReader fileReader=new StreamReader(filePathName); 
      string strLine="";int i=0;
      while (strLine != null)
      {
      i++;
      if(i==1)
         continue;   //第一行跳过
      strLine = fileReader.ReadLine();
      if (strLine != null && strLine.Length>0)
      {
      ls.Add(strLine.Split('|')); //
      }
      }
      fileReader.Close();
      return ls;
      }
    2、读出后,放入DataTable:
       注意:根据txt第一行,正确创建那些列!
       foreach(String[] strArr in ls)
       {
           table.Rows.Add(strArr);
       }3、生成xml:
       table.WriteXml(xmlfile);
      

  3.   

    测试文本:xx大学学生考勤表序号 学号 姓名 学生类型 修课方式 备注
    1 CST08008 aa 计算机 正常
    2 CST08013 bb 计算机 正常
    测试代码    class Program
        {
            static void Main(string[] args)
            {
                XElement root = new XElement("考勤");
                XElement el;
                List<string> list = new List<string>();
                List<string> col = new List<string>();
                int index = 0;            using (StreamReader sr = new StreamReader("F:\\a.txt"))
                {
                    while (sr.Peek() > 0)
                    {
                        index++;
                        list = sr.ReadLine().Trim().Split(' ').ToList();                    if (index == 1)
                        {
                            root.SetAttributeValue("Name", list[0]);
                        }
                        else if (index == 2)
                        {
                            continue;
                        }
                        else if (index == 3)
                        {
                            col = list;
                        }
                        else
                        {
                            el = new XElement("成员信息");
                            for (int i = 0, c = col.Count; i < c; i++)
                            {
                                if (i > list.Count - 1)
                                {
                                    el.Add(new XElement(col[i]));
                                }
                                else
                                {
                                    el.Add(new XElement(col[i], list[i]));
                                }
                            }
                            root.Add(el);
                        }
                    }
                }
                Console.WriteLine(root.ToString());
                Console.Read();
            }
        }
      

  4.   

    xx大学学生考勤表 这个文本最好当作 考勤 节点的一个属性,我这里取为Name
      

  5.   

    LZ在QQ中扩展了问题,就是数据分隔不仅仅是单个空格,而是有多个空格或者tab键,这里是修改后的代码:
            static void Main(string[] args)
            {
                XElement root = new XElement("考勤");
                XElement el;
                List<string> list = new List<string>();
                List<string> col = new List<string>();
                Regex reg = new Regex(@"\s+");
                int index = 0;            using (StreamReader sr = new StreamReader("F:\\a.txt"))
                {
                    while (sr.Peek() > 0)
                    {
                        index++;
                        list = reg.Split(sr.ReadLine().Trim()).ToList();                    if (index == 1)
                        {
                            root.SetAttributeValue("Name", list[0]);
                        }
                        else if (index == 2)
                        {
                            continue;
                        }
                        else if (index == 3)
                        {
                            col = list;
                        }
                        else
                        {
                            el = new XElement("成员信息");
                            for (int i = 0, c = col.Count; i < c; i++)
                            {
                                if (i > list.Count - 1)
                                {
                                    el.Add(new XElement(col[i]));
                                }
                                else
                                {
                                    el.Add(new XElement(col[i], list[i]));
                                }
                            }
                            root.Add(el);
                        }
                    }
                }
                root.Save("F:\\b.txt");
                Console.WriteLine(root.ToString());
                Console.Read();
            }