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

  • SEARCH
  • Home
  • 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 6752503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:03:09+00:00 2026-05-26T13:03:09+00:00

I have an XML string that contains an apostrophe. I replace the apostrophe with

  • 0

I have an XML string that contains an apostrophe. I replace the apostrophe with its equivalent & parse the revised string into an XElement. The XElement, however, is turning the ' back into an apostrophe.

How do I force XElement.Parse to preserve the encoded string?

string originalXML = @"<Description><data>Mark's Data</data></Description>"; //for illustration purposes only
string encodedApostrophe = originalXML.Replace("'", "&#39;");
XElement xe = XElement.Parse(encodedApostrophe);
  • 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. Editorial Team
    Editorial Team
    2026-05-26T13:03:09+00:00Added an answer on May 26, 2026 at 1:03 pm

    This is correct behavior. In places where ' is allowed, it works the same as &apos;, &#39; or &#x27;. If you want to include literal string &#39; in the XML, you should encode the &:

    originalXML.Replace("'", "&amp;#39;")
    

    Or parse the original XML and modify that:

    XElement xe = XElement.Parse(originalXML);
    
    var data = xe.Element("data");
    
    data.Value = data.Value.Replace("'", "&#39;");
    

    But doing this seems really weird. Maybe there is a better solution to the problem you’re trying to solve.

    Also, this encoding is not “ASCII equivalent”, they are called character entity references. And the numeric ones are based on the Unicode codepoint of the character.

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

Sidebar

Related Questions

I have an xml document object that I need to convert into a string.
I have to replace the content of this xml string through java <My:tag>value_1 22&#xA;value_2
I have a string that contains well formed xml in it. I want to
I have a variable that contains a long string that represents an XML document.
Currently, I have a String object that contains XML elements: String carsInGarage = garage.getCars();
I want to know that I have the string.xml Resource file which contains status
I have a string (from a CDATA element) that contains description of XML. I
I have XML in the form of a String that contains <message>HELLO!</message> How can
I have a variable string that contains well-formed and valid XML. I need to
I have an Android Library Project that contains several styles/themes defined in its styles.xml

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.