Click here to Skip to main content
13,249,174 members (34,696 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
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 5:02
memberpm823-Apr-14 5:02 
Thanks Jaime, this article's "trick" helped me a great deal as I am a VB developer trying to get into C#. Smile | :)

Exisiting VB code:
            Dim webLicXML As XDocument = XDocument.Load(WEBLICENSEFILE)
            Dim nod As XElement
            Dim nodX As XElement = webLicXML.Element("Customers")
            Dim licExpiryDate As String = DateTime.ParseExact(dtpDateNew.Value.Date, "dd/MM/yyyy", System.Globalization.CultureInfo.CreateSpecificCulture("en-GB"))
            nod = <License Customer=<%= tbxCompany.Text %> Site=<%= tbxLocation.Text %>>
                          <RunTime><%= licExpiryDate %></RunTime>

C# conversion:
             XDocument webLicXML = XDocument.Load(WEBLICENSEFILE);
             XElement nod;
             XElement nodX = new XElement(webLicXML.Element("Customers"));
             CultureInfo provider = CultureInfo.CreateSpecificCulture("en-GB");
            string dateString = dtpDateNew.Value.ToShortDateString();
            string dateFormat = "dd/MM/yyyy";
            string licExpiryDate = DateTime.ParseExact(dateString, dateFormat, provider).ToShortDateString();
            nod = XElement.Parse(
                @"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
                    <License Customer='" + tbxCompany.Text + @"' Site='" + tbxLocation.Text + @"'>
                          <RunTime>" + licExpiryDate + @"</RunTime>

For attributes use the apostrophe to enclose the value, and for each section of the string use the verbatim string literal @"

Hope this helps,
GeneralMy vote of 1 Pin
pdamp20-Feb-13 5:43
memberpdamp20-Feb-13 5:43 
GeneralRe: My vote of 1 Pin
Jaime Olivares20-Feb-13 5:53
memberJaime Olivares20-Feb-13 5:53 
QuestionXML Literals are queryable. Pin
s_p_s2-Feb-13 9:21
members_p_s2-Feb-13 9:21 
AnswerRe: XML Literals are queryable. Pin
Jaime Olivares20-Feb-13 5:55
memberJaime Olivares20-Feb-13 5: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
Web03 | 2.8.171114.1 | Last Updated 4 Sep 2010
Article Copyright 2010 by Jaime Olivares
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid