Click here to Skip to main content
12,887,921 members (39,223 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 31 May 2010

TRICK: Xml Literals for C#

, 4 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Workaround to mimic's Xml Literals in C#
Please have a glance to jpaulino's article Xml Literals[^]
Unfortunately, this useful feature doesn't work for C#. But there is a workaround to mimic this functionality.
It combines 2 characteristics:
- Well formed xml documents can use quotes or apostrophes for attributes.
- C# string literales can have newlines when using the verbatim mode (leading @).
So, we can create a LINQ XDocument by passing the xml string to the Parse method. Unique condition is to be sure that attributes are not enclosed between quotes, but between apostrophes, like the following example:

using System.Xml.Linq;
XDocument bookList = XDocument.Parse(
    @"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
    <!-- List of books and magazines -->
            <book name='The Hunger&apos;s Games' author='Suzanne Doe'/>
            <book name='Breaking Dawn' author='Stephenie Meyer'/>
            <book name='The Last Song' author='Nicholas Sparks'/>
            <magazineName>&quot;MSDN Magazine&quot;</magazineName>
            <magazineName>&quot;Code Magazine&quot;</magazineName>

Whenever the quote is needed you can use the &quot entity, and for apostrophes use the &apos entity.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jaime Olivares
Peru Peru

Computer Electronics professional, Software Architect and senior Windows C++ and C# developer with experience in many other programming languages, platforms and application areas including communications, simulation systems, PACS/DICOM (radiology), GIS, 3D graphics and HTML5-based web applications.
Currently intensively working with Visual Studio and TFS.

You may also be interested in...

Comments and Discussions

QuestionAn example with a string mixed with textbox values Pin
pm823-Apr-14 4:02
memberpm823-Apr-14 4:02 
GeneralMy vote of 1 Pin
pdamp20-Feb-13 4:43
memberpdamp20-Feb-13 4:43 
GeneralRe: My vote of 1 Pin
Jaime Olivares20-Feb-13 4:53
memberJaime Olivares20-Feb-13 4:53 
QuestionXML Literals are queryable. Pin
s_p_s2-Feb-13 8:21
members_p_s2-Feb-13 8:21 
How would you query your xml literal? Use select statements to build complex xml documents? Really I don't see any resemblence at all.
AnswerRe: XML Literals are queryable. Pin
Jaime Olivares20-Feb-13 4:55
memberJaime Olivares20-Feb-13 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 4 Sep 2010
Article Copyright 2010 by Jaime Olivares
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid