日期:2014-05-20  浏览次数:20918 次

奇怪错误:The expression passed to this method should result in a NodeSet.
在SelectSingleNode.的时候出错。请高手指点!!


string   file   =   System.Web.HttpContext.Current.Server.MapPath( "VistorsFrom.xml ");

System.Xml.XmlDocument   doc   =   new   System.Xml.XmlDocument();
doc.Load(file);

System.Xml.XmlNode   node   =   doc.SelectSingleNode( "Sources/Source/id= ' "   +   id   +   " ' ");
int   count   =   int.Parse(node.InnerText)   +   1;
node.InnerText   =   count.ToString();

doc.Save(file);


我的XML文件:
<?xml   version= "1.0 "   encoding= "utf-8 "   ?>  
<Sources>
<Source>
<id> 3 </id>
<name> Baidu </name>
<count> 0 </count>
</Source>
</Sources>

------解决方案--------------------
想将ID自增1??

try..

XmlDocument doc = new XmlDocument();
doc.Load(@ "../../test.xml ");
string id = "3 ";
XmlNode node = doc.SelectSingleNode( "Sources/Source/id[text()= ' " + id + " '] ");
node.InnerText = Convert.ToString (Convert .ToInt32 (node .InnerText ) + 1);
doc.Save(@ "../../test.xml ");
------解决方案--------------------
string strpath = "Sources/Source/id[ ' "+id+ " '] " ;
System.Xml.XmlNode node = doc.SelectSingleNode(strpath);
int count = int.Parse(node.InnerText) + 1;
node.InnerText = count.ToString();
doc.Save(file);