else语句不执行

娜娜1226 2010-08-17 01:43:04
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(new System.IO.StringReader(clickCount));
XmlNode node = xmlDoc.SelectSingleNode("/data");
XmlNodeList nodeList = xmlDoc.SelectNodes("/data/webmethod");
for (int i = 0; i < nodeList.Count; i++)
{
if (webmethod.Trim() == nodeList[i].Attributes["methodname"].Value.ToString().Trim())
{
nodeList[i].Attributes["clickcount"].Value = (int.Parse(nodeList[i].Attributes["clickcount"].Value.ToString()) + 1).ToString();
}
if (webmethod.Trim() != nodeList[i].Attributes["methodname"].Value.ToString().Trim())
{
nodeList[i].Attributes["clickcount"].Value = nodeList[i].Attributes["clickcount"].Value.ToString();
}
else
{
XmlElement xesub = xmlDoc.CreateElement("webmethod");
xesub.SetAttribute("methodname", webmethod);
xesub.SetAttribute("clickcount", "1");
node.AppendChild(xesub);
}
}
xmlDoc.Save("data.config");
FileStream fs = new FileStream("data.config", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs);
string outerXml = reader.ReadToEnd();
reader.Close();
fs.Close();
return outerXml;

else里面的语句怎么不执行?我不能把else里面的东西放到else if里面,因为那样,会添加重复的节点,而不是直接加1。有没有好的解决方法!各位高手,帮忙了!
...全文
295 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
枫林001 2010-08-17
  • 打赏
  • 举报
回复
不要看我的answer,只是为了1分
andreacc 2010-08-17
  • 打赏
  • 举报
回复
晕倒 逻辑问题啦。
cloudapex 2010-08-17
  • 打赏
  • 举报
回复
for (int i = 0; i < nodeList.Count; i++)
{
if (webmethod.Trim() == nodeList[i].Attributes["methodname"].Value.ToString().Trim())
{
nodeList[i].Attributes["clickcount"].Value = (int.Parse(nodeList[i].Attributes["clickcount"].Value.ToString()) + 1).ToString();
XmlElement xesub = xmlDoc.CreateElement("webmethod");
xesub.SetAttribute("methodname", webmethod);
xesub.SetAttribute("clickcount", "1");
node.AppendChild(xesub);
}
else
{
nodeList[i].Attributes["clickcount"].Value = nodeList[i].Attributes["clickcount"].Value.ToString();
}
}
佳岳 2010-08-17
  • 打赏
  • 举报
回复
第一个IF 过了 就不会执行第二个
如果到了第二个IF 通过 那么 也不执行ELSE
古都老码农 2010-08-17
  • 打赏
  • 举报
回复
在第一个IF下面加个空ELSE试试
chinawes 2010-08-17
  • 打赏
  • 举报
回复
楼主 我认为你处理的逻辑有问题,你再好好考虑下,怎么说处理逻辑也不能违背语言的语法啊。
重新缕一缕。
happy184 2010-08-17
  • 打赏
  • 举报
回复
第一个if和else不是一个道理?
happy184 2010-08-17
  • 打赏
  • 举报
回复
我感觉楼主这段代码的逻辑有问题。
journey_q 2010-08-17
  • 打赏
  • 举报
回复
如果你不用
if
else if
else

就用

if
if
else的话,else不执行,那么第二个if应该执行了吧...
调试看进入第二个if了没有,如果进入,else肯定不执行了,
那就是判断条件的问题了,
个人理解
无涯大者 2010-08-17
  • 打赏
  • 举报
回复
if (webmethod.Trim() == nodeList[i].Attributes["methodname"].Value.ToString().Trim())
{
nodeList[i].Attributes["clickcount"].Value = (int.Parse(nodeList[i].Attributes["clickcount"].Value.ToString()) + 1).ToString();
XmlElement xesub = xmlDoc.CreateElement("webmethod");
xesub.SetAttribute("methodname", webmethod);
xesub.SetAttribute("clickcount", "1");
node.AppendChild(xesub);
}
else
{
nodeList[i].Attributes["clickcount"].Value = nodeList[i].Attributes["clickcount"].Value.ToString();
}

看看6楼的这个程序,应该可以的。。。
Aquarius娜吖 2010-08-17
  • 打赏
  • 举报
回复
把if(...改成!=),再将if else里面的内容互换一下试试!
娜娜1226 2010-08-17
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 chinawes 的回复:]
但是你那样写的话,Else分支是无法走到得。
[/Quote]嗯嗯。所以,我不知道怎么办啊!我如果像他们说的那样,把else去掉,把else里面的内容放到else if中的话,会是这样的结果:
<data>
<webmethod methodname="ExistConfig" clickcount="5">
</webmethod>
<webmethod methodname="GetConfigBySessionKey" clickcount="2" />
<webmethod methodname="ExistConfig" clickcount="1" />
</data>

重复了 <webmethod methodname="ExistConfig" clickcount="1" />这一段!而不是在前面直接加1
leejelen 2010-08-17
  • 打赏
  • 举报
回复
第二个if有执行的话。那else怎么执行呢?看看你的判断条件!
娜娜1226 2010-08-17
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 pig510520 的回复:]
你这不是 没事 找事 么
[/Quote]我晕。什么叫没事找事儿啊!问题还没解决好吧!不知道别乱说!
chinawes 2010-08-17
  • 打赏
  • 举报
回复
但是你那样写的话,Else分支是无法走到得。
pig510520 2010-08-17
  • 打赏
  • 举报
回复
你这不是 没事 找事 么
leejelen 2010-08-17
  • 打赏
  • 举报
回复
你上面不是有三个么
if
if
else
哪个有运行之后跳出去?
娜娜1226 2010-08-17
  • 打赏
  • 举报
回复
我的代码本来就是else if。只是改成if if试了一下,后来复制到上面就那样了。
pig510520 2010-08-17
  • 打赏
  • 举报
回复
你试过 把第二个 if 改成 else if 没有
娜娜1226 2010-08-17
  • 打赏
  • 举报
回复
16楼 你说的这个我知道!我上面说得很清楚了!那样的话,会添加重复的节点,而不是直接加1
加载更多回复(16)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