麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

asp.net操作xml(增刪查改)

2019-11-17 02:12:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

asp.net操作xml(增刪查改)

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄色大片在线免费看 | 国产宾馆3p国语对白 | 久久探花 | 啊~用cao嗯力cao烂我视频 | 日本在线播放一区二区三区 | 日韩一级片一区二区三区 | 国产精品久久久久久久久久久久久久久久 | 素人视频免费观看 | 亚洲国产一区二区三区 | 日韩色视频在线观看 | 涩涩伊人 | 欧美一区二区三区不卡免费观看 | 黄色免费播放网站 | 精品国产观看 | 国产91亚洲精品一区二区三区 | 一级毛片真人免费播放视频 | 欧美视频一区二区三区 | 亚洲射情 | 免费h片 | 日本aaaa片毛片免费观看视频 | 在线a免费观看 | 国产99视频精品免视看9 | 国产四区 | 越南一级黄色片 | 成人国产视频在线观看 | 99极品视频 | 一级毛片电影院 | 欧美精品一区二区三区在线 | 成人一级毛片 | 91超在线 | 欧美一级在线免费 | 午夜精品久久久久久久久久久久久蜜桃 | 国人精品视频在线观看 | 亚洲免费视 | 欧美巨乳在线观看 | 国产一区二区影视 | 国产黄色毛片 | 性片久久 | 日本免费aaa观看 | 久久久久久久久久久高潮一区二区 | 国产精品久久久久久久hd |