asp.net操作xml
1.xml文档Products.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.pro.org/2001/products" xsi:schemaLocation="http://www.pro.org/2001/products products.xsd"> 3 <item belong="数码"> 4 <id>1</id> 5 <name>手机</name> 6 <price>1000</price> 7 </item> 8 <item belong="服装"> 9 <id>2</id> 10 <name>男装</name> 11 <price>200</price> 12 </item> 13 <item belong="食品"> 14 <id>3</id> 15 <name>黄瓜</name> 16 <price>4</price> 17 </item> 18 </products>
2.schema约束文档 products.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.pro.org/2001/products" xmlns:pro="http://www.pro.org/2001/products" elementFormDefault="qualified"> 3 <element name="products" type="pro:pro"></element> 4 <complexType name="pro"> 5 <sequence> 6 <element name="item" maxOccurs="unbounded"> 7 <complexType> 8 <sequence> 9 <element name="id" type="string"></element> 10 <element name="name" type="string"></element> 11 <element name="price"> 12 <simpleType> 13 <restriction base="float"> 14 <maxExclusive value="10000"></maxExclusive> 15 <minInclusive value="0"></minInclusive> 16 </restriction> 17 </simpleType> 18 </element> 19 </sequence> 20 <attribute name="belong" type="string"></attribute> 21 </complexType> 22 </element> 23 </sequence> 24 </complexType> 25 </schema>
3.定义实体类 DBPro.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 /// <summary> 7 ///DBPro 的摘要说明 8 /// </summary> 9 public class DBPro 10 { 11 string belong; 12 string id; 13 string name; 14 string price; 15 public DBPro(string belong,string id,string name,string price) 16 { 17 this.belong = belong; 18 this.id = id; 19 this.name = name; 20 this.price = price; 21 } 22 public string Belong 23 { 24 get { return belong; } 25 set { belong = value; } 26 } 27 public string ID 28 { 29 get { return id; } 30 set { id = value; } 31 } 32 public string Name 33 { 34 get { return name; } 35 set{name=value;} 36 } 37 public string Price 38 { 39 get { return price; } 40 set { price = value; } 41 } 42 }
4.新建一个web窗体Defaut.aspx,在Default.aspx.cs中编写核心代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Xml; 8 9 public partial class _Default : System.Web.UI.Page 10 { 11 protected void Page_Load(object sender, EventArgs e) 12 { //选择方法进行测试 13 //SearchXml(); 14 DBPro pro = new DBPro("家电","10", "电视", "3999"); 15 AddToXml(pro); 16 //UpdateOneXml(pro); 17 //DeleteXml("10"); 18 } 19 /// <summary> 20 /// 遍历xml文档 21 /// </summary> 22 /// <param name="pro"></param> 23 private void SearchXml() 24 { 25 //提取xml文档 26 XmlDocument xd = new XmlDocument(); 27 xd.Load(System.Web.HttpContext.Current.Server.MapPath("Products.xml")); 28 //获取根节点 29 XmlNode root = xd.DocumentElement; 30 //获取节点列表 31 XmlNodeList items = root.ChildNodes; 32 //遍历item项 33 Response.Write("<pre>"); 34 foreach (XmlNode item in items) 35 { 36 //输出属性 37 Response.Write(item.Attributes["belong"].Name + "=" + item.Attributes["belong"].InnerText); 38 //遍历输出子节点 39 foreach (XmlNode p in item) 40 { 41 Response.Write(p.Name + "=" + p.InnerText); 42 } 43 } 44 Response.Write("</pre>"); 45 } 46 /// <summary> 47 /// xml添加 48 /// </summary> 49 /// <param name="pro"></param> 50 private void AddToXml(DBPro pro) 51 { 52 //提取xml文档 53 XmlDocument xd = new XmlDocument(); 54 xd.Load(System.Web.HttpContext.Current.Server.MapPath("Products.xml")); 55 //获取根节点 56 XmlNode root = xd.DocumentElement; 57 //创建元素 58 XmlElement newItem = xd.CreateElement("item"); 59 XmlElement newID = xd.CreateElement("id"); 60 XmlElement newName = xd.CreateElement("name"); 61 XmlElement newPrice = xd.CreateElement("price"); 62 //配置参数 63 newItem.SetAttribute("belong", pro.Belong); 64 newID.InnerText = pro.ID; 65 newName.InnerText = pro.Name; 66 newPrice.InnerText = pro.Price; 67 //装配 68 root.AppendChild(newItem); 69 newItem.AppendChild(newID); 70 newItem.AppendChild(newName); 71 newItem.AppendChild(newPrice); 72 xd.Save(System.Web.HttpContext.Current.Server.MapPath("Products.xml")); 73 } 74 /// <summary> 75 /// 修改xml一项 76 /// </summary> 77 /// <param name="pro"></param> 78 private void UpdateOneXml(DBPro pro) 79 { 80 //提取xml文档 81 XmlDocument xd = new XmlDocument(); 82 xd.Load(System.Web.HttpContext.Current.Server.MapPath("Products.xml")); 83 //获取根节点 84 XmlNode root = xd.DocumentElement; 85 //获取节点列表 86 XmlNodeList items = root.ChildNodes; 87 //遍历节点列表 88 foreach (XmlNode item in items) 89 { 90 //遍历item 91 foreach (XmlNode p in item) 92 { 93 if (p.Name == "id" && p.InnerText == pro.ID) 94 { 95 item.Attributes["belong"].InnerText = pro.Belong; 96 p.NextSibling.InnerText = pro.Name; 97 p.NextSibling.NextSibling.InnerText = pro.Price; 98 } 99 } 100 } 101 } 102 /// <summary> 103 /// 删除xml一项 104 /// </summary> 105 /// <param name="pro"></param> 106 private void DeleteXml(string id) 107 { 108 //提取xml文档 109 XmlDocument xd = new XmlDocument(); 110 xd.Load(System.Web.HttpContext.Current.Server.MapPath("Products.xml")); 111 //获取根节点 112 XmlNode root = xd.DocumentElement; 113 //获取节点列表 114 XmlNodeList items = root.ChildNodes; 115 //遍历节点列表 116 foreach (XmlNode item in items) 117 { 118 //遍历item 119 foreach (XmlNode p in item) 120 { 121 if (p.Name == "id" && p.InnerText == id) 122 { 123 root.RemoveChild(item); 124 } 125 } 126 } 127 } 128 }
此处应注意:用XMLDocument添加元素,遇到了这样一个问题:
当根节点具有 xmlns 属性时,用 XMLDocument 创建子元素时如果不指定 xmlns 或指定 xmlns 为 null 时,子元素将自动具有 xmlns="" 属性
<item belong="家电" xmlns="">
<id>10</id>
<name>电视</name>
<price>3999</price>
</item>
问题原因:
当父节点具有 xmlns 属性时,子节点必须指定 xmlns 属性,仅当子节点的 xmnls 属性与父节点相同时,子节点才不显示 xmlns 属性,最终就不会在 .xml 文件中显示出来
解决办法:
XmlElement newItem = xd.CreateElement("item",xd.DocumentElement.NamespaceURI);
XmlElement newID = xd.CreateElement("id",xd.DocumentElement.NamespaceURI);
XmlElement newName = xd.CreateElement("name",xd.DocumentElement.NamespaceURI);
XmlElement newNumber = xd.CreateElement("number",xd.DocumentElement.NamespaceURI);
在每一个下级节点,都要继续指定命名空间,否则仍会出现 xmlns="" 属性。
///新手上道,若有不足或错误之处,敬请各位大神批评指正!