xml Show_Update (2)

CODE:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{        
        
this.GetNodeValue("c:\\myxml.xml","date");
        
this.UpdateNodeValue("c:\\myxml.xml","date""2007-05-30");
    }


    
public void GetNodeValue(string xmlName,string noteName)
    
{
     
        
//方法1.用XmlTextReader
        
//XmlTextReader reader = new XmlTextReader(xmlName);
        
//while (reader.Read())
        
//{
        
//    if (reader.IsStartElement(noteName))
        
//    {
        
//        reader.Read();
        
//        this.Label1.Text = reader.Value;
        
//    }
        
//}

        
//方法2.用XmlDocument
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlName);
        XmlNode node 
= xmlDoc.SelectSingleNode("sys");  //根结点sys
        XmlNode nodeValue = node.SelectSingleNode(".//" + noteName);
        
if (nodeValue != null)
        
{
            
this.Label1.Text = nodeValue.InnerText;
        }

        
    }


    
public void UpdateNodeValue(string xmlName,string nodeName ,string nodeValue) 
    
{
        XmlDocument xmlDoc 
= new XmlDocument();
        xmlDoc.Load(xmlName);
        XmlNode node 
= xmlDoc.SelectSingleNode("sys");
        XmlNode childNode 
= xmlDoc.SelectSingleNode(".//" + nodeName);
        
if (childNode != null)
        
{
            childNode.InnerText 
= nodeValue;
            xmlDoc.Save(xmlName);
            
this.GetNodeValue("date");
        }

              
    }

}


XML:
<?xml version="1.0" encoding="gb2312"?>
<sys>
  
<set>
    
<ISPLAY intro="是否播放:Y否,N是">Y</ISPLAY>
    
<filePath intro="文件路径">c:\adFile\</filePath>
    
<date intro="日期">2007-05-30</date>
    
<version intro="版本号">01</version>
  
</set>
  
<t>TestValue</t>
  
<tt>
    
<ttt>
      
<tttt>43</tttt>
    
</ttt>
  
</tt>
</sys>
posted @ 2007-05-30 17:07  yongwnet  阅读(198)  评论(0编辑  收藏  举报