Use XML data to fill ASP forms

Source: Internet
Author: User
In this article, we will apply a simple Web form that lists some XML files in a directory. Then, we will select a file from this directory and send it to another Web form, which will apply the selected XML file

In this article, we will apply a simple Web form that lists some XML files in a directory. Then, we will select a file from this directory and send it to another Web form.

Will apply the selected XML file to fill some text fields.

Example XML

Our example XML document contains a set of elements containing basic information, such

Listing:

Reference content is as follows:
Listing A: sample. xml

John Doe
900 N. Michigan Ave
Chicago
IL
60614
630-555-5555

To make the clarification of this article more interesting, we will create two sample files containing different data, as shown in Listing B and Listing C:

Reference content is as follows:
Listing B: sample2.xml


George W. Bush
1600 Pennsylvania Ave NW
Washington
DC
20500
202-456-1111

Listing C: sample3.xml

Bill Gates
1 Microsoft Way
Redmond
WA
98052
425-882-8080

Create a directory named C: \ xmldocs and put all three sample files in this directory.

Web forms

We will create two Web forms for our application. We should use the first form to select an XML file and the second form to display XML data.

First, use Visual Studio. NET to create a new ASP. NET Web exploitation program. Then save the original form as SelectFile. aspx. If you double-click the form, the code editor will be opened.

First, you need to add the following line to the front of the code to add the IO package to your application:

UsingSystem. IO;

Then, find the Page_Load () method in the code. You need to compile this method as shown in the following code in Listing D:

Reference content is as follows:
Listing D: SelectFile ''sPage _ Load () method
Private void Page_Load (object sender, System. EventArgs e ){
System. IO. DirectoryInfo dir;
System. IO. FileInfo [] files;
System. IO. FileInfofinfo;
System. Collections. IEnumeratorfileEnum;

Dir = new DirectoryInfo ("C: \ xmldocs ");
Files = dir. GetFiles ("*. xml ");
FileEnum = files. GetEnumerator ();
Response. Write ("Please select the XML file to load:

\ N ");

While (fileEnum. MoveNext ()){
Finfo = (FileInfo) fileEnum. Current;
Response. Write ("" finfo. Name"
\ N ");
}
}

Add a new Web form -- ShowData. aspx to your project. Put six text boxes in the form and name them txtName, txtAddress, txtCity, txtState, txtZip, and txtPhone. Similarly, create six tags in the form, and Name the title bar as Name (Name), Address (Address), City (City), State (State) zip code and Phone number ).

Double-click the ShowData form to start the code editor. You need to compile the Page_Load () method of the ShowData form to make it look like in Listing E:

Reference content is as follows:
Listing E: ShowData ''spage _ Load () method
Private void Page_Load (object sender, System. EventArgs e ){
String filename;
XmlTextReaderxmlReader;

Filename = "C :\\ xmldocs \" Request. Params. Get ("filename ");
XmlReader = new XmlTextReader (filename );

While (xmlReader. Read ()){
If (xmlReader. NodeType = XmlNodeType. Element ){
Switch (xmlReader. LocalName ){
Case "Name ":
TxtName. Text = xmlReader. ReadString ();
Break;
Case "Address ":
TxtAddress. Text = xmlReader. ReadString ();
Break;
Case "City ":
TxtCity. Text = xmlReader. ReadString ();
Break;
Case "State ":
TxtState. Text = xmlReader. ReadString ();
Break;
Case "Zip ":
TxtZip. Text = xmlReader. ReadString ();
Break;
Case "Phone ":
TxtPhone. Text = xmlReader. ReadString ();
Break;
}
}
}
}

You also need to add the following line to the front of the code to add the XML package to your application:

UsingSystem. Xml;

This code is essentially the same as the code applied in the C # form example. The important difference is that we no longer use buttons to call this code, and we no longer need to apply the OpenFileDialog control to search for XML files. The file name is supplied by the SelectFile Web form. this code is called when the ShowData form is loaded.

We apply the XmlTextReaders Read () method to iterate in the XML document. Every time we call Read (), we process another XML node. We only need to check it briefly to make sure that the node is an element, and then we can apply the string data in the XML element node to fill the corresponding text box in our table.

Run the demo program
Once you complete the coding and compilation of the demo form, you are ready to apply them. Direct your Web browser to the SelectFile form. When a page is loaded, it should be able to list the example files we created earlier. Simply click a file name and you will see the data filled in the ShowData form.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.