Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

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

Posted on 2013-12-04
4
Medium Priority
?
987 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

648 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