简单读写xml

private void button1_Click(object sender, EventArgs e)
{
XDocument dow = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"), new XElement("person", new XElement("age",12345)));
dow.Save(Application.StartupPath + "\\my.xml");
MessageBox.Show("创建成功!");
}

private void button2_Click(object sender, EventArgs e)
{
string s = Application.StartupPath + "\\myy.xml";
XElement xe = XElement.Load(s);
IEnumerable<XElement> elements = from pinfo in xe.Elements("appset")
where pinfo.Attribute("Id").Value == "lisenerror"
select pinfo;
string conn = null ;
foreach(XElement elem in elements)
{
conn= elem.Element("strconn").Value;
}

SqlConnection conns = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("select * from tbBtoB", conns);
SqlDataAdapter adpt = new SqlDataAdapter();
adpt.SelectCommand = cmd;
DataSet ds = new DataSet();

conns.Open();
MessageBox.Show("open successs");
adpt.Fill(ds, "2");
MessageBox.Show(ds.Tables["2"].Rows.Count.ToString());
conns.Close();

}

 

 

-----------------以下是上面那个读取xml的内容---------------------

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Settings>
<Appset Id="lisenError">
<strconn>server=qiaopc;database=JLDu;user id=sa;password=234561</strconn>
</Appset>
</Settings>

posted on 2012-10-23 13:04  潴潴黠  阅读(160)  评论(0编辑  收藏  举报