vb.net 将 Json 格式转换为 xml 格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18985867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:13:34  来源:igfitidea点击:

Convert Json format to xml format

xmljsonvb.netjson.net

提问by Howard Hee

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 格式。请再次帮助。谢谢

回答by Ahmad Mageed

You can use the DeserializeXNodemethod. 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 GetPersonListJsonmethod. 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 GetPersonListJsonbut 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 objecterror. To fix this you'll need to add a root element to your JSON manually:

更新:您的示例字符串是一个 json 数组,这就是上面给出XmlNodeConverter can only convert JSON that begins with an object错误的原因。要解决此问题,您需要手动向 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

回答by sobelito

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