0

I am trying to load XML files which were generated in VB6 application using ADODB into .Net application. Some of theses XMl files contain invalid characters that throws error on .Net side but loads perfectly fine via the original VB6 application. For example

VB6 code (this works fine):

Dim xmlDoc As MSXML.DOMDocument
xmlDoc.Load(XMLfilename)

C# code (this throws error 'john' is an unexpected token)

XmlDocument xmlDoc = new XmlDocument();
xmlDoc .Load(XMLfilename);

This is an example of row that throws the error: 'Cliff' is an unexpected token. Expecting white space

<rs:data>
<z:row ID="1234" ENRODATE="2010-11-12" LastName="Van "Cliff" Anderson" FirstName="Mark" GENDER="MALE"/>
</rs:data>

I do understand the above is not a valid XML but VB6 accepts this with no issue. Is there anyway to deal with problem like this?

Oded
  • 489,969
  • 99
  • 883
  • 1,009
John Sheridan
  • 83
  • 1
  • 2
  • 10
  • I'd suspect the VB side isn't using actual XML, but is simply outputting strings enclosed in XML elements, which doesn't make it "accepted by VB6". It means somebody wrote some bad code which outputs supposedly XML-formatted text without any sort of validation. The way to deal with it is either to fix the VB6 app, or write a function (or application) that goes through and fixes things like this before trying to use it as XML. – Ken White Jun 28 '12 at 20:10

2 Answers2

2

You have extra quotes around Cliff that cause invalid XML syntax

LastName="Van "Cliff" Anderson"

Apparently the VB6 parser was somehow forgiving of this. You will need to remove the quotes or properly escape them.

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
1

To fix this problem, you have to analyze XML.

LastName="Van "Cliff" Anderson" - is invalit becouse there are " char, which end LastName. You have LastName = "Van " there you have some Cliff" Anderson" which is not valid.

To solve this problem, you have to find start of this attribute and end of this attribute LastName = (start)"Van "Cliff" Anderson"(end).

You have to change XML generated from VB6 to not use attribute, but use new XML Element <LastName>Van "Cliff" Anderson"<\LastName>. If you cant change output from VB6, you have to do change manually before doing xmlDoc .Load(XMLfilename);.

The result XML:

 <z:row ID="1234" ENRODATE="2010-11-12" FirstName="Mark" GENDER="MALE">
     <LastName>Van "Cliff" Anderson<\LastName>
 <\z:row>
Jakub Čermoch
  • 431
  • 1
  • 9
  • 20
  • 1
    XML attributes cannot contain markup, including CDATA. Consult http://stackoverflow.com/questions/1289524/is-it-possible-to-have-html-text-or-cdata-inside-an-xml-attribute – Dour High Arch Jun 28 '12 at 22:21
  • So only way to use xmlDoc is change lastname from attribute to element ` Van "Cliff" Anderson<\LastName><\z:row> ` You can do this ni VB6 or before calling `xmlDoc.Load` – Jakub Čermoch Jun 29 '12 at 05:52
  • Jakub, that will work. If you change your answer to that I will upvote it. – Dour High Arch Jun 29 '12 at 19:38