Solved

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

Posted on 2013-12-04
4
925 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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