Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use XML Literals in C#?

Tags:

c#

xml

literals

Is it possible to add literal XML data within a C# code file? I'm currently using a multiline string literal but it gets messy as you can see. Any better way of doing this?

string XML = @"<?xml version=""1.0"" encoding=""utf-8""?> <customUI xmlns=""http://schemas.example.com/customui"">     <toolbar id=""save"">     </toolbar> </customUI>"; 
like image 914
Robin Rodricks Avatar asked Jan 07 '12 09:01

Robin Rodricks


2 Answers

XML literals are a feature of VB.NET, not C#.

What you have posted is as close as you can get in C#.

You may want to consider replacing the embedded double quotes with single quotes though (as both types are valid XML).

For larger amounts of XML you may want to consider the answer from Marc - using an XML file (loaded once and stored in memory), so you can take advantage of the XML editor.

like image 166
Oded Avatar answered Sep 28 '22 13:09

Oded


If the XML is big enough to get in the way, consider using a flat .xml file instead, either loaded from disk, or embedded as a resource. As long as you only load it once (perhaps in a static constructor) this will make no difference to performance. It will be considerably easier to maintain, as it will use the IDE's XML file editor. And it won't get in the way of your code.

like image 42
Marc Gravell Avatar answered Sep 28 '22 15:09

Marc Gravell