asp.net从proc获取xml?

时间:2010-11-11 21:04:30

标签: asp.net xml

我有一个使用FOR XML的存储过程,想知道如何在我的asp.net c#代码中加载xml?

3 个答案:

答案 0 :(得分:0)

只需将其加载到xml文档对象中......

XmlDocument docXml = new XmlDocument(); docXml.LoadXml(stringReturnedFromStoredProc);

然后,您可以遍历节点或使用XPath执行xmldoc所需的操作。

答案 1 :(得分:0)

string sql = "sProcName";
string connStr = "connectionString";

using (SqlConnection connection = new SqlConnection(connStr))
{
    connection.Open();

    SqlDataAdapter adapter = new SqlDataAdapter();

    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.Connection = connection;
        cmd.CommandText = sql;
        cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter p = new SqlParameter();
        p.Direction = ParameterDirection.Output;
        p.ParameterName = "@resultXML";
        p.SqlDbType = SqlDbType.Xml;

        cmd.Parameters.Add(p);

        adapter.UpdateCommand = cmd;
        result = adapter.UpdateCommand.ExecuteNonQuery();
        string resultXML = (string)p.Value;

        XElement xml = XElement.Parse(resultXML);
    }
}

答案 2 :(得分:0)

我在asp.net论坛网站上发现了这个:

XmlDocument xdoc = new XmlDocument();
SqlConnection cnn = null;
SqlCommand cmd = null;
try
{
      cnn = new SqlConnection();
      cnn.ConnectionString = "xxxxxxxxxxxxxxxxx";
      cnn.Open();

      string selectQry = "SELECT [Xml] FROM [Table1] WHERE [PK_ID] = @ID";
      cmd = new SqlCommand(selectQry, cnn);
      cmd.Parameters.AddWithValue("@ID", ID);

      XmlReader reader = cmd.ExecuteXmlReader();

      if (reader.Read())
         xdoc.Load(reader);
}
catch (Exception ex)
{
      throw ex;
}
finally
{
      cmd.Dispose();
      cnn.Close();
}