C# 使用json.net将xml转换为json

C# 使用json.net将xml转换为json,c#,xml,json,windows-runtime,json.net,C#,Xml,Json,Windows Runtime,Json.net,我尝试使用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&

我尝试使用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);
但我得到了以下错误:

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

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

感谢这解决了问题。我现在可以加载xml:)我会尽快接受答案。谢谢,这解决了问题。我现在可以加载xml:)我会尽快接受答案。
<?xml version="1.0" encoding="utf-8"?>
<item>...</item>