【问题标题】:How can i convert XML to json (without using Json.NET)我如何将 XML 转换为 json(不使用 Json.NET)
【发布时间】:2016-01-24 16:09:11
【问题描述】:

在过去的 2 个小时里,我尝试将 xml 解析为 json。 在我做的一个示例项目中,我是这样做的:

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

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

它工作得很好..

当我尝试将它集成到我需要在那里使用它的项目中时 我有一个错误:

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)

我试图解决它,但没有任何帮助 - 所以我决定不使用 Json.net 并寻找替代方案,我发现post

但我的 XML 结构比那里使用的要复杂得多..

我怎样才能达到与 Json.net 产生的结果相同的结果?

【问题讨论】:

  • 你能上传你的xml文件吗?
  • 怎么发啊,这里太长了..?
  • pastebin.com 例如
  • @Micky 建议的解决方案不符合我的需求.. 那为什么要重复?

标签: c# json xml xml-parsing


【解决方案1】:

您引用的程序集与可用的程序集之间似乎存在版本不匹配。如果您有多个项目,请确保所有项目都使用相同版本的 Json.NET。 Json.NET 是完成这项工作的工具,编写已经作为经过验证的库可用的代码(然后对其进行测试和维护)是不值得的。

【讨论】:

  • 在我的解决方案中有 2 个项目,但这个解决方案是另一个解决方案的一部分 - 另一个解决方案包含“newtonsoft” - 所以版本之间存在不匹配。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
  • 2021-03-16
  • 1970-01-01
  • 2015-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多