星空网 > 软件开发 > ASP.net

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

asp.net操作

1.

 1 <??> 2 <products ="http://www.w3.org/2001/ ="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.

 1 <??> 2 <schema ="http://www.w3.org/2001/ targetNamespace="http://www.pro.org/2001/products" ="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 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.新建一个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. 8  9 public partial class _Default : System.Web.UI.Page 10 { 11   protected void Page_Load(object sender, EventArgs e) 12   {  //选择方法进行测试 13     //Search 14     DBPro pro = new DBPro("家电","10", "电视", "3999"); 15     AddTo 16     //UpdateOne 17     //Delete 18   } 19   /// <summary> 20   /// 遍历 21   /// </summary> 22   /// <param name="pro"></param> 23   private void Search 24   { 25     //提取 26      27     xd.Load(System.Web.HttpContext.Current.Server.MapPath("Products. 28     //获取根节点 29      30     //获取节点列表 31      32     //遍历item项 33     Response.Write("<pre>"); 34     foreach ( 35     { 36      //输出属性 37       Response.Write(item.Attributes["belong"].Name + "=" + item.Attributes["belong"].InnerText); 38       //遍历输出子节点 39       foreach ( 40       { 41         Response.Write(p.Name + "=" + p.InnerText); 42       } 43     } 44     Response.Write("</pre>"); 45   } 46   /// <summary> 47   ///  48   /// </summary> 49   /// <param name="pro"></param> 50   private void AddTo 51   { 52     //提取 53      54     xd.Load(System.Web.HttpContext.Current.Server.MapPath("Products. 55     //获取根节点 56      57     //创建元素 58      59      60      61      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. 73   } 74   /// <summary> 75   /// 修改 76   /// </summary> 77   /// <param name="pro"></param> 78   private void UpdateOne 79   { 80     //提取 81      82     xd.Load(System.Web.HttpContext.Current.Server.MapPath("Products. 83     //获取根节点 84      85     //获取节点列表 86      87     //遍历节点列表 88     foreach ( 89     { 90       //遍历item 91       foreach ( 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   /// 删除104   /// </summary>105   /// <param name="pro"></param>106   private void Delete107   {108      //提取109     110     xd.Load(System.Web.HttpContext.Current.Server.MapPath("Products.111     //获取根节点112     113     //获取节点列表114     115     //遍历节点列表116     foreach (117     {118       //遍历item119       foreach (120       {121         if (p.Name == "id" && p.InnerText == id)122         {123           root.RemoveChild(item);124         }125       }126     }127   }128 }

此处应注意:用
当根节点具有

<item belong="家电"
    <id>10</id>
    <name>电视</name>
    <price>3999</price>
  </item>
问题原因:

    当父节点具有

解决办法:

        
       
       
       

        在每一个下级节点,都要继续指定命名空间,否则仍会出现

///新手上道,若有不足或错误之处,敬请各位大神批评指正!

 




原标题:asp.net操作xml(增删查改)

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

《墨西哥vat和rfc在亚马逊后台的实施及其管理》:https://www.kjdsnews.com/a/1310755.html
《墨西哥rfc和vat详解》:https://www.kjdsnews.com/a/1310756.html
《商业企业如何快速适应欧盟的新的rfc和vat》:https://www.kjdsnews.com/a/1310757.html
《升级vat v9.5 - 简化财务流程,为您带来更多成功》:https://www.kjdsnews.com/a/1310758.html
《利用vataliti提升行业竞争力》:https://www.kjdsnews.com/a/1310759.html
《亚马逊欧洲vat税务期限即将到期,请尽快做好准备!》:https://www.kjdsnews.com/a/1310760.html
TikTok 将推出先买后付服务 :https://www.kjdsnews.com/a/1836651.html
TikTok 将推出先买后付服务 :https://www.goluckyvip.com/news/188219.html
我的浏览记录
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流