我有查詢來選擇數據:轉換JSON格式爲XML格式
Public Function GetStaffList(StaffCode As String) As IEnumerable
Dim query = (From c In Staff Where c.StaffID= StaffCode Select c)
Return query
End Function
使用下面的代碼返回的JSON我後:
Public Function GetPersonListJson(PersonCode As String) As String
Return JsonConvert.SerializeObject(GetStaffList(StaffCode))
End Function
JSON格式如下:
"[{\"$id\":\"1\",\"PersonID\":10001.0,\"PersonName\":\"Staff1\"}]"
如果我想以XML格式返回,我該怎麼辦?由於
更新:我嘗試使用下面的代碼返回XML
Public Function GetPersonListJson(PersonCode As String) As String
Dim json = JsonConvert.SerializeObject(GetStaffList(StaffCode))
Dim rootJson = "{""root"":" & json & "}"
Dim xml = JsonConvert.DeserializeXNode(rootJson)
Return xml.ToString()
End Function
XML的調試過程中的值是:
<root xmlns:json="http://james.newtonking.com/projects/json" json:id="1"><PersonID>10001</PersonID><PersonName>Staff1</PersonName> <EntityKey json:id="2"><EntitySetName>tblPerson</EntitySetName><EntityContainerName>PersonEntities</EntityContainerName><EntityKeyValues><Key>PersonID</Key><Type>System.Decimal</Type><Value>10001</Value></EntityKeyValues></EntityKey></root>
後加入的ToString(),返回結果如下:
"\u000d\u000a \u000d\u000a 10001<\/PersonID>\u000d\u000a Staff1<\/PersonName>\u000d\u000a \u000d\u000a
但這不是xm l格式。請再次幫忙。謝謝
你的意思是我的代碼應該是這樣嗎? JsonConvert.DeserializeXNode(JsonConvert.SerializeObject(GetStaffList(StaffCode)), 「工作人員」)? –
@HowardHee是的,我的意思是我的代碼中的'json'是'GetPersonListJson'方法的結果,這是您在評論中顯示的內容。 –
Dim json = JsonConvert.SerializeObject(GetStaffList(StaffCode)) Dim xml = JsonConvert.DeserializeXNode(json,「Staff」) 返回xml我寫下面的代碼,但得到錯誤消息:「System.Xml.Linq.XDocument can not被轉換爲字符串「 –