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="數(shù)碼"> 4 <id>1</id> 5 <name>手機(jī)</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.定義實(shí)體類 DBPro.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 /// <summary> 7 ///DBPro 的摘要說(shuō)明 8 /// </summary> 9 public class DBPro10 {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 Belong23 {24 get { return belong; }25 set { belong = value; }26 }27 public string ID28 {29 get { return id; }30 set { id = value; }31 }32 public string Name33 {34 get { return name; }35 set{name=value;}36 }37 public string Price38 {39 get { return price; }40 set { price = value; }41 }42 }
4.新建一個(gè)web窗體Defaut.aspx,在Default.aspx.cs中編寫(xiě)核心代碼
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 { //選擇方法進(jìn)行測(cè)試 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 //獲取根節(jié)點(diǎn) 29 XmlNode root = xd.DocumentElement; 30 //獲取節(jié)點(diǎn)列表 31 XmlNodeList items = root.ChildNodes; 32 //遍歷item項(xiàng) 33 Response.Write("<pre>"); 34 foreach (XmlNode item in items) 35 { 36 //輸出屬性 37 Response.Write(item.Attributes["belong"].Name + "=" + item.Attributes["belong"].InnerText); 38 //遍歷輸出子節(jié)點(diǎn) 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 //獲取根節(jié)點(diǎn) 56 XmlNode root = xd.DocumentElement; 57 //創(chuàng)建元素 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 //配置參數(shù) 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一項(xiàng) 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 //獲取根節(jié)點(diǎn) 84 XmlNode root = xd.DocumentElement; 85 //獲取節(jié)點(diǎn)列表 86 XmlNodeList items = root.ChildNodes; 87 //遍歷節(jié)點(diǎn)列表 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一項(xiàng)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 //獲取根節(jié)點(diǎn)112 XmlNode root = xd.DocumentElement;113 //獲取節(jié)點(diǎn)列表114 XmlNodeList items = root.ChildNodes;115 //遍歷節(jié)點(diǎn)列表116 foreach (XmlNode item in items)117 {118 //遍歷item119 foreach (XmlNode p in item)120 {121 if (p.Name == "id" && p.InnerText == id)122 {123 root.RemoveChild(item);124 }125 }126 }127 }128 }
此處應(yīng)注意:用XMLDocument添加元素,遇到了這樣一個(gè)問(wèn)題:當(dāng)根節(jié)點(diǎn)具有 xmlns 屬性時(shí),用 XMLDocument 創(chuàng)建子元素時(shí)如果不指定 xmlns 或指定 xmlns 為 null 時(shí),子元素將自動(dòng)具有 xmlns="" 屬性
<item belong="家電" xmlns=""> <id>10</id> <name>電視</name> <price>3999</price> </item>問(wèn)題原因:
當(dāng)父節(jié)點(diǎn)具有 xmlns 屬性時(shí),子節(jié)點(diǎn)必須指定 xmlns 屬性,僅當(dāng)子節(jié)點(diǎn)的 xmnls 屬性與父節(jié)點(diǎn)相同時(shí),子節(jié)點(diǎn)才不顯示 xmlns 屬性,最終就不會(huì)在 .xml 文件中顯示出來(lái)
解決辦法:
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.Name
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注