Pass:看公司代码,配置下拉框的功能,和下拉框的数字转文字。配置xml里面有下拉的value,name,这样界面直接显示数字,然后转译成中文
1.xml文件格式
<?xml version="1.0" encoding="utf-8"?> <bookstore> <book category="COOKING" id="1"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>2222</price> </book> </bookstore>
2.认识xml的基本操作的几个基本概念
net操作xml有基本的XMLDocument、linq
2.1 XmlDocument : XmlDocument将XML视为树状结构,它装载XML文档,并在内存中构建该文档的树状结构。下面来看下XmlDocument提供了哪些功能。
2.2 XmlNode:节点
2.3 XmlElement:元素
2.4几个基本操作
获取节点的值:?.InnerText ;
获取节点的属性:?.Attributes["id"].InnerText ;
3.xml的增删改查
3.1 最基本
public book getModel() { //封装获取数据 book b = new book(); b.author = HttpContext.Current.Request["author"]; b.category = HttpContext.Current.Request["category"]; b.lang = HttpContext.Current.Request["lang"]; b.price = Convert.ToDouble(HttpContext.Current.Request["price"]); b.title = HttpContext.Current.Request["title"]; b.year = Convert.ToInt16(HttpContext.Current.Request["year"]); return b; } public List<book> readXMl() { List<book> ls = new List<book> { }; //1.载入xml XmlDocument doc = new XmlDocument(); doc.Load(xmlPath); //2.获取文档下第一个子类的所以子类 XmlNodeList listNode = doc.SelectSingleNode("bookstore").ChildNodes; //3.遍历子类 foreach (XmlNode books in listNode) { book b = new book(); b.Id = Convert.ToInt16(books.Attributes["id"].InnerText); b.category = books.Attributes["category"].InnerText; b.title = books["title"].InnerText; b.lang = books["title"].Attributes["lang"].InnerText; b.author = books["author"].InnerText; b.year = Convert.ToInt16(books["year"].InnerText); b.price = Convert.ToDouble(books["price"].InnerText); ls.Add(b); } return ls; } public book readXMLById() { book b = getModel(); //需要查询数据的Id b.Id = Convert.ToInt16(HttpContext.Current.Request["id"]); XmlDocument root = new XmlDocument(); root.Load(xmlPath); XmlNode xml = root.SelectSingleNode("bookstore"); XmlNodeList listnode = xml.ChildNodes; //遍历bookstore下的节点 foreach (XmlNode i in listnode) { //获取每一个book的属性Id对比是否=需要查询的值 if (i.Attributes["id"].InnerText == b.Id.ToString()) { b.category = i.Attributes[0].InnerText; b.title = i["title"].InnerText; b.lang = i["title"].Attributes["lang"].InnerText; b.author = i["author"].InnerText; b.year = Convert.ToInt16(i["year"].InnerText); b.price = Convert.ToDouble(i["price"].InnerText); } } return b; } public bool WriteXml() { bool isValue = false; try { //设置模型类,只是为了更好的看到数据 book b = getModel(); //创建xml文件 XmlDocument xml = new XmlDocument(); xml.Load(xmlPath); //最高级树 XmlNode root = xml.SelectSingleNode("bookstore"); int id = Convert.ToInt16(root.LastChild.Attributes["id"].InnerText) + 1; //二级子节点 XmlElement node = xml.CreateElement("book"); node.SetAttribute("category", b.category); node.SetAttribute("id", id.ToString()); //三级子节点 XmlElement n1 = xml.CreateElement("title"); n1.SetAttribute("lang", b.lang); n1.InnerText = b.title; XmlElement n2 = xml.CreateElement("author"); n2.InnerText = b.author; XmlElement n3 = xml.CreateElement("year"); n3.InnerText = b.year.ToString(); XmlElement n4 = xml.CreateElement("price"); n4.InnerText = b.price.ToString(); //三级添加到二级 node.AppendChild(n1); node.AppendChild(n2); node.AppendChild(n3); node.AppendChild(n4); //二级添加到一级 root.AppendChild(node); //把文件保存到路径 xml.Save(xmlPath); isValue = true; } catch (Exception e) { HttpContext.Current.Response.Write(e); } return isValue; } public bool delXMLById() { bool isValue = false; book b = getModel(); b.Id = Convert.ToInt16(HttpContext.Current.Request["id"]); //以上为获取数据 XmlDocument root = new XmlDocument(); root.Load(xmlPath); XmlNode xml = root.SelectSingleNode("bookstore"); XmlNodeList listnode = xml.ChildNodes; foreach (XmlNode i in listnode) { if (i.Attributes["id"].InnerText.ToString() == b.Id.ToString()) { xml.RemoveChild(i); root.Save(xmlPath); isValue = true; } } return isValue; } public bool upXMLByModel() { book b = getModel(); bool isValue = false; b.Id = Convert.ToInt16(HttpContext.Current.Request["id"]); //以上为获取数据:request[""] XmlDocument root = new XmlDocument(); root.Load(xmlPath); XmlNode xml = root.SelectSingleNode("bookstore"); XmlNodeList xs = xml.ChildNodes; foreach (XmlNode x in xs) { if (x.Attributes["id"].InnerText.ToString() == b.Id.ToString()) { XmlElement node = x as XmlElement; node.SetAttribute("category", b.category); //三级树 XmlElement n1 = node.SelectSingleNode("title") as XmlElement; n1.SetAttribute("lang", b.lang); n1.InnerText = b.title; XmlElement n2 = node.SelectSingleNode("author") as XmlElement; n2.InnerText = b.author; XmlElement n3 = node.SelectSingleNode("year") as XmlElement; n3.InnerText = b.year.ToString(); XmlElement n4 = node.SelectSingleNode("price") as XmlElement; n4.InnerText = b.price.ToString(); root.Save(xmlPath); isValue = true; } } return isValue; }
3.2 linq操作
4.代码包
http://pan.baidu.com/s/1b6ReEU