Parse the XML file and write the data inside the file C # source code

Source: Internet
Author: User

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 Student Management System
{
public partial class Form1:form
{
XmlDocument MyDoc;
XmlNodeList MyList;
int current = 0;
Public Form1 ()
{
InitializeComponent ();
}

private void Form1_Load (object sender, EventArgs e)
{
Show data when the program is just starting to run
This.loadxmlnodelist ();
This.showstudentinfo (current);
}
Loading an XML file
private void Loadxmlnodelist ()
{
MyDoc = new XmlDocument ();
Mydoc.load ("Students.xml");
MyList = Mydoc.getelementsbytagname ("student");
}
Show Student Information
private void Showstudentinfo (int _current) The value returned by a function of type//void cannot be of type bool
{
Judge that the subscript value is out of bounds and returns directly.
if (Current < 0 | |, current > MyList. COUNT-1) return;

This.snoBox.Text = Mylist[current]. Attributes[0]. Value;

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

This.yearBox.Text = Mylist[current]. CHILDNODES[1]. Childnodes[0]. InnerText;

This.monthBox.Text = Mylist[current]. CHILDNODES[1]. CHILDNODES[1]. InnerText;

This.dayBox.Text = Mylist[current]. CHILDNODES[1]. CHILDNODES[2]. InnerText;
if (current = = 0)
this.lastOneBtn.Enabled = false;
if (current = = MyList. COUNT-1)
this.nextOneBtn.Enabled = false;

}
/** Browsing previous Student information * *
private void Lastonebtn_click (object sender, EventArgs e)
{
current--;
This.nextOneBtn.Enabled = true;
This.showstudentinfo (current);
}
/** Browsing previous Student information * *
private void Nextonebtn_click (object sender, EventArgs e)
{
current++;
This.lastOneBtn.Enabled = true;
This.showstudentinfo (current);
}
/** Add Information button **/
private void Addinfobtn_click (object sender, EventArgs e)
{
Click Add Info to display the Save button and Cancel button
This.addSaveBtn.Visible = true;
This.addCancleBtn.Visible = true;
When you click the button, the contents of all the text boxes on the form become empty.
This.snoBox.Text =null;//null and "" No spaces in the middle of the expression is not the same, in memory is not the same way of storage
This.nameBox.Text = null;
This.yearBox.Text = null;
This.monthBox.Text = null;
This.dayBox.Text = null;


}
/** Cancel Button **/
private void Addcanclebtn_click (object sender, EventArgs e)
{
This.addSaveBtn.Visible = false;
This.addCancleBtn.Visible = false;
This.showstudentinfo (current);
}
/** Save Button **/
private void Addsavebtn_click (object sender, EventArgs e)
{
Create a node with a node name of student
XmlElement Studentnode = mydoc.createelement ("student");
Create an attribute with a property name of ID
XmlAttribute Idnode = Mydoc.createattribute ("id");
id attribute Assignment Value
Idnode. Value = This.snoBox.Text;
Adding attributes to a node
Studentnode. Attributes.append (Idnode);
XmlElement Namenode = mydoc.createelement ("name");
Assigning a value to a created node
Namenode. InnerText = This.nameBox.Text;
Studentnode. AppendChild (Namenode);
XmlElement Birthnode = mydoc.createelement ("Birth");
XmlElement Birthyearnode = Mydoc.createelement ("year");
Birthyearnode. InnerText = This.yearBox.Text;
Birthnode. AppendChild (Birthyearnode);
XmlElement Birthmontynode = mydoc.createelement ("month");
Birthmontynode. InnerText = This.monthBox.Text;
Birthnode. AppendChild (Birthmontynode);
XmlElement Birthdaynode = mydoc.createelement ("Day");
Birthdaynode. InnerText = This.dayBox.Text;
Birthnode. AppendChild (Birthdaynode);
Studentnode. AppendChild (Birthnode);
MYDOC.CHILDNODES[1]. AppendChild (Studentnode);
Mydoc.save ("Students.xml");

Prevents multiple data from being saved by pressing the Save button
This.addSaveBtn.Visible = false;
This.addCancleBtn.Visible = false;
}

Package Save button
private void Saveinfo (String _nodename,string _nodetext)
//{

XmlElement Childnode = mydoc.createelement (_nodename);
Childnode.innertext = _nodetext;
Studentnode. Attributes.append (Idnode);
//}
}
}

Parse the XML file and write the data inside the file C # source code

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.