這裡介紹一種讀取XML文檔的方法,示例中用的是 XmlTextReader 函數,每執行 Read() 一次,讀取一行.
using System.Xml;
public KSBMCONFIG ReadConfig(string XmlConfigFile)
{
KSBMCONFIG _conf = new KSBMCONFIG ();
try
{
// Open an XML file
System.Xml.XmlTextReader reader;
reader = new System.Xml.XmlTextReader(XmlConfigFile);
while ( reader.Read() )
{
if((reader.NodeType == XmlNodeType.EndElement)
&&(reader.Name == "KSBM"))
{
break;
}
if(reader.IsStartElement ("ServerPath"))
{
reader.Read ();
_conf.ServerPath = reader.Value ;
}
else if(reader.IsStartElement ("SmtpServer"))
{
reader.Read ();
_conf.SMTPServer = reader.Value ;
}
else if(reader.IsStartElement ("ConnectString"))
{
reader.Read ();
_conf.ConnectString = reader.Value ;
}
}
return _conf;
}
catch
{
_conf.ConnectString = "";
_conf.ServerPath = "";
_conf.SMTPServer = "";
return _conf;
}
finally
{
}
}