修改XML文件里的节点的值
大家好我有个一操作不知道怎么去实现,帮忙看一下。
有一个A 页面里有一个超链接(B.aspx?Id=A1&target=http://localhost/a1.aspx)
一个XML文档是用来记录页面点击数的
<?xml version= "1.0 " standalone= "yes " ?>
<adResponses>
<ad adname= "a1 " hitCount= "0 " />
<ad adname= "a2 " hitCount= "0 " />
<ad adname= "a3 " hitCount= "0 " />
</adResponses>
再个就是B页面,我想在B页面里通过传个来的变量值对XML文档进行操作
如:若我的超链接是 B.aspx?Id=A1&taget=http://localhost/a1.aspx
我想对 <ad adname= "a1 " hitcount= "0 "> 里的hitcount 自加
在B页面里有:
string id=Request.QuerString( "Id ").ToString();
string target=Request.QuerString( "target ").ToString();
if (id== null || target == null)
{
target= "index.aspx ";
}
else
{
......//对XML文档里的属性值进行操作,大家能否帮我指导一下。万谢!
}
Response.Redirect(target);
------解决方案--------------------假如XML文档名为 add.xml
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( "add.xml ");
XmlNodeList nodelist=xmlDoc.SelectSingleNode( "adResponses ").ChildNodes;
foreach(XmlNOde xn in nodelist)
{
XmlElement xe=(XmlElement)xn;
if(xe.GetAttribute( "adname ") == id)
{
int i=Convert.ToInt32(xe.GetAttribute( "hitcount "))+1;
xe.SetAttribute( "hitcount ",i.ToStirng());
}
}
xmlDoc.Save( "add.xml ");
------解决方案--------------------string id=Request.QuerString( "Id ").ToString();
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath( "1.xml ")); // 加载文件
XmlNode node = doc.SelectSingleNode( "//ad[@adname= ' " + id.ToLower() + " '] ");
int iCount = Convert.ToInt32(node.Attributes[ "hitCount "].Value) + 1;
node.Attributes[ "hitCount "].Value = iCount;
doc.Save(Server.MapPath( "1.xml "));