HowTo: Serialize to a String without extra characters

I have a class that I want to serialize to an XML string.  I want the XML to serialize to utf-8 encoding.  When I serialize to an XML file, the data looks great.  When I try to serialize to a String (ala StringBuilder) I get utf-16 and instead of the parenthesis (") I get  a slash and then a " (\") which makes sense when looking at a character in memory, but not in a string

Here is my code

XmlSerializer  serializer = new XmlSerializer (myObject.GetType ())
StringBuilder  builder = new StringBuilder ()
StringWriter   stringWriter = new StringWriter (builder)
XmlTextWriter  xmlWriter = new XmlTextWriter (stringWriter)
xmlWriter.Formatting = Formatting.Indented

// Serialize the document to the XML write
serializer.Serialize (xmlWriter, message)

return  builder.ToString ()

If I try to write to a memory stream and then convert the byte array to a string via the binary reader, I get exceptions because for some reason there are garbage characters written to the front of the byte array that are not ASCII/Unicode characters

Any help would be great!  Thanks

Brian
0
anonymous (74722)
3/3/2004 10:01:08 PM
dotnet.xml 7266 articles. 0 followers. Follow

5 Replies
716 Views

Similar Articles

[PageSpeed] 56

Let me restate my problem

I am trying to serialize a class into a string in memory that is encoded with utf-8 encoding.  Using the StringBuilder, StringWriter and XmlSerializer, I can only ever serialize to utf-16, which if persisted to a file cannot be parsed by XML viewers such as IE and also the utf-16 string cannot be processed by SQL Server.  Is there a way to force these classes to serialize to utf-8 encoding

I have tried serializing to a MemoryStream, but I am getting 3 strange characters at the front of the stream.  I am creating an empty MemoryStream and passing it to an XmlTextWriter as the stream behind it.  When I serialize I get the complete document, but with the three mysterious characters on the front that prevent me from reading the stream as a string

Any serialization help would be great.  Please forgive the rambling in my previous posting

Brian
0
anonymous (74722)
3/4/2004 1:06:07 AM
"Brian Reed" <anonymous@discussions.microsoft.com> wrote in message news:3BE936DD-780D-4A43-9952-E73F9533D552@microsoft.com...
> I am trying to serialize a class into a string in memory that is encoded with utf-8 encoding.
> Using the StringBuilder, StringWriter and XmlSerializer, I can only ever serialize to utf-16,
: :
> Is there a way to force these classes to serialize to utf-8 encoding?

No.  UTF-16 is the internal representation of System.String.  If you require a different
encoding, you must store it in something other than System.String (MemoryStream or
Byte[] are what commonly come to mind).

> I have tried serializing to a MemoryStream, but I am getting 3 strange characters at the
> front of the stream.

That's probably the UTF-8 Byte Order Mark (BOM), although AFAIK the BOM
only introduces a pair of "mysterious" characters, not a trio.  On the bright side,
it suggests that you really have UTF-8 encoding there.  ;-)

The BOM exists because UTF-8 characters can be encoded as little-endian
or Big-Endian.  Its possible to suppress the BOM, but if you do, realize that your
UTF-8 might be interpreted as containing gibberish when read on Big-Endian
machines (although in that case, its reasonable for smart receivers to infer the
erroneous data is due to its having the incorrect byte ordering, and ideally an
interpretation using the opposite byte ordering could be attempted.)

Look for the constructor where you create the System.Text.UTF8Encoding in
your code.  If you pass true as the first argument (shouldEmitUtf8Bom) to this
constructor, try changing it to false instead.  This should remove any BOM
from the output.


Derek Harmon


0
loresayer (274)
3/4/2004 3:14:41 AM
Derek Harmon wrote:

> That's probably the UTF-8 Byte Order Mark (BOM), although AFAIK the BOM
> only introduces a pair of "mysterious" characters, not a trio.  On the bright side,
> it suggests that you really have UTF-8 encoding there.  ;-)

BOM length depends on encoding - in UTF-8 it's 3 bytes, while in UTF-16 
it's 2 bytes. See 
http://www.w3.org/TR/2000/REC-xml-20001006.html#sec-guessing-no-ext-info

-- 
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
0
Oleg
3/4/2004 10:18:48 AM
Hi Derek,

> That's probably the UTF-8 Byte Order Mark (BOM), although AFAIK the BOM
> only introduces a pair of "mysterious" characters, not a trio.  On the bright side,
> it suggests that you really have UTF-8 encoding there.  ;-)
> 
> The BOM exists because UTF-8 characters can be encoded as little-endian
> or Big-Endian.  Its possible to suppress the BOM,

