如何将输出数据保存到 XML 文件中 Microsoft Visual Studio 2010
本文关键字:文件 Microsoft Visual 2010 Studio XML 输出 数据 保存 | 更新日期: 2023-09-27 17:56:38
public AdvertisementDAL selectAdvertisementLocation1(DateTime now, string gender)
{
AdvertisementDAL dal = null;
string sql = "Select * From Advertisement Where @currentDate between StartDate AND EndDate AND TargetAudience = @gender AND Location = 1";
SqlConnection conn = new SqlConnection(_connStr);
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@currentDate", now);
cmd.Parameters.AddWithValue("@gender", gender);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
_advertisementID = int.Parse(dr["AdvertisementID"].ToString());
_location = int.Parse(dr["Location"].ToString());
_image = dr["Image"].ToString();
_targetAudience = dr["TargetAudience"].ToString();
_url = dr["Url"].ToString();
_onMouseOverText = dr["OnMouseOverText"].ToString();
dal = new AdvertisementDAL(_advertisementID, _location, _image, _targetAudience,
_url, _onMouseOverText);
}
conn.Close();
dr.Close();
dr.Dispose();
return dal;
}
以上是我的 3 层Data Access Layer
的代码。 我需要将上面的代码保存到XML
文件中。 我该怎么做?
<?xml version="1.0" encoding="ISO-8859-1"?>
<code>
<![CDATA[
public AdvertisementDAL selectAdvertisementLocation1(DateTime now, string gender)
{
AdvertisementDAL dal = null;
string sql = "Select * From Advertisement Where @currentDate between StartDate AND EndDate AND TargetAudience = @gender AND Location = 1";
SqlConnection conn = new SqlConnection(_connStr);
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@currentDate", now);
cmd.Parameters.AddWithValue("@gender", gender);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
_advertisementID = int.Parse(dr["AdvertisementID"].ToString());
_location = int.Parse(dr["Location"].ToString());
_image = dr["Image"].ToString();
_targetAudience = dr["TargetAudience"].ToString();
_url = dr["Url"].ToString();
_onMouseOverText = dr["OnMouseOverText"].ToString();
dal = new AdvertisementDAL(_advertisementID, _location, _image, _targetAudience,
_url, _onMouseOverText);
}
conn.Close();
dr.Close();
dr.Dispose();
return dal;
}
]]>
</code>
普雷斯托..XML 格式的 C#。
public AdvertisementDAL selectAdvertisementLocation1(DateTime now, string gender)
{
AdvertisementDAL dal = null;
string sql = "Select * From Advertisement Where @currentDate between StartDate AND EndDate AND TargetAudience = @gender AND Location = 1";
SqlConnection conn = new SqlConnection(_connStr);
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@currentDate", now);
cmd.Parameters.AddWithValue("@gender", gender);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
_advertisementID = int.Parse(dr["AdvertisementID"].ToString());
_location = int.Parse(dr["Location"].ToString());
_image = dr["Image"].ToString();
_targetAudience = dr["TargetAudience"].ToString();
_url = dr["Url"].ToString();
_onMouseOverText = dr["OnMouseOverText"].ToString();
dal = new AdvertisementDAL(_advertisementID, _location, _image, _targetAudience,
_url, _onMouseOverText);
System.IO.StreamWriter file = new System.IO.StreamWriter(
@"c:'temp'SerializationOverview.xml"); //rmb to create the file
writer.Serialize(file, dal);
file.Close();
}
conn.Close();
dr.Close();
dr.Dispose();
return dal;
}