I have the query to select data:
我有查询来选择数据:
Public Function GetStaffList(StaffCode As String) As IEnumerable
Dim query = (From c In Staff Where c.StaffID= StaffCode Select c)
Return query
End Function
After that I using code below to return Json:
之后,我使用下面的代码返回Json:
Public Function GetPersonListJson(PersonCode As String) As String
Return JsonConvert.SerializeObject(GetStaffList(StaffCode))
End Function
The Json format as below:
Json格式如下:
"[{\"$id\":\"1\",\"PersonID\":10001.0,\"PersonName\":\"Staff1\"}]"
If I want to return as XML format, how do I do? Thanks
如果我想以XML格式返回,怎么办?谢谢
UPDATE: I try using the following code to return 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
The value of xml during debug is:
在调试期间,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>
After added .ToString(), the return result as following:
添加.ToString()后,返回结果如下:
"\u000d\u000a \u000d\u000a 10001<\/PersonID>\u000d\u000a Staff1<\/PersonName>\u000d\u000a \u000d\u000a
But this is not xml format. Please help again. Thanks
但这不是xml格式。请帮助再次。谢谢
2 个解决方案
#1
3
You can use the DeserializeXNode
method. Based on your JSON you'll likely need to specify a root element name for the XML element. Below I've used "Staff" as the root name.
您可以使用DeserializeXNode方法。基于JSON,您可能需要为XML元素指定根元素名称。下面我使用“Staff”作为根名称。
Dim xml = JsonConvert.DeserializeXNode(json, "Staff")
The above returns an XDocument
. To return the XML as a string, add .ToString()
at the end of it. Based on your comments it sounds like you're trying to add this code to your existing GetPersonListJson
method. So you could add this line after the one above:
上面返回的是XDocument。要将XML作为字符串返回,在它的末尾添加. tostring()。根据您的评论,听起来好像您正在尝试将该代码添加到现有的GetPersonListJson方法中。所以你可以把这条线加到上面的这条线上:
Return xml.ToString()
That said, your method name no longer matches what you're actually doing. It's named GetPersonListJson
but now you're returning XML as a string, not JSON. I would suggest renaming it for clarity.
也就是说,你的方法名不再匹配你实际正在做的事情。它被命名为GetPersonListJson,但现在你将XML作为一个字符串返回,而不是JSON。我建议把它重新命名为清晰。
UPDATE: your sample string is a json array, which is why the above gives you the XmlNodeConverter can only convert JSON that begins with an object
error. To fix this you'll need to add a root element to your JSON manually:
更新:您的示例字符串是一个json数组,这就是为什么上面给出的XmlNodeConverter只能转换以对象错误开始的json。要解决这个问题,您需要手动向JSON添加根元素:
Public Function GetPersonListJson(PersonCode As String) As String
Dim json = JsonConvert.SerializeObject(GetStaffList(StaffCode))
' this step adds a root to the json (you can rename "root" to anything)'
Dim rootJson = "{""root"":" & json & "}"
Dim xml = JsonConvert.DeserializeXNode(rootJson)
Return xml.ToString()
End Function
#2
0
I found this to work:
我发现这是可行的:
string xml = "";
string json = @"{
'?xml': {
'@version': '1.0',
'@standalone': 'no'
},
'root': {
'object': " + JsonConvert.SerializeObject(object, Formatting.None)
+ "}}";
var xd = JsonConvert.DeserializeXmlNode(json);
using (var sw = new StringWriter()) {
using (var xw = System.Xml.XmlWriter.Create(sw)) {
xd.WriteTo(xw);
xw.Flush();
xml = sw.GetStringBuilder().ToString();
}
}
Credit to this page: http://james.newtonking.com/json/help/html/ConvertingJSONandXML.htm
此页面的荣誉:http://james.newtonking.com/json/help/html/ConvertingJSONandXML.htm。
#1
3
You can use the DeserializeXNode
method. Based on your JSON you'll likely need to specify a root element name for the XML element. Below I've used "Staff" as the root name.
您可以使用DeserializeXNode方法。基于JSON,您可能需要为XML元素指定根元素名称。下面我使用“Staff”作为根名称。
Dim xml = JsonConvert.DeserializeXNode(json, "Staff")
The above returns an XDocument
. To return the XML as a string, add .ToString()
at the end of it. Based on your comments it sounds like you're trying to add this code to your existing GetPersonListJson
method. So you could add this line after the one above:
上面返回的是XDocument。要将XML作为字符串返回,在它的末尾添加. tostring()。根据您的评论,听起来好像您正在尝试将该代码添加到现有的GetPersonListJson方法中。所以你可以把这条线加到上面的这条线上:
Return xml.ToString()
That said, your method name no longer matches what you're actually doing. It's named GetPersonListJson
but now you're returning XML as a string, not JSON. I would suggest renaming it for clarity.
也就是说,你的方法名不再匹配你实际正在做的事情。它被命名为GetPersonListJson,但现在你将XML作为一个字符串返回,而不是JSON。我建议把它重新命名为清晰。
UPDATE: your sample string is a json array, which is why the above gives you the XmlNodeConverter can only convert JSON that begins with an object
error. To fix this you'll need to add a root element to your JSON manually:
更新:您的示例字符串是一个json数组,这就是为什么上面给出的XmlNodeConverter只能转换以对象错误开始的json。要解决这个问题,您需要手动向JSON添加根元素:
Public Function GetPersonListJson(PersonCode As String) As String
Dim json = JsonConvert.SerializeObject(GetStaffList(StaffCode))
' this step adds a root to the json (you can rename "root" to anything)'
Dim rootJson = "{""root"":" & json & "}"
Dim xml = JsonConvert.DeserializeXNode(rootJson)
Return xml.ToString()
End Function
#2
0
I found this to work:
我发现这是可行的:
string xml = "";
string json = @"{
'?xml': {
'@version': '1.0',
'@standalone': 'no'
},
'root': {
'object': " + JsonConvert.SerializeObject(object, Formatting.None)
+ "}}";
var xd = JsonConvert.DeserializeXmlNode(json);
using (var sw = new StringWriter()) {
using (var xw = System.Xml.XmlWriter.Create(sw)) {
xd.WriteTo(xw);
xw.Flush();
xml = sw.GetStringBuilder().ToString();
}
}
Credit to this page: http://james.newtonking.com/json/help/html/ConvertingJSONandXML.htm
此页面的荣誉:http://james.newtonking.com/json/help/html/ConvertingJSONandXML.htm。