c# - 使用 json.net 将 xml 转换为 json

标签 c# xml json windows-runtime json.net

我尝试使用 Json.Net 将我的 XML 字符串转换为 Json

在 Json.Net Documentation它说我必须使用此代码将 xml 转换为 json:

string xml = @"<person id='1'>
         <name>Alan</name>
        <url>http://www.google.com</url>
         <role>Admin1</role>
     </person>";

 XmlDocument doc = new XmlDocument();
 doc.LoadXml(xml);

string json = JsonConvert.SerializeXmlNode(doc);

但在我的 Windows 8 应用程序中,我找不到 XmlDocument 类,也找不到 SerializeXmlNode。

我尝试了这些类和函数:

 var result = await response.Content.ReadAsStringAsync();
 XDocument xdoc = new XDocument();
 xdoc = XDocument.Load(result);
 // Parse the JSON Radio data
 string jsonText = JsonConvert.SerializeXNode(xdoc);
 var radios = JsonArray.Parse(result);

但是我得到以下错误:

An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code

Additional information: Illegal characters in path.

If there is a handler for this exception, the program may be safely continued.

结果我加载了正确的 xml。开始于:

<?xml version="1.0" encoding="utf-8"?>
<item>...</item>

最佳答案

使用 XDocument.Parse 而不是 XDocument.Load 从 url 加载 xml

关于c# - 使用 json.net 将 xml 转换为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12276915/

相关文章:

c# - Java 到 C# - CallableStatement 转换

c# - 一个单词并打印它的不同 3 个字母组合

c# - MVC Owin Cookie 身份验证 - 覆盖 ReturnUrl 生成

xml_grep 获取节中的相关信息

jquery - ASP.NET MVC + jQuery - 当我通过 jQuery $.post 表单时,如何保留 .NET 行结尾?

json - 在 Mac 上使用 Safari 通过 HTTP 发送 POST 请求

c# - 为 Azure Functions 设置 Serilog

java - android 在 onClick 之后在新 Activity 中访问 EditText 数据

java - Android,org.simpleframework.xml 持久化异常,元素 'foo' 已被使用

json - 如何将结构编码回字节缓冲区