-1

I have an XML file that pulls from a database. I'm getting an error on certain strings and URL's because it contains characters that the XML is treating like code and either gives a syntax error or says entity not defined. Is there a way, since the data is really a string, to set up the XML so when the data is in quotes, it ignores it as code and treats it like text? I created an example with the marker name and the URL having the errors. The marker name has the & sign and the URL has msa, ll, spn, and iwloc it doesn't like.

<?xml version="1.0" encoding="utf-8"  standalone="yes" ?>
<markers>
<marker name="C&O Canal Trail"
address=""
city=""
state=""
zip=""
image=""
width=""
height=""
lat="39.693978"
lng="-78.154822"
category="KML"
notes=""
url="https://maps.google.com/maps/ms?msid=209491726150360274926.0004e18eed520de4890e6&msa=0&ll=39.213103,-77.295685&spn=0.619262,1.540833&iwloc=0004e18eee21c28a5a313"
hike_distance=""
hike_trail_skill_level=""
hike_points_of_interest=""
Camping_Amenities=""
Camping_Best_Sites=""
Camping_Notes=""
/>
</markers>
2pourdrummer
  • 45
  • 1
  • 8

3 Answers3

0

You need to XML encode the text, so replace:

& with &amp; 
< with &lt;
" with &quot;
' with &apos;
> with &gt;

It's better to use a library to do this that roll your own code.

Tom
  • 43,583
  • 4
  • 41
  • 61
0

In XML, data contained inside a <[CDATA[ ... ]]> tag allows arbitrary character data. See What does <![CDATA[]]> in XML mean?

But won't work in attributes of tags.

Community
  • 1
  • 1
geocodezip
  • 158,664
  • 13
  • 220
  • 245
0

Tom is right.

Here is your exact code converted.

<?xml version="1.0" encoding="utf-8"  standalone="yes" ?>
<markers>
<marker name="C&amp;O Canal Trail"
address=""
city=""
state=""
zip=""
image=""
width=""
height=""
lat="39.693978"
lng="-78.154822"
category="KML"
notes=""
url="https://maps.google.com/maps/ms?msid=209491726150360274926.0004e18eed520de4890e6&amp;msa=0&amp;ll=39.213103,-77.295685&amp;spn=0.619262,1.540833&amp;iwloc=0004e18eee21c28a5a313"
hike_distance=""
hike_trail_skill_level=""
hike_points_of_interest=""
Camping_Amenities=""
Camping_Best_Sites=""
Camping_Notes=""
/>
</markers>

For reference here are the XML reserved characters : http://msdn.microsoft.com/en-us/library/ms145315(v=sql.90).aspx

As a recommendation, I would suggest that you scrub the data as it's coming in from the database before it hits the XML Reader/Writer and do a replace on all of the reserved characters found in your data.

Alternatively you can do this replace to your data inside your database - but then you'd be only patching this problem for XMLRead/Write instead of doing it dynamically for your specific implementation.

fenix
  • 162
  • 10