How to character escape XAttribute values for valid XML?

Posted on 2009-03-29
Medium Priority
Last Modified: 2013-11-11
I am creating some XML using XDocument that will then be transformed.
However I get errors like:
Name cannot begin with the '<' character, hexadecimal value 0x3C. Line 8, position 13.

The reason is my XML is invalid because it has special characters in it.
Is there a way to char escape theses when creating LINQ for XML?
XElement xroot=new XElement("node");
xroot.Add(new XAttribute("nodeDesc","funny chars <test>"</t334est> description that makes xml invalid for a xslt transform!"));
string output=xroot.ToString();

Open in new window

Question by:dazkraz
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
LVL 23

Accepted Solution

Tony McCreath earned 1000 total points
ID: 24017908
Your example won't compile as it contains an unescaped double quote (") inside the string.

I don't have 3.5 but I would be very surprised if the X objects don't encode/decode correctly.

Assisted Solution

by:Sreedhar Vengala
Sreedhar Vengala earned 1000 total points
ID: 24036205
This works fine

var xroot=new XElement("node");
            xroot.Add(new XAttribute("nodeDesc","funny chars <test></t334est> \"description that makes xml invalid for a xslt transform!"));
            string output=xroot.ToString();

Output: <node nodeDesc="funny chars <test></t334est>" description that makes xml invalid for a xslt transform!" />

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

764 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