asp.net 操作xml(添加删除修改查询)

asp.net创建xml就是通过创建DataTable来创建xml中的树

型等
 DataSet objset=new DataSet();
          DataTable istable=new DataTable("test");
         istable.Columns.Add("rate1",typeof(int));
                 istable.Columns.Add

("rate2",typeof(int));
                 istable.Columns.Add

("rate3",typeof(int));
                 istable.Columns.Add

("rate4",typeof(int));
         objset.Tables.Add(istable);
         
         DataRow dr=objset.Tables["test"].NewRow

();
        dr[0]=赋值;
        dr[1]=赋值;
        dr[3]=赋值;
       objset.Tables["money"].Rows.Add(dr);        

 
        objset.WriteXml(Server.MapPath

("test.xml"),XmlWriteMode.WriteSchema);


        其中就是先创建DataSet和DataTable,其中建立

的表为test,再在表中添加子项rate1,2,3,4,再定义新

的行,分别添加对应的值,最后这些都已经写进DataSet

表中,通过DataSet把xml输入就完成了。要读就只需要把

xml的数据读到DataSet中,再通过 DataSet中的表的项来

对应读出数据。

        而C#中则使用的DOM来实现操作,比如现有一个

bookstore.xml文件,内容如下
        <?xml version="1.0" encoding="gb2312"?>
        <bookstore>
          <book genre="fantasy" ISBN="2-3631-4">
             <title>Oberon's Legacy</title>
             <author>Corets, Eva</author>
             <price>5.95</price>
          </book>
        </bookstore>
        下面讲解4种常用的方法
  1、往<bookstore>节点中插入一个<book>节点:

   XmlDocument xmlDoc=new XmlDocument();
   xmlDoc.Load("bookstore.xml");
   XmlNode root=xmlDoc.SelectSingleNode

("bookstore");//查找<bookstore>
   XmlElement xe1=xmlDoc.CreateElement("book");//

创建一个<book>节点
   xe1.SetAttribute("genre","李赞红");//设置该节点

genre属性
   xe1.SetAttribute("ISBN","2-3631-4");//设置该节

点ISBN属性
 
   XmlElement xesub1=xmlDoc.CreateElement

("title");
   xesub1.InnerText="CS从入门到精通";//设置文本节

点
   xe1.AppendChild(xesub1);//添加到<book>节点中
   XmlElement xesub2=xmlDoc.CreateElement

("author");
   xesub2.InnerText="候捷";
   xe1.AppendChild(xesub2);
   XmlElement xesub3=xmlDoc.CreateElement

("price");
   xesub3.InnerText="58.3";
   xe1.AppendChild(xesub3);
   
   root.AppendChild(xe1);//添加到<bookstore>节点中
   xmlDoc.Save("bookstore.xml");
   //================
   结果为:
   
   <?xml version="1.0" encoding="gb2312"?>
   <bookstore>
    <book genre="fantasy" ISBN="2-3631-4">
     <title>Oberon's Legacy</title>
     <author>Corets, Eva</author>
     <price>5.95</price>
    </book>
    <book genre="李赞红" ISBN="2-3631-4">
     <title>CS从入门到精通</title>
     <author>候捷</author>
     <price>58.3</price>
    </book>
   </bookstore>


  2、修改节点:将genre属性值为“李赞红“的节点的

genre值改为“update李赞红”,将该节点的子节点

<author>的文本修改为“亚胜”。

   XmlNodeList nodeList=xmlDoc.SelectSingleNode

("bookstore").ChildNodes;//获取bookstore节点的所有

