Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to character escape XAttribute values for valid XML?

Posted on 2009-03-29
4
Medium Priority
?
2,102 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
[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
4 Comments
 
LVL 23

Accepted Solution

by:
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.
0
 
LVL 9

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!" />
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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