1

My application needs to parse XML files provided by the user, so I don't have control over its quality.

I have an XML which is incorrect, snippet I have shared below. Notice the missing prefix, xmlns:=

<?xml version="1.0" encoding="utf-8" ?>
<ncx version="2005-1" xml:lang="por" xmlns:="http://www.daisy.org/z3986/2005/ncx/">
</ncx>

When I try to use XDocument.Load() in C#, it throws this exception and rightly so -

System.Xml.XmlException: 'Name cannot begin with the '=' character, hexadecimal value 0x3D. Line 2, position 44.'

Is there a way I can correct such minor errors in the XML? I tried looking for it but didn't find any good solution. And I want to avoid string search/replace to fix such errors.

ac-lap
  • 1,623
  • 2
  • 15
  • 27
  • 1
    Such a file is technically not an XML file. It is of course a text file and can be loaded as such. If you have an idea of the possible defects, you might be able to correct it at this level. That said, it is always better to correct the problem at the source. – Klaus Gütter Jan 03 '23 at 16:03

0 Answers0