以所需格式解析上传到内存的 .txt 文件中的文本 C#

发布于 2024-12-11 00:39:26 字数 1519 浏览 0 评论 0原文

这里对 C# 相当陌生...


...我想做的是将上传到内存的 .txt 文件中的一堆文本解析为网页。

这是 .txt 文件的样子。


.RES B7=121
.RES C12=554
.RES VMAX=4.7μV

同样,它继续这样持续了另外 50 个左右的 .RES'... 我已成功解析它,但不是所需的格式...
这是我希望它在网页上的外观



B7.........121
C12.........554
VMAX.........4.7μV

全部位于 id="outpt" 的隐藏面板内...当前设置为visible="false"

<小时>

这是我在 C# 页面中使用的代码

namespace Sdefault
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnUpld_Click(object sender, EventArgs e)
        {
            Stream theStream = file1.PostedFile.InputStream; //uploads .txt file     to memory for parse

            using (StreamReader sr = new StreamReader(theStream))
            {
                string tba;
                while ((tba = sr.ReadLine()) != null)
                {

                    string[] tmpArray = tba.Split(Convert.ToChar("=")); //Splits ".RES B7=121" to "B7"... but for some reason, It prints it as
                                                                        //".RES B7.RES C12.RES VMAX"... I would ultimately desire it to be printed as shown above




                    Response.Write(tmpArray[0].ToString());
                    var.Text = tba;
                    outpt.Visible = true;
                }

            }

        }
    }

}

有任何指针我应该朝哪个方向走吗?

fairly new to C# here...

...What I'm trying to do is parse a bunch of text to a webpage from a .txt file uploaded to memory.

Here's what the .txt file looks like


.RES B7=121
.RES C12=554
.RES VMAX=4.7μV

Again, it goes on like this for another 50 or so .RES'...
I've successfully got it to parse, but not in the desired format...
Here's how I want it to look on the webpage



B7.........121
C12.........554
VMAX.........4.7μV

all inside of a hidden panel with id="outpt"... which is currently set to visible="false"


Here's the code i'm using in my C# Page

namespace Sdefault
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnUpld_Click(object sender, EventArgs e)
        {
            Stream theStream = file1.PostedFile.InputStream; //uploads .txt file     to memory for parse

            using (StreamReader sr = new StreamReader(theStream))
            {
                string tba;
                while ((tba = sr.ReadLine()) != null)
                {

                    string[] tmpArray = tba.Split(Convert.ToChar("=")); //Splits ".RES B7=121" to "B7"... but for some reason, It prints it as
                                                                        //".RES B7.RES C12.RES VMAX"... I would ultimately desire it to be printed as shown above




                    Response.Write(tmpArray[0].ToString());
                    var.Text = tba;
                    outpt.Visible = true;
                }

            }

        }
    }

}

Any pointers in which direction I should go with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

红焚 2024-12-18 00:39:26
// Open the file for reading
using (StreamReader reader = new StreamReader((Stream)file1.PostedFile.InputStream))
{
  // as long as we have content to read, continue reading
  while (reader.Peek() > 0)
  {
    // split the line by the equal sign. so, e.g. if:
    //   ReadLine() = .RES B7=121
    // then
    //   parts[0] = ".RES b7";
    //   parts[1] = "121";
    String[] parts = reader.ReadLine().split(new[]{ '=' });

    // Make the values a bit more bullet-proof (in cases where the line may
    // have not split correctly, or we won't have any content, we default
    // to a String.Empty)
    // We also Substring the left hand value to "skip past" the ".RES" portion.
    // Normally I would hard-code the 5 value, but I used ".RES ".Length to
    // outline what we're actually cutting out.
    String leftHand = parts.Length > 0 ? parts[0].Substring(".RES ".Length) : String.Empty;
    String rightHand = parts.Length > 1 ? parts[1] : String.Empty;

    // output will now contain the line you're looking for. Use it as you wish
    String output = String.Format("<label>{0}</label><input type=\"text\" value=\"{1}\" />", leftHand, rightHand);
  }

  // Don't forget to close the stream when you're done.
  reader.Close();
}
// Open the file for reading
using (StreamReader reader = new StreamReader((Stream)file1.PostedFile.InputStream))
{
  // as long as we have content to read, continue reading
  while (reader.Peek() > 0)
  {
    // split the line by the equal sign. so, e.g. if:
    //   ReadLine() = .RES B7=121
    // then
    //   parts[0] = ".RES b7";
    //   parts[1] = "121";
    String[] parts = reader.ReadLine().split(new[]{ '=' });

    // Make the values a bit more bullet-proof (in cases where the line may
    // have not split correctly, or we won't have any content, we default
    // to a String.Empty)
    // We also Substring the left hand value to "skip past" the ".RES" portion.
    // Normally I would hard-code the 5 value, but I used ".RES ".Length to
    // outline what we're actually cutting out.
    String leftHand = parts.Length > 0 ? parts[0].Substring(".RES ".Length) : String.Empty;
    String rightHand = parts.Length > 1 ? parts[1] : String.Empty;

    // output will now contain the line you're looking for. Use it as you wish
    String output = String.Format("<label>{0}</label><input type=\"text\" value=\"{1}\" />", leftHand, rightHand);
  }

  // Don't forget to close the stream when you're done.
  reader.Close();
}
何其悲哀 2024-12-18 00:39:26

