使用SqlDataReader读取XML数据

时间:2017-03-22 15:00:36

标签: c# sql-server xml ado.net

我有一个创建XML文档的存储过程。然后我有以下代码:

using (SqlConnection con = new SqlConnection(_connectionString))
{
    con.Open();

    using (SqlCommand cmd = new SqlCommand("GetModuleInstallerManifestXML", con))
    {
        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        cmd.Parameters.Add(new SqlParameter("@mod_name", SqlDbType.VarChar, 250)).Value = this.ModuleName;

        using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
        {
            while (reader.Read())
            {
                manifestXmlList.Add(reader[]);
            }
        }
    }
}

我需要逐行将xml数据添加到列表中,然后将该列表写入文件,我该怎么做?

1 个答案:

答案 0 :(得分:1)

检查此答案https://stackoverflow.com/a/5424250/5358389

string xmlString = string.Empty;
using (XmlReader reader = cmd.ExecuteXmlReader())
{            
     XDocument xml = XDocument.Load(reader);
     x.Save("filePath");
}