<-- snip -->

can you tell me how to suppress the BOM?

Thanx,
Timo
0
the (1)
3/4/2004 1:23:52 PM
Timo,
Have you tried the constructors for System.Text.UTF8Encoding that accept a
boolean parameter which specifies whether to prefix or not prefix an
encoding with a Unicode byte order mark?

Hope this helps
Jay

"Timo Henne" <the@startext.de> wrote in message
news:2f555901.0403040523.2b787caf@posting.google.com...
> Hi Derek,
>
> > That's probably the UTF-8 Byte Order Mark (BOM), although AFAIK the BOM
> > only introduces a pair of "mysterious" characters, not a trio.  On the
bright side,
> > it suggests that you really have UTF-8 encoding there.  ;-)
> >
> > The BOM exists because UTF-8 characters can be encoded as little-endian
> > or Big-Endian.  Its possible to suppress the BOM,
>
> <-- snip -->
>
> can you tell me how to suppress the BOM?
>
> Thanx,
> Timo


0
3/4/2004 2:09:34 PM
Reply:

Similar Artilces:

String search on two tables
Hello, I have two tables where I want find the best match according to the description on both tables. The result will be the corresponding code in the second table. Example: first table code description 101 agri 102 single family 103 mobile home 104 vacant land 105 recr Second table Code Description 1000 Agricultural 1002 Single Family Residence 1003 Recreational 1004 Vacant When both tables are matched, the macro should look for the best fit in table 2 and returns the code. ...

How do I print without borders when creating a brochure
When I creat a brochure in Publisher there is a border around it. How do I eliminate it so that the pictures print to the edge? Jomo wrote: > When I creat a brochure in Publisher there is a border around it. How > do I eliminate it so that the pictures print to the edge? ==================================== Do you have a printer that can print to the edge? The following article may be worth a look: "How to find the maximum print area of your printer" http://ed.mvps.org/Static.aspx?=Publisher/horidiag -- John Inzer MS-MVP Digital Media Experience Notice ...

Chinese characters in English subject
I have a user who sends internet e-mail in English and sometimes her subject line changes to Chinese characters. When I check the Options I get the following: MIME-Version: 1.0 Content-Type: application/ms-tnef; name="winmail.dat" Content-Transfer-Encoding: binary Subject: =?iso-2022-jp?B?Rlc6IBskQkB1XEAbKEJs?= I had her foward me the message from her Sent Mail and again checked the options and get the following: Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: application/ms-tnef; name="winmail.dat" Content-Transfer-Encoding: binary Subj...

Howto create textbox like in powerpoint
Hi Could someone tell me how to create powerpoint type textboxes? Are they windowless rich edit controls? Thanks Sachin They are rich edit control, but they have a window, they are owned by the window that they are being displayed on. AliR. "sachinkundu" <sachinkundu@gmail.com> wrote in message news:1170062520.486641.43880@m58g2000cwm.googlegroups.com... > Hi > > Could someone tell me how to create powerpoint type textboxes? Are > they windowless rich edit controls? > > Thanks > Sachin > > They are rich edit control, but they have a wind...

parsing query strings in XSL?
I'm using XSLT to transoform XML into navigation. My XML has a field for the URL, and a field for a variable that sets the 'active node' in the menu when it displays. The catch is that I now repeat data on some nodes: <url>link.aspx?activeLink=8.2.1</url> <linkID>8.2.1</url> So, if we change one, we need to remember to change the other. I'd like to streamline this and append the linkID to the URL when I write it out/transform it in XSL: <a href="{linkURL}?activeLink={linkID}"> This will work just fine provided I DON&#...

MessageBox without buttons
How do I put up a "MessageBox" like message to inform the user that I am looking for data and than close it when I return without any actions on the part of the user? Thanks in advance, this is a great group! Ron H. x-- 100 Proof News - http://www.100ProofNews.com x-- 3,500+ Binary NewsGroups, and over 90,000 other groups x-- Access to over 1 Terabyte per Day - $8.95/Month x-- UNLIMITED DOWNLOAD You will need to have a separate worker thread in order to achieve this. UI thread will show the dialog and worker thread will do the work. There are alternate approaches. Ta...

What's wrong with this code? Serialization
Dim strSQL As String = "SELECT lastname, firstname, title, hiredate " & _ "FROM Employees " If Len(lastname) > 0 Then strSQL += "WHERE lastname = '" & lastname & "' " End If strSQL += "FOR XML AUTO" Dim objConn As SqlConnection = New SqlConnection("Data Source=PASWEB22D;Initial Catalog=iClas;uid=sa;password=emitsadmin;") objConn.Open() Dim objCmd As SqlCommand = New SqlCommand(strSQL, objConn) Dim serializer As XmlSerializer Dim reader As XmlTextReader reader = objCmd.ExecuteXmlReader Dim emp As New ...

