【问题标题】:Convert Json to XML String with namespace?使用命名空间将 Json 转换为 XML 字符串?
【发布时间】:2014-05-09 12:31:24
【问题描述】:

我有一个这样的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>

【问题讨论】:

  • 返回 xmlDoc.OuterXml
  • 感谢您提出这个问题 - 花了几个小时的谷歌搜索才弄清楚如何让 Json.net 尊重 XML 命名空间。我猜测@xmlns : "url" 会设置默认命名空间,确实如此。此功能的文档记录很差。

标签: c# xml json json.net


【解决方案1】:

我不确定您的代码为什么会抛出异常,因为自从 XML 成功加载到 XmlDocument 对象后,它一定是一个有效的 XML。

我认为获取格式化 XML 字符串的另一种方法是使用将未格式化的 XML 字符串从 XmlDocument.OuterXml 加载到 XDocument 然后将其转换为格式化的 XML 字符串:

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

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

【讨论】:

  • 非常感谢。我发现自己使用 xmlDoc.OuterXml 已经解决了问题。
  • 更多问题,它显示 hxxp://content.jwplatform.com/thumbs/3XnJSIm4-640.jpg 但我的预期是 hxxp:// content.jwplatform.com/thumbs/3XnJSIm4-640.jpg
猜你喜欢
  • 2013-04-01
  • 1970-01-01
  • 2012-10-26
  • 2016-03-17
  • 1970-01-01
  • 2015-11-16
  • 2012-05-18
  • 2010-12-16
  • 2022-10-05
相关资源
最近更新 更多