1

for the past 2 hours i have tried to parse an xml to json. in an example project that i did i did it like that:

string  xmlString = XDocument.Load("G00011071.xml").ToString();
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlString);

var json = Newtonsoft.Json.JsonConvert.SerializeObject(xml);

and it worked fine..

when i tried to integrate it in a project that i need to use it there i got an error:

An exception of type 'System.IO.FileLoadException' occurred in xmloutput.dll but was not handled in user code. Additional information: Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

i tried to solve it, but nothing helped - so i decided not to use Json.net and looked for alternatives, i found that post:

but my XML structure is much more complex then the one used there..

How can i achieve the same result as the one Json.net produced?

Community
  • 1
  • 1
Eran Meir
  • 923
  • 3
  • 17
  • 32
  • Can you upload your xml file? – ganchito55 Jan 24 '16 at 16:11
  • how can i post it, it's very long for here.. ? – Eran Meir Jan 24 '16 at 16:13
  • 1
    http://pastebin.com/ for example – ganchito55 Jan 24 '16 at 16:14
  • http://pastebin.com/Vdun68Xz – Eran Meir Jan 24 '16 at 16:18
  • @Micky the solution suggested there isn't fits my needs.. so why duplicate? – Eran Meir Jan 24 '16 at 16:26
  • Considering your XML is _"much more complex"_, I would advise seeking out why JSON.NET is not working for you in the first place. Maybe remove any existing reference and getting the latest JSON.NET NuGet? –  Jan 24 '16 at 16:26
  • I find a solution for you error [here](http://stackoverflow.com/questions/22507189/could-not-load-file-or-assembly-newtonsoft-json-version-4-5-0-0-culture-neutr) . I think that these json and xml are perfectly managed for JSON.Net and I think it is faster than other solution. – ganchito55 Jan 24 '16 at 16:27
  • I removed it. I didn't notice your second last line till after I marked it as duplicate :P –  Jan 24 '16 at 16:27
  • @ganchito55, i already saw it - i tried each of the answers there and nothing solved my problem. (wish they had) – Eran Meir Jan 24 '16 at 16:28
  • You seem to have a version mismatch between the assembly you referenced and the assembly that was available. If you have multiple projects make sure that all of them are using the same version of JSon.NET. Json.NET is the tool for the job and writing code (and then maintaining it) is not worth it. I think you can also try DataContractSerializer instead of JSon.NET - it is part of .NET Framework so you should not have mismatches. – Pawel Jan 24 '16 at 19:10
  • @Pawel, i'll try to look for an example on how to use "DataContractSerializer" , for the "mismatch between the assembly" only 2 project is in this solution- none of them use json.net – Eran Meir Jan 25 '16 at 07:23
  • i found that example [link](http://stackoverflow.com/questions/5010191/using-datacontractserializer-to-serialize-but-cant-deserialize-back), but i don't understand something. i need to call "Deserialize" with 2 parameters- string xml, Type toType, what should i sent to - "toType"? i just want to convert an XML string to JSON string – Eran Meir Jan 25 '16 at 07:51
  • Manage to solve the "mismatch between the assembly" apparently there was an older version of newsoft.json in other prooject – Eran Meir Jan 25 '16 at 10:18

1 Answers1

0

You seem to have a version mismatch between the assembly you referenced and the assembly that was available. If you have multiple projects make sure that all of them are using the same version of Json.NET. Json.NET is the tool for the job and writing code that is already available as a proven library (and then test and maintain it) is not worth it.

Pawel
  • 31,342
  • 4
  • 73
  • 104
  • in my solution there are 2 projects, but this solution is part of an other solution - the other solution contain "newtonsoft"- so there was a mismatch between the versions. Thanks – Eran Meir Jan 26 '16 at 09:05