嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 1 元微信扫码支付:1 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
C#3层架构 Winform操作XML文件
Model层代码:
namespace OperationXML.Model
{
public class BookModel
{
private string _bookType;
public string BookType
{
get { return _bookType; }
set { _bookType = value; }
}
private int _bookID;
public int BookID
{
get { return _bookID; }
set { _bookID = value; }
}
private string _bookName;
public string BookName
{
get { return _bookName; }
set { _bookName = value; }
}
private string _bookAuthor;
public string BookAuthor
{
get { return _bookAuthor; }
set { _bookAuthor = value; }
}
private string _bookPrice;
public string BookPrice
{
get { return _bookPrice; }
set { _bookPrice = value; }
}
}
}
using OperationXML.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
namespace OperationXML.DAL
{
public class BookDal
{
static string strPath = "Book.xml";
public void CreatBookInfo(BookModel _bookmodel)
{
XmlDocument doc = new XmlDocument(); //实例化XmlDocument对象
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null); //XML头
doc.AppendChild(dec);
XmlElement root = doc.CreateElement("bookstore"); //根节点
doc.AppendChild(root);
XmlElement xml0 = doc.CreateElement("book"); //子根节点
xml0.SetAttribute("课程类型", _bookmodel.BookType); //设置属性。由UI端写入数据。
xml0.SetAttribute("编号", Convert.ToString(_bookmodel.BookID));
XmlElement xml1 = doc.CreateElement("书名");//创建一个书名节点
xml1.InnerText = _bookmodel.BookName;
xml0.AppendChild(xml1);//添加
XmlElement xml2 = doc.CreateElement("作者");//创建作者节点
xml2.InnerText = _bookmodel.BookAuthor;
xml0.AppendChild(xml2);//添加
XmlElement xml3 = doc.CreateElement("价格");//创建一个价格节点
xml3.InnerText = _bookmodel.BookPrice;
xml0.AppendChild(xml3);//添加
root.AppendChild(xml0);
doc.Save("Book.xml");
} //创建
public DataSet LoadBookInfo()
{
DataSet ds = new DataSet();
ds.ReadXml(strPath);
return ds;
} //加载到datagridview中
public void AddBookInfo(BookModel _bookmodel)
{
XmlDocument doc = new XmlDocument();
doc.Load("Book.xml");
XmlNode root = doc.SelectSingleNode("bookstore");
XmlElement xelKey = doc.CreateElement("book");
XmlAttribute a = doc.CreateAttribute("课程类型");
a.InnerText = _bookmodel.BookType;
xelKey.SetAttributeNode(a);
XmlAttribute b = doc.CreateAttribute("编号");
b.InnerText = Convert.ToString(_bookmodel.BookID);
xelKey.SetAttributeNode(b);
XmlElement c = doc.CreateElement("书名");
c.InnerText = _bookmodel.BookName;
xelKey.AppendChild(c);
XmlElement d = doc.CreateElement("作者");
d.InnerText = _bookmodel.BookAuthor;
xelKey.AppendChild(d);
XmlElement e = doc.CreateElement("价格");
e.InnerText = _bookmodel.BookPrice;
xelKey.AppendChild(e);
root.AppendChild(xelKey);
doc.Save("Book.xml");
} //增加
public void DeleteBookInfo(string a)
{
XElement xe = XElement.Load("Book.xml");
IEnumerable<XElement> elements = from ele in xe.Elements("book")
where ele.Attribute("编号").Value == a
select ele;
if (elements.Count() > 0)
{
elements.First().Remove();
}
xe.Save("Book.xml");
} //删除
public void UpdateBookInfo(BookModel a)
{
XElement xe = XElement.Load("Book.xml");
IEnumerable<XElement> element = from ele in xe.Elements("book")
where ele.Attribute("编号").Value == Convert.ToString(a.BookID)
select ele;
if (element.Count() > 0)
{
XElement first = element.First();
//设置新的属性
first.SetAttributeValue("课程类型", a.BookType);
first.SetAttributeValue("编号", a.BookID);
//替换新的节点
first.ReplaceNodes(
new XElement("书名", a.BookName),
new XElement("作者", a.BookAuthor),
new XElement("价格", a.BookPrice)
);
}
xe.Save("Book.xml");
} //更改
public List<BookModel> QueryBookInfo(int intFalg,string c)
{
List<BookModel> bookList = new List<BookModel>();
XElement xml = XElement.Load(strPath);
var bookVar = xml.Descendants("book"); //默认查询所有图书
switch (intFalg) {
case 1: //"根据编号查询"
bookVar = xml.Descendants("book").Where(a => a.Attribute("编号").Value ==c); //c由UI端写入数据传到这儿
break;
case 2://"根据书名查询"
bookVar = xml.Descendants("book").Where(a => a.Element("书名").Value == c);
break;
case 3://"根据作者查询"
bookVar = xml.Descendants("book").Where(a => a.Element("作者").Value == c);
break;
}
bookList = (from book in bookVar
select new BookModel
{
BookID = int.Parse(book.Attribute("编号").Value),
BookType = book.Attribute("课程类型").Value,
BookName = book.Element("书名").Value,
BookAuthor = book.Element("作者").Value,
BookPrice = book.Element("价格").Value,
}).ToList();
return bookList;
} //查询
}
}
BLL层代码:
using OperationXML.DAL;
using OperationXML.Model;
using System.Collections.Generic;
using System.Data;
namespace OperationXML.BLL
{
public class BookBll
{
BookModel _person= new BookModel();
BookDal _a = new BookDal();
public DataSet PersonLoad()
{
DataSet ds = _a.LoadBookInfo();
return ds;
} //加载
public void Creat(BookModel n)
{
_a.CreatBookInfo(n);
} //创建
public void Add(BookModel m)
{
_a.AddBookInfo(m);
} //添加
public void Delete(string _bookmodel)
{
_a.DeleteBookInfo(_bookmodel);
} //删除
public void Update(BookModel d)
{
_a.UpdateBookInfo(d);
} //更改
public List<BookModel> Query( int intFalg,string c)
{
List<BookModel> list = new List<BookModel>();
list = _a.QueryBookInfo(intFalg,c);
return list;
} //查询
}
}