Solved

How to character escape XAttribute values for valid XML?

Posted on 2009-03-29
4
1,934 Views
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

0
Comment
Question by:dazkraz
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
Tiggerito earned 250 total points
Comment Utility
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.
0
 
LVL 9

Assisted Solution

by:Sreedhar Vengala
Sreedhar Vengala earned 250 total points
Comment Utility
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!" />
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
Comment Utility
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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:…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

10 Experts available now in Live!

Get 1:1 Help Now