Solved

How to character escape XAttribute values for valid XML?

Posted on 2009-03-29
4
1,974 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
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 250 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
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24036265
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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