不确定你的格式,但简单的分割和格式就可以了。

在您的拆分中,您将获得 = 符号之前的所有内容,而根据您的描述,您需要其之后的所有内容。例如

while(...)
{
   var line = String.Format("<label>{0}< />.........textbox>{0}< />",tba.Split('=')[1]);
   Response.Write(line);
}

Not sure on your formatting, but a simple Split and Format will do the trick.

On your split, you're getting everything before the = sign, whereas from your description, you want everything after it. e.g.

while(...)
{
   var line = String.Format("<label>{0}< />.........textbox>{0}< />",tba.Split('=')[1]);
   Response.Write(line);
}
弱骨蛰伏 2024-12-18 00:39:26

如果您采用 .RES B7=121 并将其拆分为“=”,则索引 0 将是 .RES B7,索引 1 将是 121

如果您想进一步细分,则必须使用 Chr(32) 再次拆分索引 0将产生 .RES 为 0,B7 为 1。

可以按照上面建议的字符串格式内联完成。

看起来你想要类似的东西

String.Format("<label>{0}< />.........textbox>{1}< />",tba.Split('=')[0].Split(' ')[1],tba.Split('=')[1].);

if you take .RES B7=121 and split it on "=" then the index 0 would be .RES B7 and index one would be 121

if you want to further subdivide you wil have to split index 0 again using Chr(32) which would yield .RES as 0 and B7 as 1.

Can be done inline as suggested above with string formatting.

It looks as though you want someling like

String.Format("<label>{0}< />.........textbox>{1}< />",tba.Split('=')[0].Split(' ')[1],tba.Split('=')[1].);
美男兮 2024-12-18 00:39:26

好吧..你在这里有几个问题..

首先,你使用 =.. 进行分割,所以你将字符串分成两部分......
.RES B7=121 得出:
tmpArray[0] = .RES B7
tmpArray[1] = 121

为了避免再次分割,最好的方法是将 .RES 替换为空字符串:

tmpArray[0] = tmpArray[0].replace(".RES","");

然后,您应该在页面上的某个位置显示它,因此您应该编写:

Response.Write("<label>" + tmpArray[0].ToString() + "< />" + "<textbox>" + tmpArray[1].ToString() + "< />");

Ok.. you have several issues here..

first, you are spliting using the =.. so you are splitting the string in two parts...
.RES B7=121 comes to:
tmpArray[0] = .RES B7
tmpArray[1] = 121

To avoid another split, the best you can do is to replace .RES with and empty string:

tmpArray[0] = tmpArray[0].replace(".RES","");

Then, you should be showing this somewhere on your page, so you should be writting:

Response.Write("<label>" + tmpArray[0].ToString() + "< />" + "<textbox>" + tmpArray[1].ToString() + "< />");
总攻大人 2024-12-18 00:39:26

看起来您需要进行 2 次分割:

第一个 var tmpArr1 = tba.Split(' ') 将生成一个字符串数组 {".RES","B7=121"}

2nd var tmpArr2 = tmpArr1[1].split('=') 将生成一个字符串数组{"B7","121"}

然后你可以这样做:

   var line = String.Format("<label>{0}</label><textbox>{1}</textbox>",tmpArr2[0],tmpArr2[1]);
   Response.Write(line);

Looks like you need to do 2 splits:

1st var tmpArr1 = tba.Split(' ') will result in a string array {".RES","B7=121"}

2nd var tmpArr2 = tmpArr1[1].split('=') will result in a string array {"B7","121"}

Then you can do:

   var line = String.Format("<label>{0}</label><textbox>{1}</textbox>",tmpArr2[0],tmpArr2[1]);
   Response.Write(line);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文