Convert ‘\\’ to ‘\’ in c#

This post would sound pretty weird to any c# developer. However, a situation i had been in last week forced me to bring this into notice.

I had an XML file in which i had node values like ‘Requirements Management\XYZ’. I read the XML using XLinq and the c# string looked like ‘Requirements Management\\XYZ’. At first i felt its fine, as C# compiler added that extra backslash to handle an escape sequence. But when i tried using this value (stored in a variable), it was always returning me the string with ‘\\’. 😛

After 4 hours of trial and error, i found the solution. Believe it or not, i tried the extreme of exploiting the ‘Split’ and ‘Replace’ methods string gives me. 😛


Whenever you need to assign this variable to some object property, in my case Area Path and Iteration Path for my TFS 2010 SDK API, you just need to make use of the powerful ‘@’ operator & String.Format method

string strXyz = Xdoc..Load(“myXML.xml”).Element(“abc”).Value; //This returned ‘Requirements Management\\XYZ’

Now to use this:

Obj.XYZ = String.Format(@”{0}”,strXyz); //The string value assigned to my ‘Obj’ object will be ‘Requirements Management\XYZ’.

It doesn’t make much sense, but this will handle the conversion of the double slashes to slash conversion.

But if you are using strings all the way, there’s nothing to worry, C# compiler will handle such situations. For instance displaying it on a textbox or some label or something.

Ideally you cannot convert double slashes to single slashes.. 🙂

2 thoughts on “Convert ‘\\’ to ‘\’ in c#

  1. Hey there, I think your blog might be having
    browser compatibility issues. When I look at your blog in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, fantastic blog!

  2. He now lives in Sydney and is literary editor of the Australian. Oakley has published four novels, A Wild Ass of a Man (1967), A Salute to the Great McCarthy (1970), which was produced as a film, The Great McCarthy, in 1976, Let’s Hear It for Prendergast (1970) and The Craziplane (1989); a collection of short stories, Walking Through Tigerland (1977), which reprints only some of his stories that have appeared in magazines and newspapers since 1955; a collection of reminscences and reviews, Scribbling in the Dark (1985); and numerous plays, Witzenhausen, Where Are You? in Meanjin (1967) and Six One-Act Plays (1970), ed. cheap oakleys wholesale

Feel free to leave a reply here...

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s