首页 > 开发 > XML > 正文

C#读取XML文档

2024-09-05 20:56:02
字体:
来源:转载
供稿:网友

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表