1

I try to convert my XML String to Json with Json.Net

In the Json.Net Documentation it says that i have to use this code to convert xml to 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);

But in my Windows 8 App i can't find the XmlDocument class neither the SerializeXmlNode.

I tried it with these classes and functions:

 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);

But i get the following error:

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.

In result i have the correct xml loaded. Starting with:

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

1 Answer 1

5

use XDocument.Parse instead of XDocument.Load which loads the xml from an url

Sign up to request clarification or add additional context in comments.

1 Comment

thanks this solved the problem. i can load the xml now :) I'll accept the answer as soon as i can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.