Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have code with not well formatted XML and I need to parse it to JSON in C#.

It looks like

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

</mediaType>



Output should be:

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


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.
Posted
Updated 8-May-19 15:10pm

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900