用DateSet读如xml文件

用于连接数据库的xml文件(conn.xml)

<?xml version="1.0" encoding="utf-8" ?>
<conn>
 <hostname>ZJG-C14616DC51A</hostname>
 <catalog>government</catalog>
 <user>sa</user>
 <password>123</password>
</conn>

static void Main(string[] args)
  {
   DataSet ds=new DataSet();

   FileStream fs=new FileStream("conn.xml",FileMode.Open,FileAccess.Read);
   StreamReader reader=new StreamReader(fs);
   ds.ReadXml(reader);
   fs.Close();

   DataTable dt=ds.Tables[0];
   
   Console.WriteLine(dt.Rows[0]["hostname"]);
   Console.WriteLine(dt.Rows[0]["catalog"]);
   Console.WriteLine(dt.Rows[0]["user"]);
   Console.WriteLine(dt.Rows[0]["password"]);
   Console.Read();
  }

 

 

 

 

 // Read the XML document back in.
   // Create new FileStream to read schema with.
   System.IO.FileStream fsReadXml = new System.IO.FileStream
      (xmlFilename, System.IO.FileMode.Open);
   // Create an XmlTextReader to read the file.
   System.Xml.XmlTextReader myXmlReader =
      new System.Xml.XmlTextReader(fsReadXml);
   // Read the XML document into the DataSet.
   newDataSet.ReadXml(myXmlReader);
   // Close the XmlTextReader
   myXmlReader.Close();

   // Print out values of each table in the DataSet using the
   // function defined below.
   PrintValues(newDataSet,"New DataSet");
}

private void PrintValues(DataSet ds, string label){
   Console.WriteLine("/n" + label);
   foreach(DataTable t in ds.Tables){
      Console.WriteLine("TableName: " + t.TableName);
      foreach(DataRow r in t.Rows){
         foreach(DataColumn c in t.Columns){
            Console.Write("/t " + r[c] );
         }
         Console.WriteLine();
      }
   }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值