【问题标题】:How can I generate XML from the JSON Data using c#?如何使用 c# 从 JSON 数据生成 XML?
【发布时间】:2010-09-22 13:31:30
【问题描述】:

我有如下 JSON 数据:

{
    "#data": [
        {
            "nid": "814",
            "type": "resource",
            "language": "",
            "uid": "125",
            "status": "1",
            "title": "title 1",
            "description": "description 1",
            "rating": "5",
            "picture": "" 
        },
        {
            "nid": "814",
            "type": "resource",
            "language": "",
            "uid": "125",
            "status": "1",
            "title": "title 2",
            "description": "description 2",
            "rating": "3.5",
            "picture": "" 
        }
    ]
}

我需要像下面这样构建 XML:

<node title="title 1" type="resource" rating="5">
        <description>description 1</description>
</node>
<node title="title 2" type="resource" rating="3.5">
 <description>description 2</description>
</node>

我不想反序列化 JSON 数据,但我只需要获取少量元素并构建 XML 对象。谁能帮我解决这个问题?

【问题讨论】:

  • 如果您不想反序列化 Json 数据,则需要对其进行解析。你确定值得付出努力吗?
  • 嗨 Paolo,如果我反序列化 json 数据,我会得到所有我不想要的数据元素。我只想获取几个元素并排除其余部分。在上面的例子中,我只使用了很少的元素,但实际上它得到了更多的数据。请建议我如何做到这一点。谢谢。

标签: c# xml json


【解决方案1】:

将其反序列化为 c# 对象,然后 xml 对其进行序列化。最简单的方法

【讨论】:

    【解决方案2】:

    你需要反序列化它,除非你想做一些难看的字符串解析。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      相关资源
      最近更新 更多