代码及代码说明如下:
需要在WindowsApplication1/bin/debug目录下面放个个Config.xml文件供程序读取 代码为:
<?xml version="1.0" encoding="GB2312"?>
<connectionstring>
<con store="测试店" ip="10.1.1.1" uid="rjynx" pwd="rjynx" database="metis_test"></con>
</connectionstring>
在工程WindowsApplication1下面新建的CS文件Config.cs 代码如下:
using System;
using System.Collections.Generic; 
using System.Text;
using System.Xml;
using System.Collections;namespace WindowsApplication1
{
    public  class Config
    {
        public Config()
        {
            this.OpenConfigFile();
            this.GetConnectionData();
        }        static public Config GetConfig()
        {
            return config_;
        }
        static public Config config_ = new Config();//我怀疑这里new了以后在Form1_Load调用出了问题  public ArrayList GetStore()
{
            XmlNodeList list = GetNodeByTagName("config");

    ArrayList store_list = new ArrayList();     int i;     try
       {
for(i = 0;i < list.Count;i++)
 {
  if(((XmlElement)this.GetNodeByTagName("config")[i]).HasAttribute("store"))
  store_list.Add(list.Item(i).Attributes["store"].Value);
 }
conNode_ =  list.Item(i);
       }
       catch(System.Exception e)
       {
System.Windows.Forms.MessageBox.Show(e.Message);
       }
return store_list;
}    
        public void OpenConfigFile()//打开并读取Config.xml文件的方法
        {
            try
            {
                System.IO.FileStream stream = new System.IO.FileStream(this.path_, System.IO.FileMode.Open, System.IO.FileAccess.Read);                byte[] b = new byte[stream.Length];                stream.Read(b, 0, b.Length);                string strXML = "";                strXML = System.Text.Encoding.Default.GetString(b, 0, b.Length);                System.Console.WriteLine("strXML = " + strXML);                doc_.LoadXml(strXML);
            }
            catch (System.Xml.XmlException e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
            }
            catch (System.Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
            }
        }           public void GetConnectionData()
          {         
            XmlNodeList list = GetNodeByTagName("config");
            //每当程序运行到这里就不在继续运行,导致后面Form1_Load中Class1.GetConfig().store_的store_为空值
            int i;
            try
            {
                for (i = 0; i < list.Count; i++)
                {
                    if (((XmlElement)this.GetNodeByTagName("config")[i]).HasAttribute("store"))
                    {
                        if (list.Item(i).Attributes["store"].Value == this.store_)//这里的赋值操作根本没执行
                        {
                            break;
                        }
                    }
                }
                conNode_ = list.Item(i);
            }
            catch (System.Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
            }
        }   
        private XmlNodeList GetNodeByTagName(string TagName)
        {          
            XmlNodeList nodelist = doc_.GetElementsByTagName(TagName);            XmlNodeList list = nodelist.Item(0).ChildNodes.Item(1).ChildNodes;            return list;//这里也没有返回节点列表,导致我后面程序中调用的list也无值,直接出错
        }        public string store_ = "";        private XmlDocument doc_ = new XmlDocument();        public string path_ = "Config.xml";        private XmlNode conNode_;        public string ver_ = "POS_ADMIN 2008.01.15";
    }
}
然后在Form1里面写一句调用的代码   
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Control;
using Model;
using Util;namespace WindowsApplication1
{
/// <summary>
/// frmLogin 的摘要说明。
/// </summary>
public class Form1: System.Windows.Forms.Form
{
                ServiceControl Service;
public frmLogin()
{
System.Console.WriteLine (Config.GetConfig().store_);
                        //由于前面CS中没有取到值所以GetConfig().store_的值为空;
InitializeComponent();
}
private void frmLogin_Load(object sender, System.EventArgs e)
{
Service = new ServiceControl();
ArrayList list = new ArrayList();
list = Config.GetConfig().GetStore();
                        //这里也是list列表也取不到值,也就是说GetConfig().GetStore();根本没有值可取
combox.Items.Clear();
for (int i = 0;i < list.Count;i++)
{
combox.Items.Add(list[i]);
}
combox.SelectedIndex = 0;
}
}
}错误信息:
未处理 System.TypeInitializationException
  Message="“WindowsApplication1.Class1”的类型初始值设定项引发异常。"------------异常
  Source="WindowsApplication1"
  TypeName="WindowsApplication1.Class1"
  StackTrace:
       在 WindowsApplication1.Class1.GetConfig()
       在 WindowsApplication1.Form1.Form1_Load(Object sender, EventArgs e) 位置 C:\Documents and Settings\liujie\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs:行号 21
       在 System.Windows.Forms.Form.OnLoad(EventArgs e)
       在 System.Windows.Forms.Form.OnCreateControl()
       在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       在 System.Windows.Forms.Control.CreateControl()
       在 System.Windows.Forms.Control.WmShowWindow(Message& m)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
       在 System.Windows.Forms.Form.WmShowWindow(Message& m)
       在 System.Windows.Forms.Form.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
       在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
       在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
       在 System.Windows.Forms.Control.set_Visible(Boolean value)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 WindowsApplication1.Program.Main() 位置 C:\Documents and Settings\liujie\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Program.cs:行号 17
       在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()