Akbar Badhusha
Akbar Badhusha

Reputation: 2627

Parsing a XML response back to JSON

I'm working on project where i have to make an API request. When I post a JSON request to the server I get the following XML as response:

"<response>
    \r\n  
    <data>
       \r\n   
        <status no=\"0\" substatus=\"0\">
           Connection succeeded
        </status>\r\n 
    </data>
        \r\n
</response>"

I need to convert the response back to JSON. But when I try to parse it i'm getting an error saying

'Unexpected character encountered while parsing value: <. Path '', line 0, position 0.'

I'm using NewtonSoftJSON for conversion.

This is the code I'm using to convert the XML string back to JSON:

var response = JsonConvert.DeserializeXmlNode(xmlResponse);

How can I achieve this?

Upvotes: 0

Views: 9574

Answers (3)

Akbar Badhusha
Akbar Badhusha

Reputation: 2627

I have figured out the issue. rene's answer helped to figure out the how to convert from xml to json. But the issue was with my response. I was able to fix it by removing the /" and unwanted double quotes from the response.

 response = response.Replace("<response>\"", "<response>")
                 .Replace("\"<response>", "<response>")
                 .Replace("\\", "")
                 .Replace("rn", string.Empty);

var xmlReader = XmlReader.Create(new StringReader(response));
var doc = new XmlDocument();
doc.Load(xmlReader);

var jsonResponse = JsonConvert.SerializeXmlNode(doc);

Upvotes: 1

rene
rene

Reputation: 42493

I would be highly confused by any webapi that expect JSON as input and return XML as its response. But if that is really what you're looking at then you need to take the approach in Serializing that XML response into either an XmlDocument or the newer XDocument/XNode and then Serialize an instance of one of those into JSON.

The XmlDocument type has a Load method that consumes a XmlReader. (it also offers a LoadXml but I wanted to show several options here).

The XDocument type has a Parse method that consumes a string with XML.

Based on your example input you can use two approaches:

var xml = @"<response>
    <data>
        <status no=""0"" substatus=""0"">
           Connection succeeded
        </status>
    </data>
</response>";

// xmldocument
var xmlReader = XmlReader.Create(new StringReader(xml));
var doc = new XmlDocument();
doc.Load(xmlReader);
var response = JsonConvert.SerializeXmlNode(doc);

response.Dump("XMLDoc to Json "); // LINQPad output

// or XDOcument

response = JsonConvert.SerializeXNode(XDocument.Parse(xml));

response.Dump("XDocument to Json");// LINQPad output

And this will be the result:

enter image description here

Upvotes: 3

sɐunıɔןɐqɐp
sɐunıɔןɐqɐp

Reputation: 3512

There is no direct conversion from XML to JSON. And you should be using a XML parser for this response.

Parsing XML (C#) :

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/parsing-xml

I also suggest reading a little bit more about serialization, since this question actually makes no sense.

Upvotes: 2

Related Questions