我尝试使用Json.Net将我的XML String转换为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>
答案 0 :(得分:5)
使用XDocument.Parse
代替XDocument.Load
从网址加载xml