這裡介紹一種讀取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
{
}
}