Friday, September 19, 2008

How to encode double quotes and newlines inside a C# string

It turns out that this is not as easy as it seems. The syntax is kludgy. I found the answer on Gus Perez's blog

Imagine that you want this literal string in your C# code:
<GC xmlns:dt="urn:schemas-microsoft-com:datatypes">
<pInLotIDs x:dt="list">
<item dt:dt="string">108P091G-000</item>
</pInLotIDs>
</GC>


Then you have to code this in C# as:


string sInputData = @"
<GC xmlns:dt=""urn:schemas-microsoft-com:datatypes"">
<pInLotIDs CSIM:dt=""list"">
<item dt:dt=""string"">108P091G-000</item>
</pInLotIDs>
</GC>


Ugh!

No comments: