日期:2010-07-03  浏览次数:20399 次

  这里介绍一种读取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
   {
   }
  }