1 示例xml文件 model.XML
<?XML version="1.0" encoding="utf-8" ?>
<DrRoot>
<SiteName>XML操作示例</SiteName>
<SiteUrl>www.abc.com.cn</SiteUrl>
<SiteDescript>.Net開源論壇</SiteDescript>
<SiteKeyWord>XML操作示例</SiteKeyWord>
<FileType>gif|jpg|rar</FileType>
<FileSize>1024</FileSize>
<Filters>script|iframe</Filters>
<FilterEvent>onload|onmouSEOver</FilterEvent>
<BadWords>admin|webmaster|drbbs</BadWords>
<DisallowIP>0.0.0.0</DisallowIP>
<IsCloseReg>False</IsCloseReg>
<IsCloseBBS>False</IsCloseBBS>
<PostPay>5</PostPay>
<ReplyPay>2</ReplyPay>
<GoodPay>10</GoodPay>
<DelPost>5</DelPost>
<DelReply>2</DelReply>
<CancleGood>10</CancleGood>
</DrRoot>
2 主要方法
public string ReadValue(string key)
{
XmlDocument xd=new XMLDocument();
xd.Load(Server.MapPath("model.XML"));
XMLNodeList xnl = xd.GetElementsByTagName(key);
if(xnl.Count==0)
return "";
else
{
XMLNode mNode=xnl[0];
return mNode.InnerText;
}
}
public void UpdateValue(string nKey,string nValue)
{
if(ReadValue(nKey)!="")
{
XmlDocument xmlDoc=new XMLDocument();
xmlDoc.Load(Server.MapPath("model.XML"));
XmlNodeList elemList = XMLDoc.GetElementsByTagName(nKey);
XMLNode mNode=elemList[0];
mNode.InnerText=nValue;
XmlTextWriter xw = new XmlTextWriter(new StreamWriter(Server.MapPath("model.XML")));
xw.Formatting = Formatting.Indented;
XMLDoc.WriteTo(xw);
xw.Close();
}
}