想要从具有特定模式的文本文件中读取数据并相应地填充文本框

时间:2014-03-03 08:09:23

标签: asp.net text-files

这是我的文本文件

R3073101.txt

3070004626!@!JAVEED BASHIR BURZA#@#BASHIR BURZA#@#502 BLUE BELLS FLOWER VALLEY附近#@#PAYYEDE HOTEL NAYA NAGAR#@#MIRA ROAD EAST THANE#@#401107#@#502 BLUE BELLS FLOWER VALLEY #@#PAYYEDE HOTEL NAYA NAGAR#@#MIRA ROAD EAST THANE#@#401107#@#1#@#1#@#N#@#15#@#114#@#FORD FIGO 1 4 EXI#@# 4#@#1399#@#5#@#0#@#1525#@#0#@#2#@#MAJ1XXMRJ1DU78421#@#MAJ1XXMRJ1DU78421#@#C01#@#K BLUE#@#1#@#2014 #@#二零二九年一月三十日#@#307#@#1#@#O#上@ ## @#PV#@ ## @#541137#@ ## @ ## @#1/31/2014年#@ #0#@#1399#@#0#@#0#@#0#@#0#@ ## @ ## @ ## @ ## @ ## @ ## @#1/31/2014年#@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @# #@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @#NE#@# 1#@#2014年1月31日#@#2015年1月30日#@#71050131130100129567#@#HP>< 401107>< 2014年1月31日#@# 3070004627!@!SAJID PERVEZ#@#ALEY AHMED#@#1005/6 10th FLR MEGHDOOT A#121#@ LOKHANDWALA COMPLEX#@#ANDHERI WEST MUMBAI#@#400053#@#1005/6 10TH FLR MEGHDOOT A 121#@ #LOKHANDWALA COMPLEX#@#ANDHERI WEST MUMBAI#@#400053#@#1#@#1#@#N#@#15#@#114#@#FORD ECOSPORT 1 5 P TITA at#@#4#@# 1499#@#5#@#0#@#1700#@#0#@#1#@#MAJAXXMRKAEL12244#@#MAJAXXMRKAEL12244#@#C01#@#D WHITE#@#1#@#2014#@#1 /二千零二十九分之三十零#@#307#@#N#@#O#上@ ## @#PV#@ ## @#952517#@ ## @ ## @#2014年1月31日#@#0#@ #1499#@#0#@#0#@#0#@#0#@ ## @ ## @ ## @ ## @ ## @ ## @#2014年1月31日#@ ## @# #@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @# #@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @#IL#@#1#@# 2014年1月31日#@#2015年1月30日#@#I24131022#@# 3070004628!@!ABRAR MALIK#@#SARDAR MALIK#@#A / 703 PANCHTANTRA APT-II#@#PANCH MARG VERSOVA#@#MUMBAI#@#400061#@#A / 703 PANCHTANTRA APT-II#@#PANCH MARG VERSOVA#@#MUMBAI#@#400061#@#1#@#1#@#N#@#15#@#114#@#FORD ECOSPORT 1 5 P AMB MT#@#4#@#1499#@# 5#@#0#@#1700#@#0#@#1#@#MAJAXXMRKAEL15204#@#MAJAXXMRKAEL15204#@#C01#@#D WHITE#@#1#@#2014#@#1/30/209 #@#307#@#1#@#O#上@ ## @#PV#@ ## @#653670#@ ## @ ## @#2014年1月31日#@#0#@#1499#@ #0#@#0#@#0#@#0#@ ## @ ## @ ## @ ## @ ## @ ## @#2014年1月31日#@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @# #@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @#IL#@#1#@#1/31 / 2014#@#2015年1月30日#@#PS11225946#@#HP>< 400059>< 2014年1月31日#@# 3070004629!@ AVB AMARAN#@#BABU ANTHONY CHETTY#@#B / 203 DRONAGIRI现代USHA COLONY#@#EVERSHINE NAGAR MALAD WEST#@#MUMBAI#@#400064#@#B / 203 DRONAGIRI MODERN USHA COLONY#@# EVERSHINE NAGAR MALAD WEST#@#MUMBAI#@#400064#@#1#@#1#@#N#@#15#@#114#@#FORD ECOSPORT 1 5 P AMB MT#@#4#@#1499 #@#5#@#0#@#1700#@#0#@#1#@#MAJAXXMRKAEL15207#@#MAJAXXMRKAEL15207#@#C01#@#M SILVER#@#1#@#2014#@#1 /二千零二十九分之三十零#@#307#@#1#@#O#上@ ## @#PV#@ ## @#653670#@ ## @ ## @#2014年1月31日#@#0#@# 1499#@#0#@#0#@#0#@#0#@ ## @ ## @ ## @ ## @ ## @ ## @#2014年1月31日#@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @# #@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @#NE#@#1#@#1 / 31/2014#@#1/30/2015#@#71050131130100129516#@#HP>< 3RD FLR A-WING TRADE STAR BLDG>< 400059>< 1/31/2014#@# 3070004630!@!NAROTTAM KUMAR SHARMA#@#PARMANAND SHARMA#@#103-B ASTER TOWER FILM CITY ROAD#@#FILM CITY ROAD MALAD EAST#@​​#MUMBAI#@#400097#@#103-B ASTER TOWER FILM CITY ROAD #@ #FILM CITY ROAD MALAD EAST#@​​#MUMBAI#@#400097#@#1#@#1#@#N#@#15#@#114#@#FORD ECOSPORT 1 0 P TITA MT#@#4 #@#999#@#5#@#0#@#1700#@#0#@#1#@#MAJAXXMRKAEL17257#@#MAJAXXMRKAEL17257#@#C01#@#CHILL#@#1#@#2014#@ #1/30/2029年#@#307#@#1#@#O#上@ ## @#PV#@ ## @#895890#@ ## @ ## @#2014年1月31日#@#0 #@#999#@#0#@#0#@#0#@#0#@ ## @ ## @ ## @ ## @ ## @ ## @#2014年1月31日#@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @# #@ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @ ## @#IL#@#1# @#1 /二千○十四分之三十一#@#2015年1月30日#@#I-24134004#@#HP>< 400098>< 2014年1月31日#@#

