我尝试使用 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/