如何解析格式不正确的XML到JSON [英] How to parse not well formed XML to JSON

查看:114
本文介绍了如何解析格式不正确的XML到JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码格式不正确,我需要在C#中将其解析为JSON。



看起来好像



I have code with not well formatted XML and I need to parse it to JSON in C#.

It looks like

<mediaType>
<example>Example</example>
<example1>WrongEnd </example1>
<example2>WrongElement </exampl>

</mediaType>







输出应为:






Output should be:

{
  "mediaType"{
     "Example": "WrongEnd",
   }
}





什么是最好的处理的方式?要创建对象类并将值放入其中?我不确定如何跳过错误的值。



我尝试过:



使用xmlReader设置一致性级别片段,但我在阅读时遇到错误。



What would be the best way to handle with that? To create object class and put values into it? I'm not sure how to skip wrong values.

What I have tried:

Using xmlReader with setting conformance level to fragment, but I'm getting error while reading.

推荐答案

根据定义,格式错误的XML不是XML。它是一个看起来像XML的文本文件,但不是普通的解析器。



您可以尝试自己编写一个。祝你好运,因为它并不容易。



或者,您可以尝试使用它: GitHub - XmlParser [ ^ ]解析器。它应该是容错的,但我不知道这种容忍度是多么广泛。



我认为更大的问题是如何确定哪些部分是XML应该导出到JSON文件。目前还不完全清楚你如何识别有效数据以及你发布的例子中的内容。
By definition, malformed XML isn't XML. It's a text file that looks like XML to us, but not to normal parsers.

You can try to write one yourself. Good luck with that as it's not going to be easy.

Or, you can try using this: GitHub - XmlParser[^] parser. It's supposed to be fault-tolerant, but I have no idea how extensive that tolerance is.

I think the bigger problem is going to be how you identify which parts of the XML are supposed to be exported to the JSON file. It's not entirely clear how you identify what is valid data and what isn't from the example you posted.


这篇关于如何解析格式不正确的XML到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