我的.aspx代码

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div align="left">
        <h3>
            <b>New Customer </b>
        </h3>
    </div>
    <br />
    <div class="form-horizontal">
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Date</label>
            <div class="col-sm-6">
              <div class="input-group date"  data-date="12-02-2012" data-date-format="mm-dd-yyyy" id="datetimepicker1">
                <asp:TextBox ID="TextBox1" class="span2" runat="server" Placeholder="Enter Date" CssClass="form-control"></asp:TextBox>
                    <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar">
                    </span>
                    </span>
                </div>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Date Required"
                    ControlToValidate="TextBox1" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Customer Name</label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox2" runat="server" Placeholder="Enter Name" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Customer Name Required"
                    ControlToValidate="TextBox2" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Regn No</label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox3" runat="server" Placeholder="Enter Regn No" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Regn No Required"
                    ControlToValidate="TextBox3" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Imv</label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox4" runat="server" Placeholder="Enter Imv" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server" ErrorMessage="IMV Required"
                    ControlToValidate="TextBox4" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Registration Cho</label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox5" runat="server" Placeholder="Enter Registration Cho" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="Registration CHO Required"
                    ControlToValidate="TextBox5" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Crtem Fees</label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox6" runat="server" Placeholder="Enter Crtem Fees" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Crtem Fees Required"
                    ControlToValidate="TextBox6" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Crtem S.C</label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox7" runat="server" Placeholder="Enter Crtem S.C" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Crtem S.C Required"
                    ControlToValidate="TextBox7" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                SOC</label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox8" runat="server" Placeholder="Enter SOC" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="SOC Required"
                    ControlToValidate="TextBox8" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Soc S.C
            </label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox9" runat="server" Placeholder="Enter Soc S.C" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="Soc S.c Required"
                    ControlToValidate="TextBox9" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Spl No.fees
            </label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox10" runat="server" Placeholder="Enter Spl No.Fees" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ErrorMessage="Spl No.fees Required"
                    ControlToValidate="TextBox10" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Spl No.s.c
            </label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox11" runat="server" Placeholder="Enter Spl No.s.c" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ErrorMessage="Spl No.s.c Required"
                    ControlToValidate="TextBox11" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Total No
            </label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox12" runat="server" Placeholder="Enter Total No" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage=" Total No Required"
                    ControlToValidate="TextBox12" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                No.5
            </label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox13" runat="server" Placeholder="Enter No.5" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ErrorMessage="No.5 Required"
                    ControlToValidate="TextBox13" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Redium
            </label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox14" runat="server" Placeholder="Enter Redium" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator14" runat="server" ErrorMessage="Redium Required"
                    ControlToValidate="TextBox14" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                W.V
            </label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox15" runat="server" Placeholder="Enter W.V" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator15" runat="server" ErrorMessage="W.V Required"
                    ControlToValidate="TextBox15" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                TAX
            </label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox16" runat="server" Placeholder="Enter TAX" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator16" runat="server" ErrorMessage="TAX Required"
                    ControlToValidate="TextBox16" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Int
            </label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox17" runat="server" Placeholder="Enter Int" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator17" runat="server" ErrorMessage="Int Required"
                    ControlToValidate="TextBox17" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                CF
            </label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox18" runat="server" Placeholder="Enter CF" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator18" runat="server" ErrorMessage="CF Required"
                    ControlToValidate="TextBox18" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Above 10 Lacs
            </label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox19" runat="server" Placeholder="Enter Above 10 lacs" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator19" runat="server" ErrorMessage="Above 10 Lacs Required"
                    ControlToValidate="TextBox19" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">
                Total
            </label>
            <div class="col-sm-6">
                <asp:TextBox ID="TextBox20" runat="server" Placeholder="Enter Total" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="col-sm-4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator20" runat="server" ErrorMessage="Total Required"
                    ControlToValidate="TextBox20" ForeColor="Red" ValidationGroup="val1"></asp:RequiredFieldValidator>
            </div>
        </div>
        <div class="row" style="margin-left: 0px; margin-right: 176px">
            <div class="col-md-6">
                <asp:Button ID="Button1" runat="server" Text="Submit" CssClass="btn btn-primary"
                    ValidationGroup="val1" Height="40px" Width="89px" OnClick="btnsubmit_Click" />
            </div>
            <div class="col-md-6">
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </div>
        </div>
        <div class="row" style="margin-left: 0px; margin-right: 176px">
            <div class="col-md-6">
                <asp:Button ID="Button2" runat="server" Text="Upload" CssClass="btn btn-primary"
                     Height="40px" Width="89px" 
                    onclick="Button2_Click" />
            </div>
            <div class="col-md-6">
                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            </div>
        </div>
    </div>
</asp:Content>

在点击更新按钮上我的文本框应该按照那里的名字填充,例如ex-:在文本文件中它首先有3070004626 = Sr.no!@!JAVEED BASHIR BURZA =名字,就像javeed bashir burza应该得到的填充到textbox1.text

感谢Advace。

1 个答案:

答案 0 :(得分:0)

假设您的txt文件的格式完全相同,您可以在button_Click()方法中使用此查询:

var result = from txt in File.ReadAllLines(@"/path/to/your/file")
             let column = txt.Split(new [] {'!', '@','#'}).Where (t => t.Length > 0 )
             select column.Skip(1).FirstOrDefault();

//Output: JAVEED BASHIR BURZA
textbox1.Text = result.First();

修改

如果您需要所有元素并希望迭代它们,请使用以下代码:

var result = from txt in File.ReadAllLines(@"/path/to/your/file")
             let column = txt.Split(new [] {'!', '@','#'}).Where (t => t.Length > 0 )
             select column;

foreach (var element in result)
{
    Console.WriteLine(element);
}