XML stores Address book data and resolves it in a C # program to appear on the form

Source: Internet
Author: User

1. First create an XML file to hold the data

2. Create a new project in VS Creating a Windows Forms application (the path project name modifies itself)

3. Drag the following controls on the form and modify the properties of each control to make it easier to determine which control is in your code, preferably without using the system's default control name (you can make a meaningful name that makes it easier to confirm the data the control will display when writing code ...). I didn't see what I was feeling.)

.

4. Double-click the form to write code, first to parse the Xml file must be added using System.Xml in the header file; Header file

Then, to load it out, the data must be added in this private void Form1_Load (object sender, EventArgs e) method to parse the XML document, first creating an XML file object, It then loads the XML file that was created before into the XML file object, and then gets an array of type XmlNodeList that holds the nodes under the XML, and then takes the data from the array to the text box. Here is the display data encapsulated in a function, because the display of data to use more places, so as to improve the utilization of code, modify this is also more convenient. (Note that you want to define an array of type XmlNodeList and an object of type XmlDocument as a global variable for easy use in other functions)

Methods to invoke when loading views!!!!

5. Finally, the main consideration is to consider two buttons to browse the address book, it is important to note that the display data is changed by the value of a to display the data of different contacts, it is possible to consider changing the value of a to display the previous contact or the next contact, as long as the click event to add code, When browsing to pay attention to an array of cross-border problems, a becomes larger than the array is the length of the time is no data, then the system will error, so should add a judgment statement, but also pay attention to the judgment statement placed in a value change front, (why write their own thinking, Tip: Consider what is going to happen when the length of the array is going to be greater, and one thing to note is that when you browse to the end of the array, the judgment statement gets the length of the array and a is zero, so the judgment statement has to be reduced by one.

The source code is as follows: (Master do not spray!!! )

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Xml;

namespace Contacts
{
public partial class Form1:form
{
int a = 0;
XmlDocument MyDoc;
XmlNodeList MyList;

Public Form1 ()
{
InitializeComponent ();
}

private void Label1_click (object sender, EventArgs e)
{

}

private void Form1_Load (object sender, EventArgs e)
{
Create an XML File object
MyDoc = new XmlDocument ();
Load the XML file into the MyDoc object
Mydoc.load ("Addressbook.xml");
Gets the child nodes under all parent nodes in the XML file
MyList = Mydoc.getelementsbytagname ("Linkman");

Show data
This.showinfo (a);


}

private void Lastone_click (object sender, EventArgs e)
{

The current table is less than or equal to 0 click event Execution return
if (a <= 0) return;
a--;
This.showinfo (a);
}

private void Nextone_click (object sender, EventArgs e)
{
A value that is greater than or equal to the total number of arrays minus one click event execution returns
if (a >= mylist. COUNT-1) return;
a++;
This.showinfo (a);

}

The method of displaying the data A is to call the parameter passed by this function, control the number of child nodes under the XML
private void Showinfo (int _a)
{
This.linkmanBox.Text = Mylist[a]. Attributes[0]. Value;

This.nameBox.Text = Mylist[a]. Childnodes[0]. InnerText;

This.sexBox.Text = Mylist[a]. CHILDNODES[1]. InnerText;

This.phoneNumberBox.Text = Mylist[a]. CHILDNODES[2]. InnerText;
}
}
}

XML stores Address book data and resolves it in a C # program to appear on the form

Related Article

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.