文件是这样的:
<m_layoutstructure_count> 1
<m_id> 0
<m_layout_filename> None
<m_nature_reversal_flag> 0
......
我用下面的语句来读取<>后面的数据:
StreamReader sr;
sr=new StreamReader(name);
LayoutCount = int.Parse (sr.ReadLine().Replace ("<m_layoutstructure_count>",""));
wk_id = int.Parse (sr.ReadLine().Replace ("<m_id>",""));
......
第一次的时候还好用,之后读文件就不是从0位置开始读了,不一定跳到什么地方.
想问一下有没有什么可以定位的方法啊.我试了好几个都达不到效果:
sr.BaseStream.Position =0;
sr.BaseStream.Seek(0,System.IO.SeekOrigin.Begin);
都不好用啊.

解决方案 »

  1.   

    你应该加一个
    while StreamReader 做循环才行吧
      

  2.   

    什么意思?我只读一遍啊,为什么要用while?
      

  3.   

    sr.ReadLine()这只读了一行,要加循环
      

  4.   

    我是每行都ReadLine()了一遍,为什么要循环?我现在的问题是读文件时不是从头开始读了.
    LayoutCount = int.Parse (sr.ReadLine().Replace ("<m_layoutstructure_count>",""));
    按理说是读第一行的,但是读的却是第二行<m_id> 0
    然后<m_id>与<m_layoutstructure_count>一比较,就报错了.
      

  5.   

    要多行,可以放入数组,当然要循环如果只要第一行,用一个字符串变量就好了string str = sr.ReadLine();
    以后直接用str就可以了
      

  6.   

    因为我要用到每一行<>后面的那个变量,而变量的值的类型不是确定的,
    所以我需要每一行ReadLine();一次啊,然后保存到一个变量里.很正常的事情,为什么大家都这么惊讶的感觉,我都糊涂了.
      

  7.   

    重复一下我的问题啊.我遇到的问题是这样的:比如文件是这样的:
    <m_layoutstructure_count> 1
    <m_id> 0
    <m_layout_filename> None
    <m_nature_reversal_flag> 0
    ......
    而我的程序是这么写的:
    StreamReader sr;
    sr=new StreamReader(name);
    LayoutCount = int.Parse (sr.ReadLine().Replace ("<m_layoutstructure_count>",""));
    wk_id = int.Parse (sr.ReadLine().Replace ("<m_id>",""));
    wk_layout_filename =sr.ReadLine().Replace ("<m_layout_filename>","");
    wk_nature_reversal_flag = int.Parse (sr.ReadLine().Replace ("<m_nature_reversal_flag>",""));
    ......就这样每行为一个变量赋值,然后运行,
    得到了LayoutCount=1,wk_id=0,到第三个的时候报错了,说输入字符串格式不正确.
    于是我什么也没改,想找原因于是重新跟踪,
    可没想到这回第一个数据就读不出来了.
    问题就在这里,我觉得是缺少定位引起的,希望有人能指点一下.
    谢谢.
      

  8.   

    to 什么意思?我只读一遍啊,为什么要用while?你做个循环读,如果各式固定的话,进行固定格式的循环分析。