我有选择数据的查询:
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
但这不是 xml 格式。请再帮忙。谢谢