Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now