1

I have a JSON string as :

var jsonString = JSON.stringify(dataObject);
document.getElementById("hdnChromedata").value = jsonString;

==> hdnChromedata = JSON string

BUT in a different code section I am storing XML serialized string to "hdnChromedata" .as :

XmlSerializer xmlSerializer = new XmlSerializer(vinDescription.GetType());
StringWriter textWriter = new StringWriter();

xmlSerializer.Serialize(textWriter, vinDescription);
this.hdnChromedata.Value = textWriter.ToString();

==> hdnChromedata = XML string

AND while retriving the values I am deserializing the string like this :

XmlDocument doc = new XmlDocument();
doc.LoadXml(this.hdnChromedata.Value);
XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);

XmlSerializer ser = new XmlSerializer(decodedInfo.GetType());
object textObj = ser.Deserialize(reader);
vinDescription = (AutoExact.AEVINDecoderService.VINDescription)textObj;

HERE the line doc.LoadXml(this.hdnChromedata.Value) is throwing error when hdnChromedata is a JSON string.

My question is, HOW can I make this JSON String to XML string?

OR is there any other to address this?

Basically I need a method to convert JSON string to XML string in ASP.NET 1.1

3 Answers 3

3

You can use the Json.NET library's JsonConvert for that. See the specifics at http://james.newtonking.com/projects/json/help/index.html?topic=html/ConvertingJSONandXML.htm.

Json.NET is an open-source JSON handling library for .NET, and it's the best there is.

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

7 Comments

Is it some kind of third party reference I will need to add? I afraid I can not reference it from my project..
Yes, you need to reference the DLL from your project.
Is not there any other way to convert JSON string to XML string..I dont think referencing a DLL from project is going to be feasible for me..
Are you saying you cannot use any libraries in your projects?? That would mean that you have to write every bit of functionality you might need yourself unless it happens to be in the System library...
Then, if you don't mind my saying so, your client is a fool, and this makes your life much harder.
|
0

No need for conversion, just test the first character of the string before deserialising it. If the string starts with <, then treat it as XML, and if it starts with {, then treat it as JSON.

7 Comments

What if it starts with ' '? :)
I guess this depends on the OP's requirements, however I like this approach
@L.B: throw new InvalidOperationException :-)
@ChristianHayter.. I have written a lot of code which uses the XML object...If I will follow the approach you suggested, I will have to write the same code again for JSON object too..
@Learner: You should not be writing two different kinds of data to the same variable in different places. If you choose to do that, you must accept the consequences. Pick one data format and stick with it.
|
-1

Use json-lib, a library which adds JSON support to any Java program. json-lib provides an XMLSerializer which can be used to output XML from a JSON object.

https://discursive.atlassian.net/wiki/display/CJCOOK/Converting+JSON+to+XML

Comments

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.