Tutorials to .com

Tutorials to .com » Asp » Xml » The use of XML data to ASP form filling

The use of XML data to ASP form filling

Print View , by: iSee ,Total views: 12 ,Word Count: 1204 ,Date: Wed, 22 Apr 2009 Time: 7:19 AM

Brian Schaffner is the author of this article Fujitsu, deputy director of the consulting firm. Fujitsu his technology consulting firm providing architecture, design and development support.

In this article, we will use a simple Web form, it lists a directory of some xml documents. Then, we will from this directory to select a file, send it to another Web form, the latter will be selected using the xml file to populate some text fields.

Example xml

Our sample XML document contains a set of elements containing basic information, such as shown in Listing A:

Listing A: sample.xml
<Sample>
<Name> John Doe </ Name>
<Address> 900 N. Michigan Ave </ Address>
<City> Chicago </ City>
<State> IL </ State>
<Zip> 60614 </ Zip>
<Phone> 630-555-5555 </ Phone>
</ Sample>

To make this article more interesting note, we will create two examples of data with different documents, such as Listing B and Listing c follows:

Listing B: sample2.xml

<Sample>
<Name> George W. Bush </ Name>
<Address> 1600 Pennsylvania Ave NW </ Address>
<City> Washington </ City>
<State> DC </ State>
<Zip> 20500 </ Zip>
<Phone> 202-456-1111 </ Phone>
</ Sample>

Listing C: sample3.xml
<Sample>
<Name> Bill Gates </ Name>
<Address> 1 Microsoft Way </ Address>
<City> Redmond </ City>
<State> WA </ State>
<Zip> 98052 </ Zip>
<Phone> 425-882-8080 </ Phone>
</ Sample>

Is called to create a C: \ xmldocs the directory, and all three sample file into the directory.

Web form

We will for our application to create two Web forms. We must use a form to select the XML file, use the second form to display XML data.

First of all, to use Visual Studio. NET to create a new ASP.NET Web application. The initial form and then save it as a SelectFile.aspx. If you double-click the form, code editor will open.

You first need to add the following line to the top of the code can be added to the IO package your application program:

usingSystem.IO;

Then, find the code within the Page_Load () method. This method you need to edit the image following the code in Listing D, as:

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: <br> <br> \ n");

while (fileEnum.MoveNext ()) (
finfo = (FileInfo) fileEnum.Current;
Response.Write ( "<a href=\"ShowData.aspx?filename=" + finfo.Name + "\">" + finfo.Name + "</ a> <br> \ n");
)
)

Now in your project add a new Web form - ShowData. Aspx. Placed in the six form text box, and name it: txtName, txtAddress, txtCity, txtState, txtZip and txtPhone. Similarly, in the form in the creation of six labels, and named the title bar Name (Name), Address (Address), City (city), State (State), Zip (postal code) and Phone (phone) .

Double-click the form to start ShowData code editor. ShowData form you need to edit the Page_Load () method so that it looks like the same as in Listing E:

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 need to add the following line to the top of the code can be added to the XML package your application program:

usingSystem.Xml;

This code is essentially the same form in the C++ # example uses the same code. The main difference lies in the fact that we no longer use the button to call the code, and we no longer need to use the OpenFileDialog control to find the XML file. SelectFile file name is provided by the Web form, and the code has been loaded in the form ShowData when called.

We use XmlTextReaders Read () method in the XML document in iteration. With each of the Read () call, we will deal with another XML node. We only use a simple check to ensure that the node is an element, and then you can use XML element node's string data to fill in our form of the corresponding text box.

Run demo
Once you have completed the presentation of the code and compile the form, you are ready to use them. Your Web browser to SelectFile form. When the time the page was loading, it should be able to create a list of examples of our previous papers. Only a simple click on a file name, you will see the data in a form to fill in ShowData.


Asp and XML related Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.