2

I have a webapi (.NET core 3.1) method which is getting XML and deserializing to object. But some of the tag openings and closings are not same. For example if opens with <Address> then it closed with </address>. So the XML is not well-formed. I know XML is case-sensitive, but is there any option that we can ignore that case-sensitivity?

The XML is something like this:

<?xml version="1.0" encoding="UTF-8"?>
<Orders>
    <User>UserName</User>
    <Address>address line</address>
</Orders>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
Kadir
  • 3,094
  • 4
  • 37
  • 57
  • Why not fix the buggy XML producer instead? *That* is the real bug. `Address` and `address` are two different XML elements. – Panagiotis Kanavos Jan 08 '21 at 12:46
  • It's not on our side. It's coming from SAP and they don't want to fix it. – Kadir Jan 08 '21 at 12:47
  • SAP doesn't do that. Custom partner or implementer code does that. They got paid to write some extensions that were broken. They should fix them. One could argue that the bug is so bad that even if the project is out of support, they *have* to fix it because they never delivered - that's not XML – Panagiotis Kanavos Jan 08 '21 at 12:49
  • Even if you are the partner, and "they" is the customer whose buggy code produces invalid XML, they're still not fulfilling their obligations of providing valid XML. BTW SAP has great SOAP support and .NET integration. If *SAP* had problems, hundreds of thousands of companies would have noticed 20 years ago. – Panagiotis Kanavos Jan 08 '21 at 12:51
  • To put it another way, one your project manager would understand, if they want you to do extra job to handle non-XML text, shouldn't you be paid for it? Who's going to make the decision to work for free? – Panagiotis Kanavos Jan 08 '21 at 12:53
  • As for what to do when the PMs decide whose job it is, one option is to use XmlReader and read the file tag by tag. No library would be able to deserialize the entire document in that state. `Address` and `address` are different elements so it's perfectly valid to have `
    `
    – Panagiotis Kanavos Jan 08 '21 at 13:01
  • Another option would be to use text replacements or a regular expression to replace invalid tags before loading it. In both cases you'd either have to create a custom binder or read the contents of the Request body directly. That's days of work wasted fixing someone else's bug, without pay – Panagiotis Kanavos Jan 08 '21 at 13:02
  • Don't refer to this as XML. It isn't XML, it's garbage. If they're not prepared to fix it, then you're dealing with someone who doesn't mind sending you garbage, and the only possible course of action is to stop dealing with them - if they get little things like that wrong, then there are almost certainly much bigger things wrong as well. – Michael Kay Jan 08 '21 at 21:28

1 Answers1

2

No

XML component names, including element names, are case sensitive. There is no provision within the standard to change that, nor should you attempt do so on an ad hoc basis because you'd be undermining the XML standard and all benefits of having a standard.

But what should I do?

  1. Fix the problem at the source that's generating the bad "XML" (preferably), or
  2. Fix the bad XML itself. See How to parse invalid (bad / not well-formed) XML?
kjhughes
  • 106,133
  • 27
  • 181
  • 240