将Json转换为带有名称空间的XML字符串



我有一个json字符串,像这样

"{
  "RSS": {
    "Channel": {
      "item": [
        {
          "title": "Overlay HD/CC",
          "guid": "1",
          "description": "This example shows tooltip overlays for captions and quality.",
          "jwplayer:image": "http://content.jwplatform.com/thumbs/3XnJSIm4-640.jpg",
          "jwplayer:source": [
            {
              "@file": "http://content.jwplatform.com/videos/3XnJSIm4-DZ7jSYgM.mp4",
              "@label": "720p"
            },
            {
              "@file": "http://content.jwplatform.com/videos/3XnJSIm4-kNspJqnJ.mp4",
              "@label": "360p"
            },
            {
              "@file": "http://content.jwplatform.com/videos/3XnJSIm4-injeKYZS.mp4",
              "@label": "180p"
            }
          ],
          "jwplayer:track": [
            {
              "@file": "http://content.jwplatform.com/captions/2UEDrDhv.txt",
              "@label": "English"
            },
            {
              "@file": "http://content.jwplatform.com/captions/6aaGiPcs.txt",
              "@label": "Japanese"
            },
            {
              "@file": "http://content.jwplatform.com/captions/2nxzdRca.txt",
              "@label": "Russian"
            },
            {
              "@file": "http://content.jwplatform.com/captions/BMjSl0KC.txt",
              "@label": "Spanish"
            }
          ]
        }
      ]
    },
    "@xmlns:jwplayer": "http://support.jwplayer.com/customer/portal/articles/1403635-media-format-reference#feeds",
    "@version": "2.0"
  }
}"

我尝试使用json.net将其转换为xmlDocument:

XmlDocument xmlDoc = JsonConvert.DeserializeXmlNode(json);
using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
    xmlDoc.WriteTo(xmlTextWriter); // Exception: Cannot use a prefix with an empty namespace.
    xmlTextWriter.Flush();
    return stringWriter.GetStringBuilder().ToString();
}

但是当我尝试访问OuterXml属性时,它抛出异常"不能使用空命名空间的前缀"。

是否有任何方式来修复该异常或另一种方法来转换json到xml字符串?XML字符串的名称空间如下

<jwplayer:image>http://content.jwplatform.com/thumbs/3XnJSIm4-640.jpg</jwplayer:image>

我不知道为什么你的代码抛出异常,因为它必须是一个有效的XML,因为XML成功加载到XmlDocument对象。

我能想到的另一种方式来获得格式化的XML字符串是使用加载未格式化的XML字符串从XmlDocument.OuterXmlXDocument,然后将其转换为格式化的XML字符串:

XmlDocument xmlDoc = JsonConvert.DeserializeXmlNode(json);
XDocument xDoc = XDocument.Parse(xmlDoc.OuterXml);
return xDoc.ToString();

不是一个有效的方法,但是这个方法简单有效。

将模式信息放在主元素后面,这里是RSS。之后,它可以与Newtosoft一起工作。

{
  "RSS": {
    "@xmlns:jwplayer": "http://support.jwplayer.com/customer/portal/articles/1403635-media-format-reference#feeds",
    "@version": "2.0",
    "Channel": {
      "item": [
        {
          "title": "Overlay HD/CC",
          "guid": "1",
          "description": "This example shows tooltip overlays for captions and quality.",
          "jwplayer:image": "http://content.jwplatform.com/thumbs/3XnJSIm4-640.jpg",
          "jwplayer:source": [
            {
              "@file": "http://content.jwplatform.com/videos/3XnJSIm4-DZ7jSYgM.mp4",
              "@label": "720p"
            },
            {
              "@file": "http://content.jwplatform.com/videos/3XnJSIm4-kNspJqnJ.mp4",
              "@label": "360p"
            },
            {
              "@file": "http://content.jwplatform.com/videos/3XnJSIm4-injeKYZS.mp4",
              "@label": "180p"
            }
          ],
          "jwplayer:track": [
            {
              "@file": "http://content.jwplatform.com/captions/2UEDrDhv.txt",
              "@label": "English"
            },
            {
              "@file": "http://content.jwplatform.com/captions/6aaGiPcs.txt",
              "@label": "Japanese"
            },
            {
              "@file": "http://content.jwplatform.com/captions/2nxzdRca.txt",
              "@label": "Russian"
            },
            {
              "@file": "http://content.jwplatform.com/captions/BMjSl0KC.txt",
              "@label": "Spanish"
            }
          ]
        }
      ]
    }
  }
}
And result is
<?xml version="1.0" encoding="utf-16"?>
<RSS xmlns:jwplayer="http://support.jwplayer.com/customer/portal/articles/1403635-media-format-reference#feeds" version="2.0">
  <Channel>
    <item>
      <title>Overlay HD/CC</title>
      <guid>1</guid>
      <description>This example shows tooltip overlays for captions and quality.</description>
      <jwplayer:image>http://content.jwplatform.com/thumbs/3XnJSIm4-640.jpg</jwplayer:image>
      <jwplayer:source file="http://content.jwplatform.com/videos/3XnJSIm4-DZ7jSYgM.mp4" label="720p" />
      <jwplayer:source file="http://content.jwplatform.com/videos/3XnJSIm4-kNspJqnJ.mp4" label="360p" />
      <jwplayer:source file="http://content.jwplatform.com/videos/3XnJSIm4-injeKYZS.mp4" label="180p" />
      <jwplayer:track file="http://content.jwplatform.com/captions/2UEDrDhv.txt" label="English" />
      <jwplayer:track file="http://content.jwplatform.com/captions/6aaGiPcs.txt" label="Japanese" />
      <jwplayer:track file="http://content.jwplatform.com/captions/2nxzdRca.txt" label="Russian" />
      <jwplayer:track file="http://content.jwplatform.com/captions/BMjSl0KC.txt" label="Spanish" />
    </item>
  </Channel>
</RSS>

相关内容

  • 没有找到相关文章

最新更新