【发布时间】: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"会设置默认命名空间,确实如此。此功能的文档记录很差。