C# & SQL & XML - Escape characters returned in XML

I am pulling a field out of a SQL table which contains XML. I need to be able to load this XML into an XElement variable, but the XML is being returned with escape characters. How can I load the XML without escape characters?

Value in my DataReader object
"<entry timestamp=\"2011122200162561\" actor=\"1234\" authenticatedactor=\"1234\" effective=\"19010101\" ... more attributes

Value when writing to Console.WriteLine <--- This is what I want to pass into XElement.Load
<entry timestamp="2011122200162561" actor="1234" authenticatedactor=\"1234\" effective=\"19010101\"
nightshadzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

Instead of XElement.Load, use XElemet.Parse as below


string value = "<entry timestamp=\"2011122200162561\" actor=\"1234\" authenticatedactor=\"1234\" effective=\"19010101\"></entry>";
            
  string result = value.Replace(@"\", "");

 XElement element = XElement.Parse(result, LoadOptions.PreserveWhitespace);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nightshadzAuthor Commented:
Thanks Lokesh!

Is this part necessary though?  When I look at the contents of result the \ is still there and when I comment out the line, the XElement.Parse works fine. Can you explain what this is doing please?

string result = value.Replace(@"\", "");

Open in new window

käµfm³d 👽Commented:
What is the data type of the column that holds the XML?
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Lokesh B RDeveloperCommented:
Hi,

No the replace function line is not required and since i was testing i posted that line as well.

You can use the following line to load the XML string to XElement.

string value = "<entry timestamp=\"2011122200162561\" actor=\"1234\" authenticatedactor=\"1234\" effective=\"19010101\"></entry>";
         
 XElement element = XElement.Parse(value);

Open in new window

nightshadzAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for nightshadz's comment #a40798486

for the following reason:

Thanks!

Kaufman, it's defined at (text, null).
nightshadzAuthor Commented:
Thanks!

Kaufman, it's defined as (text, null).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.