用DateSet读如xml文件

    技术2022-05-11  67

    用于连接数据库的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();      }   }} 


    最新回复(0)