Hiển thị kết quả từ 1 đến 3 / 3
  1. #1
    Tham gia
    06-01-2011
    Bài viết
    1
    Like
    0
    Thanked 0 Times in 0 Posts

    liên kết xml với form c#

    Mình đang làm form để lưu thông tin vào file xml (thay csdl ),nhưng có lỗi chút ,ai biết thì chỉ giúp với !

    - Đâu tiên có 1 form
    http://i909.photobucket.com/albums/a...untitled-2.jpg

    - Thứ 2 : ta tao add file xsd

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="khontin"
    targetNamespace="http://tempuri.org/khontin.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/khontin.xsd"
    xmlns:mstns="http://tempuri.org/khontin.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    >
    <xs:simpleType name="ma">
    <xs:restriction base="xs:ID">
    <xs:minLength value="0"/>
    <xs:maxLength value="6"/>
    </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="ten">
    <xs:restriction base="xs:string">
    <xs:minLength value="0"/>
    <xs:maxLength value="100"/>
    </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="sothuc">
    <xs:restriction base="xs:float">
    <xs:minInclusive value="1"/>
    <xs:maxInclusive value="100"/>
    </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="SINHVIEN">
    <xs:sequence>
    </xs:sequence>
    <xs:attribute name="MaSV" type="ma"/>
    <xs:attribute name="HoTen" type="ten"/>
    <xs:attribute name="DTB" type="sothuc"/>
    </xs:complexType>
    <xs:complexType name="LOP">
    <xs:sequence>
    <xs:element name="SINHVIEN" type="SINHVIEN" maxOccurs="10"/>
    </xs:sequence>
    <xs:attribute name="MaLop" type="ma"/>
    <xs:attribute name="TenLop" type="ten"/>
    </xs:complexType>
    <xs:element name="thongtinsv">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="LOP" type="LOP" maxOccurs="3"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>


    - Thư 3 :từ file xsd ta tạo ra file xml


    <?xml version="1.0" encoding="utf-8" ?>
    <thongtinsv xmlns="http://tempuri.org/khontin.xsd">
    <LOP MaLop="F5345" TenLop="GEWRGR">
    <SINHVIEN MaSV="F3252" HoTen="ẺGWERG" DTB="4"/>
    <SINHVIEN MaSV="O3252" HoTen="ẺGWERG" DTB="4"/>
    <SINHVIEN MaSV="R3252" HoTen="ẺGWERG" DTB="4"/>
    <SINHVIEN MaSV="T3252" HoTen="ẺGWERG" DTB="4"/>
    </LOP>
    </thongtinsv>


    -tiếp đên là viết code để lưu dữ liệu từ form vào file xml

    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 WindowsFormsApplication6
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void btThemSV_Click(object sender, EventArgs e)
    {
    SinhVien SV = new SinhVien();
    SV.MSSV = tbMssv.Text.Trim();
    SV.HoTen = tbHoTen.Text.Trim();
    SV.DTB = float.Parse(tbDTB.Text.Trim());
    try
    {
    XmlDocument tailieu = new XmlDocument();
    tailieu.Load("..\\..\\SinhVien.xml");
    XmlElement goc = tailieu.DocumentElement;
    XmlElement S = tailieu.CreateElement("SINHVIEN");
    S.SetAttribute("MaSV", SV.MSSV);
    S.SetAttribute("HoTen", SV.HoTen);
    S.SetAttribute("DTB", SV.DTB.ToString());
    goc.AppendChild(S);
    tailieu.Save(".\\..\\SinhVien.xml");
    MessageBox.Show("them da thanh cong");
    }
    catch (Exception)
    {
    MessageBox.Show("them da that bai");
    }


    }
    }
    }
    //////////////////////////////////
    thêm class SinhVien

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace WindowsFormsApplication6
    {
    public class SinhVien
    {

    private string mSSV;

    public string MSSV
    {
    get { return mSSV; }
    set { mSSV = value; }
    }
    private string hoTen;

    public string HoTen
    {
    get { return hoTen; }
    set { hoTen = value; }
    }
    private float dTB;

    public float DTB
    {
    get { return dTB; }
    set { dTB = value; }
    }
    private string lopHoc;
    public string LopHoc
    {
    get { return lopHoc; }
    set { lopHoc = value; }
    }
    }
    }
    ////////////////////////////////
    nhưng sao khi lưu file xml rồi nhưng nó không chèn vào Lop mà nó tạo ra phần tử "SinhVien" cùng cấp với "Lớp"
    nhưng mình đang muốn là "SinhVien" là con của "Lớp"

    ai biết thì chỉ giúp với
    Quote Quote

  2. #2
    Tham gia
    15-11-2005
    Location
    Hà Nội
    Bài viết
    696
    Like
    0
    Thanked 14 Times in 12 Posts
    Có phải là cái này không bạn:

    Lưu dữ liệu từ winform xuống file XML

  3. #3
    Tham gia
    30-12-2010
    Bài viết
    2
    Like
    0
    Thanked 0 Times in 0 Posts
    Bạn phải chọn element Lop rồi mới tạo element sinhvien
    private void btThemSV_Click(object sender, EventArgs e)
    {
    SinhVien SV = new SinhVien();
    SV.MSSV = tbMssv.Text.Trim();
    SV.HoTen = tbHoTen.Text.Trim();
    SV.DTB = float.Parse(tbDTB.Text.Trim());
    try
    {
    XmlDocument tailieu = new XmlDocument();
    tailieu.Load("..\\..\\SinhVien.xml");
    //XmlElement goc = tailieu.DocumentElement;
    // Thay dòng trên bằng dòng này
    XmlElement goc = tailieu.SingleNode("LOP");
    XmlElement S = tailieu.CreateElement("SINHVIEN");
    S.SetAttribute("MaSV", SV.MSSV);
    S.SetAttribute("HoTen", SV.HoTen);
    S.SetAttribute("DTB", SV.DTB.ToString());
    goc.AppendChild(S);
    tailieu.Save(".\\..\\SinhVien.xml");
    MessageBox.Show("them da thanh cong");
    }
    catch (Exception)
    {
    MessageBox.Show("them da that bai");
    }
    }
    }
    }

Bookmarks

Quy định

  • Bạn không thể tạo chủ đề mới
  • Bạn không thể trả lời bài viết
  • Bạn không thể gửi file đính kèm
  • Bạn không thể sửa bài viết của mình
  •