如何将输出数据保存到XML文件Microsoft Visual Studio 2010

时间:2023-01-06 11:35:11
    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;
    }

Above is my code for my 3-tier Data Access Layer. I need to save the above code into an XML file. How do I do this?

以上是我的3层数据访问层的代码。我需要将上面的代码保存到XML文件中。我该怎么做呢?

2 个解决方案

#1


0  

<?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>

Presto.. C# in XML format.

Presto .. C#的XML格式。

#2


0  

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;
}

#1


0  

<?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>

Presto.. C# in XML format.

Presto .. C#的XML格式。

#2


0  

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;
}