我试图用Json.Net 将我的XML字符串转换为Json
在Json.Net文档中,它说我必须使用此代码将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);
但在我的Windows8应用程序中,我找不到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