Solved

How to character escape XAttribute values for valid XML?

Posted on 2009-03-29
4
2,027 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to sort columns in DataGridView 4 62
ModalPopup  question 22 56
Header Font Size in Grid View 6 47
GET INFO ABOUT WHAT THE PRINTER IS DOING ? 11 73
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:…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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