如何使用c#获取sql xml保存的文件?

时间:2014-02-28 01:56:20

标签: c# sql xml

我有这个存储过程,它工作正常

alter PROC DBXMLSTRUCT 
AS
BEGIN
DECLARE @xmlStr XML;
set @xmlStr =
(select '[Stok]' [@DataBaseName],
(select t.table_name [@TableName],
(select c.COLUMN_NAME [@ColumnName] 
from INFORMATION_SCHEMA.COLUMNS c
where c.TABLE_NAME = t.TABLE_NAME
for xml path('Column'), type)
from INFORMATION_SCHEMA.TABLES t
for xml path('Table'), type)
for xml path('DataBase'),type)
SELECT @xmlStr = BulkColumn FROM OPENROWSET(BULK 'D:\XMLDBStruct.xml', SINGLE_CLOB) as XMLDBStruct(BulkColumn)

SELECT @xmlStr
END

如何使用c#
获取此保存的xml文件 但通过特殊的方式,它返回一个像sql server

中相同的一个英俊的xml文档

2 个答案:

答案 0 :(得分:2)

只需将结果写入文件即可。您已经返回格式化的xml

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xml);
xdoc.Save("myfilename.xml");

答案 1 :(得分:0)

 using (SqlConnection conn = new SqlConnection(con))
    {
        XDocument results = new XDocument(
       new XElement("results"));

        using (SqlCommand cmd = conn.CreateCommand())
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "MyStoredProc";
            conn.Open();
            var count = 0;
            using (XmlReader reader = cmd.ExecuteXmlReader())
            {

                while (reader.Read())
                {

                    results.Root.Add(XElement.Parse(reader.ReadOuterXml()));
                    count += 1;

                }
            }

            return results.ToString();

        }
    }