Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 43519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:30:29+00:00 2026-05-10T15:30:29+00:00

I have inherited a poorly written web application that seems to have errors when

  • 0

I have inherited a poorly written web application that seems to have errors when it tries to read in an xml document stored in the database that has an ‘&’ in it. For example there will be a tag with the contents: ‘Prepaid & Charge’. Is there some secret simple thing to do to have it not get an error parsing that character, or am I missing something obvious?

EDIT: Are there any other characters that will cause this same type of parser error for not being well formed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:30:29+00:00Added an answer on May 10, 2026 at 3:30 pm

    The problem is the xml is not well-formed. Properly generated xml would list the data like this:

    Prepaid & Charge

    I’ve fixed the same problem before, and I did it with this regex:

    Regex badAmpersand = new Regex("&(?![a-zA-Z]{2,6};|#[0-9]{2,4};)"); 

    Combine that with a string constant defined like this:

    const string goodAmpersand = "&"; 

    Now you can say badAmpersand.Replace(<your input>, goodAmpersand);

    Note a simple String.Replace("&", "&amp;") isn’t good enough, since you can’t know in advance for a given document whether any & characters will be coded correctly, incorrectly, or even both in the same document.

    The catches here are you have to do this to your xml document before loading it into your parser, which likely means an extra pass through the document. Also, it does not account for ampersands inside of a CDATA section. Finally, it only catches ampersands, not other illegal characters like <. Update: based on the comment, I need to update the expression for hex-coded (&#x…;) entities as well.

    Regarding which characters can cause problems, the actual rules are a little complex. For example, certain characters are allowed in data, but not as the first letter of an element name. And there’s no simple list of illegal characters. Instead, large (non-contiguous) swaths of UNICODE are defined as legal, and anything outside that is illegal.

    When it comes down to it, you have to trust your document source to have at least a certain amount of compliance and consistency. For example, I’ve found people are often smart enough to make sure the tags work properly and escape <, even if they don’t know that & isn’t allowed, hence your problem today. However, the best thing would be to get this fixed at the source.

    Oh, and a note about the CDATA suggestion: I use that to make sure xml I’m creating is well-formed, but when dealing with existing xml from outside, I find the regex method easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You should create one image with a width of 50px… May 11, 2026 at 3:51 pm
  • added an answer I can't guarantee this is the best way, but it's… May 11, 2026 at 3:51 pm
  • added an answer On the line the data will be garbled, which may… May 11, 2026 at 3:51 pm

Related Questions

Its been a long time since I had to do anything but minor fixes
A typical problem in OO programming is the diamond problem. I have parent class
I'm trying to convert an XML file into the markup used by dokuwiki, using
I have inherited a client site which crashes every 3 or 4 days. It

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.