任何人都可以帮我使用C#将XML数据(我从远程 REST服务)插入我的SQL Server数据库吗?
提前致谢。
代码
HttpWebRequest myHttpWebRequest = WebRequest.Create("http://api.asicentral.com/v1/news.xml") as HttpWebRequest;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader loResponseStream =
new StreamReader(myHttpWebResponse.GetResponseStream(), enc);
string Response = loResponseStream.ReadToEnd();
myHttpWebResponse.Close();
答案 0 :(得分:1)
开始学习ADO.Net http://www.csharp-station.com/Tutorial/AdoDotNet/lesson01
或尝试此实体框架代码优先方法http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx
好的以上内容有点生硬,为什么要将Web服务响应存储到数据库中呢?
要做任何有意义的事情,你可能想要解析xml。你有很多选择。一对情绪浮现在脑海中;将xml反序列化为强类型对象,或者使用linq创建匿名类型。最后,您可以使用ADO.Net或Entity Framework将对象存储到数据库中。
答案 1 :(得分:1)
为了将此XML插入SQL Server,您需要在SQL Server中创建一个包含XML
类型列的表。
然后你可以使用非常简单的东西:
string connectionString = "......"; // define your connection string here
string query = "INSERT INTO dbo.YourTableNameHere(XmlColumn) VALUES(@XmlContent)";
// set up SqlConnection and SqlCommand
using(SqlConnection conn = new SqlConnection(connectionString))
using(SqlCommand cmd = new SqlCommand(query, conn))
{
// define parameter for query and set its value to your XML response
cmd.Parameters.Add("@XmlContent", SqlDbType.VarChar, -1);
cmd.Parameters["@XmlContent"].Value = Response; // assign your XML response here
// open connection, execute INSERT, close connection
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
当然,如果你使用像Entity Framework或其他一些ORM这样的东西,那么事情看起来会有很多不同......这只是“直接”纯ADO.NET
答案 2 :(得分:1)
[WebMethod]
public void XMLPersing()
{
var XMLDATA = "";
WriteLogCLS objWriteLog = new WriteLogCLS();
Stream receiveStream = HttpContext.Current.Request.InputStream;
receiveStream.Position = 0;
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); //For xml persing..
XMLDATA = readStream.ReadToEnd();
readStream.Close();
objWriteLog.WriteLog(Convert.ToString(XMLDATA));
XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("Log/Exception/Sample.xml"));
DataSet ds = new DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
if (ds.Tables.Count != 0)
{
var strCon = string.Empty;
strCon = ConfigurationManager.AppSettings["constring"];
SqlCommand cmdInsertXMLData = new SqlCommand();
SqlConnection SqlConn;
SqlConn = new SqlConnection(strCon);
try
{
cmdInsertXMLData = new SqlCommand("usp_InsertXML", SqlConn);
cmdInsertXMLData.CommandType = CommandType.StoredProcedure;
// cmdInsertLoginDetails.Parameters.Add("@XMLdata", SqlDbType.Xml).Value = ds.GetXml();
cmdInsertXMLData.Parameters.AddWithValue("@XMLdata", SqlDbType.Xml);
if (SqlConn.State == ConnectionState.Closed)
{
SqlConn.Open();
}
cmdInsertXMLData.ExecuteNonQuery();
// response = cmdInsertLoginDetails.Parameters["@Message"].Value.ToString();
}
catch (Exception ex)
{
objWriteLog.WriteLog("Error on XML Persing : " + ex.Message);
// response = "Error";
}
finally
{
if (cmdInsertXMLData != null)
{
cmdInsertXMLData.Dispose();
}
if (SqlConn.State == ConnectionState.Open)
{
SqlConn.Close();
SqlConn.Dispose();
}
objWriteLog = null;
}
// return response ;
}
}
}