-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>
2
  • what programming language / platform are you using
    – Tom
    Commented Jul 17, 2013 at 19:30
  • Not sure what you mean, its generated as a text file from an access database, with an xml extension. Commented Jul 17, 2013 at 19:33

3 Answers 3

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.

1
  • I can't expect uses to enter data that way, and it also doesn't help with the URL that has msa, ll, smn and iwloc. Commented Jul 17, 2013 at 19:36
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.

1
  • Not sure how that helps Commented Jul 17, 2013 at 19:39
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.