473,785 Members | 2,969 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string.Format encoding?

Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(m yFormat);

....that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

....load xmlNode WorkNode...
string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
string formattedString = string.Format(m yFormat);

....the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters correctly?
Sep 28 '06 #1
6 5238
Scewbedew,

That's the ting, it isn't the same format string. The same string in an
XML file would have the character code 10 (newline), not "\n" in it. You
need to insert the newline character into your XML, not use "\n".

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Scewbedew" <Sc*******@disc ussions.microso ft.comwrote in message
news:BF******** *************** ***********@mic rosoft.com...
Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(m yFormat);

...that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

...load xmlNode WorkNode...
string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
string formattedString = string.Format(m yFormat);

...the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters
correctly?


Sep 28 '06 #2
Oh, I thought that string formatting was done by string.Format.. .silly me ;-)

I can't change the xml file, so I guess I'd have to do some subsititutions
of my own. Thanks for the info.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Scewbedew,

That's the ting, it isn't the same format string. The same string in an
XML file would have the character code 10 (newline), not "\n" in it. You
need to insert the newline character into your XML, not use "\n".

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Scewbedew" <Sc*******@disc ussions.microso ft.comwrote in message
news:BF******** *************** ***********@mic rosoft.com...
Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(m yFormat);

...that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

...load xmlNode WorkNode...
string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
string formattedString = string.Format(m yFormat);

...the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters
correctly?


Sep 28 '06 #3
On Thu, 28 Sep 2006 08:21:02 -0700, Scewbedew
<Sc*******@disc ussions.microso ft.comwrote:
>Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(m yFormat);

...that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

...load xmlNode WorkNode...
string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
string formattedString = string.Format(m yFormat);

...the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters correctly?
Interesting. Did you use the debugger to look at the string you got from the
xml file to verify that you actually got what you expected?
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Sep 28 '06 #4

Scewbedew wrote:
Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(m yFormat);

...that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

...load xmlNode WorkNode...
string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
string formattedString = string.Format(m yFormat);

...the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters correctly?
Probably a typo, but you posted "/n" instead of "\n". However, the more
important point is that you misunderstand who is doing what.

string.Format is not resolving \n into a newline character. The
compiler is doing that. In other words, the variable strFormat does not
contain:

L-i-n-e-1-\-n-L-i-n-e-2

In fact, it contains this:

L-i-n-e-1-newline-L-i-n-e-2

because the compiler has already resolved the \n notation into the
corresponding control character. So, string.Format doesn't "handle" the
\n notation at all. It just treats the newline like any other character
and puts it in the output string.

Now, when you read "Line1\nLin e2" from an XML file, what you're passing
to string.Format is exactly that sequence of characters:

L-i-n-e-1-\-n-L-i-n-e-2

and so that's what it puts in the output string.

So, the real question is how can you take a character string that may
contain some character escapes, and translate them into the appropriate
control characters. Perhaps someone else can answer that one.

Sep 28 '06 #5
Yes. In fact, I discovered exactly what Bruce Wood describes in another
answer to my question: the xml originated string included all the characters
unchanged, while the inline string included newline charactes instead of the
"\n" characters.

"Otis Mukinfus" wrote:
On Thu, 28 Sep 2006 08:21:02 -0700, Scewbedew
<Sc*******@disc ussions.microso ft.comwrote:
Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(m yFormat);

...that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

...load xmlNode WorkNode...
string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
string formattedString = string.Format(m yFormat);

...the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters correctly?
Interesting. Did you use the debugger to look at the string you got from the
xml file to verify that you actually got what you expected?
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Sep 28 '06 #6
Thanks for the clarification. You where correct about the typo, sorry about
that. In my mind, it was sooo very obvious that string.Format should handle
the formatting of the string, so I got really puzzled when it didn't happen.

I have verified exactly the behaviour you describe while debugging my app,
so it is now painfully obvious that my initial assumption was wrong (or...as
a matter of fact I'm never wrong, it's only the world that's temporary out of
sync...)