Smtp Authentication
Hi all I'm an IBM Domino administrator.But i have to fix an exchange 2003 server in these days.The problem is; the exchange server was full of spam yesterday because of open relay, i deleted all the spam.Now i want to make an smtp authentication setting on the exchange server. the schema is as that: 1 MS 2003 server (domain controller) with exchange 2003 installed (all fixpacks are included) (192.168.0.1).15 clients (winXP+Outlook 2000/XP/2003 mixed). network adress is 192.168.0.0/24 all the computers access to internet by a dsl router (192.168.0.254), using 192.168.0.1 as DNS server...

Setting up e-mail without Exchange Server
Need some help here.... I want everytime a lead or an oppurtunity is created, an e-mail to be sent to a user... The problem is that i am not using an exchange e-mail server but an the e-mail server of my isp... I have created a rule in the Process Manager but where should i state the e-mail's server address? My best Regards Chris Hi Chris, You will be using SMTP to send mails out from the CRM server. You can configure the SMTP settings during the installation of the server. If you have already installed the server and want to specify the SMTP settings now, refer to http://sup...

Serializing object to XmlDocument (or string)
I am on .NET 2.0 and am trying to serialize an object to an XmlDocument (or a string which I can then create an XmlDocument from). What I have so far is (User is my class); XmlSerializer x = new XmlSerializer(typeof(User)); MemoryStream memStream = new MemoryStream(); How can I get this to a string or an XmlDocument? The serialization works fine as if I serialize to a text file (using StreamWriter), everything looks fine. I've try converting to bytes, reading on XmlWriter, but nothing seems to work. Thanks, ...

SEND A STRING TO THE PRINTER
I have a string variable that is presently being sent to a MessageBox. I want to add a command button to the Form so I can send the string variable directly to the printer. Is there a simple way to do this. Thanks The only simple way is to create a report and run it. -- Dave Hargis, Microsoft Access MVP "RICHARD BROMBERG" wrote: > I have a string variable that is presently being sent to a MessageBox. > > I want to add a command button to the Form so I can send the string variable > directly to the printer. > > Is there a simple way to do this. > &g...

Replacing "OR" with a character separator
When I am using a form to view data and use the Filter by Selection option I would like to use a character separator between the multiple values rather than typing the word OR between: Rather than type "Bob OR Alice OR Susan" I would rather type "Bob, Alice, Susan" or some similar character Anyone know how to do this? IN("Bob", "Alice", "Susan") -- Dave Hargis, Microsoft Access MVP "RandallMJ" wrote: > When I am using a form to view data and use the Filter by Selection option I > would like to use a character separator...

? Tutorial on HOOKS without DLL
Hi, Can anyone point me towards a tutorial on using hooks within a program (without using a dll)? I need a good clear example. Thanks. -- Alec S. alec <@> synetech <.> cjb <.> net Note that global hooks *must* reside in a DLL. This is because these events can occur in other processes and the "hooking" DLL must be mapped into the address space of each process that can cause the event. -- Jonathan Wood SoftCircuits http://www.softcircuits.com Available for consulting: http://www.softcircuits.com/jwood/resume.htm "Alec S." <a@a.com&...

special characters
I have a memo field in table A that contains " (special character). I need to update table B with the memo field from table A. However, Access compalins that the string used to update table B is not formatted properly because the memo field in table A contains " characters. How do I enclose the memo field in an update statment to accommodate that? sqlstring = "UPDATE [table_A] " _ & " SET [comments] = " & Chr(34) & table_B!Comments & Chr(34) DoCmd.RunSQL sqlstring thanks in advance for your help! If the comm...

Serial emails
Hello, is there a possibility to send serial emails out of MS CRM with attachment or are there any suitable add-ons for it? Thanks ...

A string problem
I have a form on which is a subform and 5 commands buttons. On the click of a button, I want to change the records shown on the subform. I am having a problem with one of those buttons. One is suppose to show records of costumers that came between 2 dates. To do so I made a table with 3 fields :ID, date_1, date_2. date_1 and date_2 are date/time variables. I also made a small form that changes those dates and with an OK button. The command button opens the small date subform. On the click of the OK button on the small subform I go into this procedure : ' A déf...