子节点
   foreach(XmlNode xn in nodeList)//遍历所有子节点
   {
       XmlElement xe=(XmlElement)xn;//将子节点类型

转换为XmlElement类型
       if(xe.GetAttribute("genre")=="李赞红")//如

果genre属性值为“李赞红”
       {
           xe.SetAttribute("genre","update李赞

红");//则修改该属性为“update李赞红”
           XmlNodeList nls=xe.ChildNodes;//继续获

取xe子节点的所有子节点
           foreach(XmlNode xn1 in nls)//遍历
           {
               XmlElement xe2=(XmlElement)xn1;//转

换类型
               if(xe2.Name=="author")//如果找到
               {
                   xe2.InnerText="亚胜";//则修改
                   break;//找到退出来就可以了
               }
           }
           break;
       }
   }
 
   xmlDoc.Save("bookstore.xml");//保存。
   //=================

   最后结果为:

   <?xml version="1.0" encoding="gb2312"?>
   <bookstore>
    <book genre="fantasy" ISBN="2-3631-4">
     <title>Oberon's Legacy</title>
     <author>Corets, Eva</author>
     <price>5.95</price>
    </book>
    <book genre="update李赞红" ISBN="2-3631-4">
     <title>CS从入门到精通</title>
     <author>亚胜</author>
     <price>58.3</price>
    </book>
   </bookstore>


  3、删除 <book genre="fantasy" ISBN="2-3631-4">节

点的genre属性,删除 <book genre="update李赞红" 

ISBN="2-3631-4">节点。

   XmlNodeList xnl=xmlDoc.SelectSingleNode

("bookstore").ChildNodes;
 
   foreach(XmlNode xn in xnl)
   {
      XmlElement xe=(XmlElement)xn;
 
      if(xe.GetAttribute("genre")=="fantasy")
      {
          xe.RemoveAttribute("genre");//删除genre

属性
      }
      else if(xe.GetAttribute("genre")=="update李

赞红")
      {
          xe.RemoveAll();//删除该节点的全部内容
      }
   }
   xmlDoc.Save("bookstore.xml");
   //====================
  
   最后结果为:

   <?xml version="1.0" encoding="gb2312"?>
   <bookstore>
    <book ISBN="2-3631-4">
     <title>Oberon's Legacy</title>
     <author>Corets, Eva</author>
     <price>5.95</price>
    </book>
    <book>
    </book>
   </bookstore> 


  4、显示所有数据。

   XmlNode xn=xmlDoc.SelectSingleNode

("bookstore"); 
   XmlNodeList xnl=xn.ChildNodes;
   
   foreach(XmlNode xnf in xnl)
   {
       XmlElement xe=(XmlElement)xnf;
       Console.WriteLine(xe.GetAttribute

("genre"));//显示属性值
       Console.WriteLine(xe.GetAttribute("ISBN"));
 
       XmlNodeList xnf1=xe.ChildNodes;
       foreach(XmlNode xn2 in xnf1)
       {
           Console.WriteLine(xn2.InnerText);//显示

子节点点文本
       }
   }  

        以上的就是ASP.NET和C#对xml的基本使用方法。

下面说一下xml和xsl,从抽象的来说,我个人觉得xml象

是数据库,而xsl就象是过滤的。xsl中可以加入html等语

法,也可以加入xml的语法等,可以列出需要的数据等。
现有一个xml文件,内容如下
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<NewDataSet>
 <Table id="1">
    <ProductID>1001</ProductID>
    <CategoryID>1</CategoryID>
 </Table>
 <Table id="2">
    <ProductID>1002</ProductID>
    <CategoryID>2</CategoryID>
 </Table>
</NewDataSet>

其中第2句是引用xsl,xsl内容如下
<?xml version="1.0"?>
<xsl:stylesheet 

xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
     <body>
       <center>
       <h2>the notepad</h2>
       <table border="1">
           <tr>
                <td>id</td>
                <td>name</td>
           </tr>
      <xsl:for-each select="NewDataSet/Table">
           <tr>
                <td><xsl:value-of 

select="ProductID"/></td>
                <td><xsl:value-of 

select="ProductName"/></td>
            </tr>
      </xsl:for-each>
       </table>
       </center>
     </body>
    </html>
 </xsl:template>
</xsl:stylesheet>

中间的for-each就是循环遍历节点,获取指定的select后

的内容,下面的value-of就相当于sql中字段名。在使用

xsl的时候,还可以查询某一个值,这样xsl就需要如下

<xsl:value-of select="/students/student

[@id='2']/ProductID"/>
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet 

xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<center> <h1>id号是"2"的厂家的产品ID是:

<xsl:value-of select="/NewDataSet/Table

[@id='2']/ProductID"/></h1></center>
 </xsl:template>
</xsl:stylesheet>

有些如果在Table下还有节点,要显示这个节点下的东西

xsl就该如下
<?xml version="1.0"?>
<xsl:stylesheet 

xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
    <html>
     <body>
       <center>
       <h2>the notepad</h2>
       <table border="1">
           <tr>
                <td>随便写</td>
           </tr>
      <xsl:for-each 

select="NewDataSet/Table/xx/*">
           <tr>
                <td><xsl:value-of 

select="."/></td>
            </tr>
      </xsl:for-each>
       </table>
       </center>
     </body>
    </html>
</xsl:template>
</xsl:stylesheet>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值