Có rất nhiều bạn trong lớp không làm được thao tác này thêm một node vào xml hay nói dúng hơn là thêm một sinh viên vào file SinhVien.xml hoặc các bạn thêm vào rồi mà dữ liệu cũ đã mất chỉ tồn tại một node mới thôi. mình sẽ hướng dẫn các bạn insert node xml hay hơn và dễ hiểu hơn là cách thêm cảu thầy dạy :
Bài demo:
Tạo file cls_sinhVien.cs :
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
///
/// Summary description for class_SinhVien
///
public class class_SinhVien
{
private string _strMaSSV;
private string _strTenSV;
private string _strLop;
public class_SinhVien()
{
this._strMaSSV = "";
this._strTenSV = "";
this._strLop = "";
}
public class_SinhVien(string massv,string tensv,string lop)
{
this._strMaSSV = massv;
this._strTenSV = tensv;
this._strLop = lop;
}
public void Insert_SinhVien(string fileName)
{
XmlDocument xmldocment = new XmlDocument();
xmldocment.Load(fileName);
XmlElement xmlelement_SinhVien = xmldocment.CreateElement("SinhVien");
xmlelement_SinhVien.SetAttribute("id",_strMaSSV);
//massv
XmlElement xmlelement_Node;// = xmldocment.CreateElement("MaSSV");
http://xmlelement_Node.InnerText = _strMaSSV;
http://xmlelement_SinhVien.InsertAfter(xmlelement_Node,xmlelement_SinhVien.LastChild);
//hoten sinh vien
xmlelement_Node = xmldocment.CreateElement("HoVaTen");
xmlelement_Node.InnerText = _strTenSV;
xmlelement_SinhVien.InsertAfter(xmlelement_Node,xmlelement_SinhVien.LastChild);
//lop
xmlelement_Node = xmldocment.CreateElement("Lop");
xmlelement_Node.InnerText = _strLop;
xmlelement_SinhVien.InsertAfter(xmlelement_Node,xmlelement_SinhVien.LastChild);
//danh muc mon hoc
xmlelement_Node = xmldocment.CreateElement("DanhMucMonHoc");
xmlelement_Node.InnerText = "";
xmlelement_SinhVien.InsertAfter(xmlelement_Node,xmlelement_SinhVien.LastChild);
XmlNode xml_node = xmldocment.LastChild;
xml_node.InsertAfter(xmlelement_SinhVien,xml_node.LastChild);
xmldocment.Save(fileName);
}
}
Tại code design ThemSinhVien.aspx :
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnHuy_Click(object sender, EventArgs e)
{
txtHoVaTen.Text = "";
txtMaSSV.Text = "";
dropDownList_lop.SelectedIndex = 0;
}
protected void btnThem_Click(object sender, EventArgs e)
{
string fileName = Server.MapPath("xml_SinhVien.xml");
class_SinhVien cls_sv = new class_SinhVien(txtMaSSV.Text,txtHoVaTen.Text,dropDownList_lop.SelectedValue.ToString());
cls_sv.Insert_SinhVien(fileName);
}
}
Danh mục môn học thêm như sau :
public void Insert_DanhMucMonHoc(string strFileName, string str_MaSSV)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);
XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName("SinhVien");
foreach (XmlNode node in xmlNodeList)
{
if (node.Attributes["id"].Value == str_MaSSV)
{
foreach (XmlElement ele in node.ChildNodes)
{
if (ele.LocalName == "DanhMucMonHoc")
{
XmlElement e_new_MH = xmlDoc.CreateElement("MaMH");
e_new_MH.SetAttribute("id", _strMaMH);
XmlElement e_new = xmlDoc.CreateElement("TenMH");
e_new.InnerText = _strTenMH;
e_new_MH.InsertAfter(e_new, e_new_MH.LastChild);
e_new = xmlDoc.CreateElement("SoTC");
e_new.InnerText = _intSoTC.ToString();
e_new_MH.InsertAfter(e_new, e_new_MH.LastChild);
e_new = xmlDoc.CreateElement("DiemThi");
e_new.InnerText = _decDiemThi.ToString();
e_new_MH.InsertAfter(e_new, e_new_MH.LastChild);
XmlNode xmlNode = node.LastChild;
xmlNode.InsertAfter(e_new_MH, xmlNode.LastChild);
}
}
}
}
xmlDoc.Save(strFileName);
}
Kiểm tra môn học trùng :
public bool KiemTraMaMonHocTrung(string strFileName, string strMaMH, string strMaSSV)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);
XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName("SinhVien");
foreach (XmlNode node in xmlNodeList)
{
if (node.Attributes["id"].Value == strMaSSV)
{
foreach (XmlNode node_con in node.ChildNodes)
{
if (node_con.LocalName == "DanhMucMonHoc")
{
foreach (XmlElement e in node_con.ChildNodes)
{
if (e.Attributes["id"].Value == strMaMH)
{
return false;
break;
}
}
}
}
}
}
return true;
}
File demo : http://www.mediafire.com/?kb7tb75siytzq6r
Bài này có hơi khó hiều nhưng các bạn chụi khó một chút thì rất dễ hiểu. Nhưng hãy cẩn thận với vị trí insert node.Có trường hợp insert được có trường ợp không. Nhưng vì đây là công nghệ mới nên có nhiều hạn chế lắm, các bạn hãy tìm hiểu kỹ hơn trên một số trang web khác nha. Chúc tành công!!!