HOWTO Force landscape printing with CPrintDialog.
HOWTO Force landscape printing with CPrintDialog. Please help, I have the following code I want to FORCE to print in landscape after the CPrintDialog finishes. This solution must not touch the CWinApp's "m_hDevMode" data member. This solution must only do something to the DC returned from the CPrintDialog, and it must override any possible orientation settings the user may have change while they were inside CPrintDialog. : CPrintDialog CPrintDialog_Temp(FALSE); if (CPrintDialog_Temp.DoModal() == IDOK) { // Do something here to FORCE the print out to be landscape ...

std:string problem (string subscript out of range)
Hi. This code gives me an "string subscript out of range" error. int contor=0; for(int i=0;i<6;i++) for(int j=0;j<8;j++){ string siruri[6]; char ch,k=0; while(k<6){ ...................................................ch=afterRLE[contor+ +];............................................HERE!!!!!, THIS LINE if(ch==-128){ k++; } else siruri[k]+=ch; } where afterRLE is a std:string. Is memory data in the string continuous? Does it matter? Where could be the problem? In addition I say that my application is compressing/decompressing/ rendering v...

First 6 characters in column
I have a data that I'm importing into excel that looks similar to: 123456 012. What I want to do is on this whole column trim everything away and leave only the first 6 characters. Is this something fairly simple? Thanks --- Message posted from http://www.ExcelForum.com/ You could use the Text to Columns feature: Select the cells that contain the numbers. Choose Data>Text to Columns Select Fixed Width, click Next In the Data Preview window, click after the sixth character, to add a break line. Click Next With the first column selected, choose 'Do not import column ...

Send characters to combobox
Hi, I have a non-updateable datasheet subform. I am wanting to catch the subform keypress and direct these to a combobox on the parent as if the keypress was to the combobox directly. I don't want to use sendkeys as all comments caution its use, and the combobox may not have the focus anyway (I could ensure it does). Using the following in the subform: Private Sub Form_KeyPress(KeyAscii As Integer) If (KeyAscii > 64 And KeyAscii < 91) Or (KeyAscii > 96 And KeyAscii < 123) Then 'Alpha key so send to parent combo Me.Parent.cboFiller = Nz(Me.Parent.cboFil...

CString
Hello: I have been working on very large cstrings - maximum length goes upto around 750000. I am working on a 1 GB RAM machine and i have the following problem in a portion of the code. CString abc; while(blah blah) { abc += xyz; } At some point (though it is different everytime i run, around length of 500000 + ), the concatination statement crashes (consistently as unhandled exception) and the call stack leads me to the allocbuffer statement in cstring (where in turn it seems to fail on the new handler) My questions: 1) Viewing the task manager, I can say that the full RAM is not ut...

How do I compare item text of combobox to one string
Hello all experts I have used combobox I ms Access like that Combobox.rowsource="xxxx" combobox.bound Column=2 Limit to List=yes all above it working but I can not get item text of this combobox,so How can I do can you healp me. Thank combobox.value "cambo" wrote: > Hello all experts > I have used combobox I ms Access like that > Combobox.rowsource="xxxx" > combobox.bound Column=2 > Limit to List=yes > all above it working but > I can not get item text of this combobox,so How can I do > can you healp me. > Thank > > > ...

Why does Autoexpand in combo box not work with accented characters?
To quote the Access online help: You can use the AutoExpand property to specify whether Microsoft Access automatically fills the text box portion of a combo box with a value from the combo box list that matches the characters you enter as you type in the combo box. This lets you quickly enter an existing value in a combo box without displaying the list box portion of the combo box. This works fine when I use the normal English alphabet, but fails if I type an accented character as the first character of a name in a foreign language such as Hungarian. Is there any way to make AutoExpand ...

DLL Howto
Hello everyone, I would like to fill in people who have not made MFC dlls my experiences making them, and how to make DLL creation extremely simple. First you want to create a MFC extension dll, which is important because this specific type of DLL will allow you to export functions and classes without any confusing code. After that, implement any classes to the dll and when the class is being declared include the AFX_EXT_CLASS macro. For instance class CModifyTransaction : public CDialog would become... class AFX_EXT_CLASS CModifyTransaction : public CDialog after that is added any dll ...

HOWTO: Protecting a Document???
Hi... I'm still using Office XP (Word 2002). I'm just about finished making a FORM document which I plan to turn into a template (.DOT). I've turned on the "Forms Protect"... which moves the user around the "protected text" but I also want to disable anyone from changing the original .DOT itself (this will be a standard form and don't need some smart-ass altering something that I'm, ultimately, responsible for). I'm still GOOGLE'g but... how can I allow a user to OPEN this file to enter data/check boxes into the FORM FIELDS......