我有一個將對象序列化到SQL Server XML字段的方法。我在Managmenet Studio中的SQL服務器中看到XML,現在我需要在HTML頁面中顯示它。SQL中的XML字段
我使用AJAX函數調用到一個WebMethod,如下它調用一個存儲過程:
using (conn){
using (SqlCommand cmd = new SqlCommand()){
conn.Open();
cmd.CommandText = "GetMessage_Sel";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@messageId", SqlDbType.VarChar, 50).Value = str;
cmd.Connection = conn;
try{
rdr = cmd.ExecuteReader();
if (rdr.HasRows){
while (rdr.Read()){
returnValue1 = rdr.GetString(0);
returnValue2 = rdr.GetString(1);
}
}
else{
Console.WriteLine("No rows found.");
}
rdr.Close();
}
catch (Exception err){
// handle the error
//messageInsert = false;
}
finally{
conn.Close();
}
}
}
return new string[] {returnValue1,returnValue2};
的AJAX因此設置如下:
$('.view-details').click(function (e) {
e.preventDefault();
$('#ticket-id').text("Ticket id: " + $(this).closest('tr').children().eq(1).html());
$.ajax({
type: "POST",
url: "Default.aspx/PopulatePopUp",
cache: false,
data: "{'arg':'" + $(this).closest('tr').children().eq(1).html() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg)
{
$("#CompleteMessage").text(msg.d[0]);
$("#lblaka").text(msg.d[1]);
}
});
}
所以#lblaka顯示整個XML消息,但我需要將其分解爲更具可讀性的方式。所以eithe在我的WebMethod或Ajax的功能,我如何通過rdr.GetString(1)迭代,所以像這樣
foreach (var item in rdr.GetString(1)) {
string 1 = xml node value 1 ..... etc
}
編輯:
這裏是被存儲在XML的一個例子。
<Person xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AKA>
<string>Name 1</string>
<string>Name 2</string>
</AKA>
<Countries>
<string>USA</string>
<string>UK</string>
</Countries>
<Names>
<string>Name 1</string>
<string>Name 2</string>
</Names>
<Gender>Male</Gender>
</Person>
此方法的一個小問題。如果我點擊了Click按鈕,那麼同樣的信息會一次又一次地寫出來。我想這是.append方法 – CSharpNewBee
嗨,對不起。我已更新jsFiddle以顯示清除。選擇'顯示人物'可以在http://jsfiddle.net/MZ5Xs/2/ – hutchonoid
看到這個動作非常好。只需要確定哪種方法可以更快地使用/。謝謝你們 – CSharpNewBee