简体   繁体   English

使用SqlDataReader读取XML数据

[英]Reading XML Data using SqlDataReader

I have a stored procedure that creates an XML document. 我有一个创建XML文档的存储过程。 I then have the following code: 然后,我有以下代码:

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[]);
            }
        }
    }
}

I need to add the xml data row by row into a list and then write that list to a file, how do I do this? 我需要将xml数据逐行添加到列表中,然后将该列表写入文件中,我该怎么做?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM