Solved

Write "&" not "&" to XML file with Linq

Posted on 2013-12-04
4
919 Views
Last Modified: 2016-02-10
I am stuck.  I have an XML file that I am using Linq to edit.  This particular XML file has an ampersand (&) in the text of a Tag.  I am editing other parts of this string, but the '&' should not be touched.  However, when I go to write my XML to disk, the '&' is replaced with '&'  I understand that ampersands are illegal characters in XML and that is why this happens, but this particular file has to have it.  It is not my file, so I can't help that fact.

Rough example:
XElement root = XElement.Load(localXMLPath);
var myElement= root.Element("System").Element("Math");
myElement.Value =  = "Object1&Object2";
root.Save(localXMLPath);

Open in new window


Output of myElement tag is always "Object1&Object2"

Hope that explains the issue and I hope there is a workaround other than writing it to disk and then using a text editor to remove the "amp;".  Thanks.
0
Comment
Question by:efryevt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:paulfarmer
ID: 39695865
0
 

Accepted Solution

by:
efryevt earned 0 total points
ID: 39695967
To elaborate for anyone that comes across this questions.  I think what paulfarmer was pointing out was all XML parsers should inherently handle the "&" entity.  If not I guess declaring it will fix it.  I think this link has a better explanation. http://www.ibm.com/developerworks/library/x-entities/

Thanks, paulfarmer.
0
 
LVL 4

Expert Comment

by:paulfarmer
ID: 39696021
That is what I was pointing out. And, I agree, I should have explained that.
0
 

Author Closing Comment

by:efryevt
ID: 39705695
Single link, no explanation.  Was pretty obscure to the actual solution.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question