If anyone has any good input to how to make the character translation I'm
eager to take part of it.

"Bruce Wood" wrote:
>
Scewbedew wrote:
Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(m yFormat);

...that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

...load xmlNode WorkNode...
string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
string formattedString = string.Format(m yFormat);

...the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters correctly?

Probably a typo, but you posted "/n" instead of "\n". However, the more
important point is that you misunderstand who is doing what.

string.Format is not resolving \n into a newline character. The
compiler is doing that. In other words, the variable strFormat does not
contain:

L-i-n-e-1-\-n-L-i-n-e-2

In fact, it contains this:

L-i-n-e-1-newline-L-i-n-e-2

because the compiler has already resolved the \n notation into the
corresponding control character. So, string.Format doesn't "handle" the
\n notation at all. It just treats the newline like any other character
and puts it in the output string.

Now, when you read "Line1\nLin e2" from an XML file, what you're passing
to string.Format is exactly that sequence of characters:

L-i-n-e-1-\-n-L-i-n-e-2

and so that's what it puts in the output string.

So, the real question is how can you take a character string that may
contain some character escapes, and translate them into the appropriate
control characters. Perhaps someone else can answer that one.

Sep 28 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6548
by: WEIWEIWEI | last post by:
Hi Al I'd like to encode a string submitted from a utf-8 form in a aspx page to big5 Any ideas on how to do that I try sth like public static string unicode_big5(string src) { Encoding big5 = Encoding.GetEncoding("big5") Encoding unicode = Encoding.UTF8 byte unicodeBytes = unicode.GetBytes(src)
0
1152
by: Benoît | last post by:
Hi, I try to detect the encoding type of a string $myString which comes from a html file. $myString = file_get_contents($myFileName); Firstly : I tried to get the charset in <meta charset="iso-8859-1">. But I've the following problem. Internet Explorer write ISO-8859-1 even if we have save it in Unicode format (UTF-16).
6
53725
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how do I convert it to: dim myStr as string = "11,22,33,44"
4
3397
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char() (BinaryWriter.Write) from the string (String.ToCharArray), then use Stream.ToArray to convert everything to byte(). It works most of the time, but it happens that an error tells me something like "Additional information: Caractère de substitution faible trouvé...
4
4024
by: movieknight | last post by:
Hi, I have an application that stores raw .wav files (and also jpgs/bitmaps) within strings, and I need to sometimes convert these strings to byte arrays, and sometimes go from byte arrays back to strings. How do I convert this raw data from one format to another? I don't think system.text.encoding is useful here as the data is not in any particular encoding format, it's just raw data and it needs to stay
4
8381
by: Christina | last post by:
Hey Guys, Currently, I am using the below code: Dim oReqDoc as XmlDocument Dim requiredBytes As Byte() requiredBytes = System.Text.UTF8Encoding.UTF8.GetBytes(oReqDoc.InnerXml). Here, I am encoding my xml string in UTF8 format.
16
2155
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as sooooo slow. Anyone know how I can speed this functions up? I basically need to convert a byte to string, perform a function on each 'section' of the string, then reconvert it to a byte. The slow part is the conversion to and from byte, not the...
4
8253
by: J Peyret | last post by:
Well, as usual I am confused by unicode encoding errors. I have a string with problematic characters in it which I'd like to put into a postgresql table. That results in a postgresql error so I am trying to fix things with <string>.encode he Company�s ticker Trying for an encode:
5
2372
by: sniipe | last post by:
Hi, I have a problem with unicode string in Pylons templates(Mako). I will print first char from my string encoded in UTF-8 and urllib.quote(), for example string '£ukasz': ${urllib.unquote(c.user.firstName).encode('latin-1')} and I received this information:
3
2185
by: ravan1234 | last post by:
Hello Guys, I am having some problem with Unicode string in KATAKANA format. I am decoding string and then storing in db (SYbase). Also while retrieving data I am encoding and then displaying. Source Encoding is jp_shift Target Encoding is 932. But the data get stores in ???? format.
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.