首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

奇怪异常:The expression passed to this method should result in a NodeSet

2011-12-30 
奇怪错误:The expression passed to this method should result in a NodeSet.在SelectSingleNode.的时候

奇怪错误: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);

热点排行