make sure the InnerText is not empty:foreach(XmlNode xNode in xList)
{
try
{
d=XmlConvert.ToInt32(xNode.InnerText);
if(i<d)
i=d;
}
catch (Exception ex)
{
Console.WriteLine("****{0}****",xNode.InnerText);
}
}
{
try
{
d=XmlConvert.ToInt32(xNode.InnerText);
if(i<d)
i=d;
}
catch (Exception ex)
{
Console.WriteLine("****{0}****",xNode.InnerText);
}
}
我也搞了一段时间,后来发现是楼主xml文件里的中文问题,
但是以前碰到的有中文也没有问题的呀,很奇怪,可能你的文件很特殊,
现在就这样解决吧:
using System;
using System.IO;
using System.Xml;namespace ZZ
{
class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
int i=0;
int d=0;
XmlDocument xdoc= new XmlDocument();
StreamReader sr = new StreamReader(@"d:\test.xml",Encoding.Default);
xdoc.Load(sr);
sr.Close();
XmlNodeList xList = xdoc.SelectNodes("//Page");
foreach(XmlNode xNode in xList)
{
d = XmlConvert.ToInt32(xNode.InnerText);
if(i<d)
i=d;
}
Console.WriteLine(i.ToString());
Console.ReadLine();
} }
}
输出620,
正确,
有问题发短消息给我。
或
给XmlConvert.ToInt32(xNode.InnerText);改为XmlConvert.ToInt16(xNode.InnerText);这两种方法一定该能实现的