C#中操作XML

XML命名空间:using System.Xml;

1.XML文件(bookstore.xml)

Code

2.往<bookstore>节点中插入一个<book>节点:

Code

结果:

Code

3.修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。

Code

结果:

Code

4.删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点

Code

结果:

Code

5.显示所有数据。

Code

            //string author=xnf["author"].InnerText; 
            XmlElement xe
=(XmlElement)xnf;
            Response.Write(xe.GetAttribute(
"genre"+ "<br/>");//显示属性值 
            Response.Write(xe.GetAttribute("ISBN"+ "<br/>");  
            XmlNodeList xnf1
=xe.ChildNodes;  
            
foreach(XmlNode xn2 in xnf1)   
            {
                Response.Write(xn2.InnerText 
+ "<br/>");//显示子节点点文本   
            }
        }

XML

Code

读出来的数据

Code

 

6.修改XML

Code

XML

Code

 

posted @ 2008-08-30 10:15  ike_li  阅读(220)  评论(0)    收藏  举报