C# 读取XML文档

类别:.NET开发 点击:0 评论:0 推荐:

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

本文地址:http://com.8s8s.com/it/it46177.htm