473,788 Members | 2,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

incorrect encoding after serialisation to XML

Using the code below I am trying, in VB .Net 2003, to serialise classes
defined in a couple of XSD documents. The encoding for both is
Unicode(UTF-8). However the resulting XML is encoded as UTF-16. This is
causing me problems when I try to load it into an XPath document. I would
imagine I should be able to use System.Text.Enc oding to define the encoding
as UTF-8 but I haven't been able to figure out how so far.

Dim ser As New XmlSerializer(G etType(GTPAPP))
Dim sw As New StringWriter
ser.Serialize(s w, domainObj.getGT PApp)
Return sw.ToString()

any help would be appreciated.
Nov 12 '05 #1
4 5736
one additional point on this is that when I try to load the XML when encoded
as UTF-16 I get the error
"There is no Unicode byte order mark. Cannot switch to Unicode."

"Stephen" wrote:
Using the code below I am trying, in VB .Net 2003, to serialise classes
defined in a couple of XSD documents. The encoding for both is
Unicode(UTF-8). However the resulting XML is encoded as UTF-16. This is
causing me problems when I try to load it into an XPath document. I would
imagine I should be able to use System.Text.Enc oding to define the encoding
as UTF-8 but I haven't been able to figure out how so far.

Dim ser As New XmlSerializer(G etType(GTPAPP))
Dim sw As New StringWriter
ser.Serialize(s w, domainObj.getGT PApp)
Return sw.ToString()

any help would be appreciated.

Nov 12 '05 #2
"Stephen" <St*****@discus sions.microsoft .com> wrote in message news:8C******** *************** ***********@mic rosoft.com...
"Stephen" wrote:
Using the code below I am trying, in VB .Net 2003, to serialise classes
defined in a couple of XSD documents. The encoding for both is
Unicode(UTF-8).
The encoding of the schema documents is irrelevant, and the
encoding of the classes -- huh?

Once the Framework loads a piece of XML, that XML becomes
a node set. Metaphysically, think of it as astral projection (an
out-of-body experience) for the XML ... it goes to a higher plane
of existence where encodings no longer matter (whether attributes
are delimited by single- or double- quotes no longer matters,
whether content came from a CDATA section no longer matters,
etc.)

The challenges you're facing seem to center on entering and
leaving 'the Body' (think of the XML as being corporeal
whenever you see angle brackets).
However the resulting XML is encoded as UTF-16. : : Dim sw As New StringWriter
ser.Serialize(s w, domainObj.getGT PApp)
Return sw.ToString()

A String is always UTF-16 encoded. There is no such thing as a UTF-8
string. It's a myth. It's fiction. UTF-8 strings went out with the dragon,
leprechauns, three-headed dogs guarding the Underworld, and Java.
one additional point on this is that when I try to load the XML when encoded
as UTF-16 I get the error
"There is no Unicode byte order mark. Cannot switch to Unicode."


This depends on how the XML was saved. Was it saved with a Stream
that used System.Text.Uni codeEncoding?

You'll get this XmlException when the XML isn't encoded in the encoding
it says it is.

My advice is not to write the XML to a String. If you want to put it into
a file (and have control over it's encoding), use an XmlTextWriter and
wrap it around a Stream. Then read it back in with a TextWriter that
is wrapped around a Stream, with a matching encoding (and any
encoding declaration that appears in the XML declaration should
match both the encoding you used to serialize out and deserialize
in.)

- - - utf8xml.vb (excerpt)
' . . .
Dim tw As New System.Xml.XmlT extWriter( _
New System.IO.FileS tream( "file.xml", System.IO.FileM ode.Create), _
New System.Text.UTF 8Encoding( True) )

' XML will be serialized to file.xml, in UTF-8, with BOM.
ser.Serialize( tw, domainObj.getGT PApp)

' Finish writing the file and close it.
tw.Flush( )
tw.Close( )

Dim tr As New System.IO.TextR eader( _
New System.IO.FileS tream( "file.xml", System.IO.FileM ode.Open), _
New System.Text.UTF 8Encoding( True) )

' XML will be deserialized from file.xml, in UTF-8, with BOM.
domainObj.setGT PApp( CType( _
ser.Deserialize ( tr), GTPApp) )

tr.Close( )
' . . .
- - -
Derek Harmon
Nov 12 '05 #3
Derek
I have a further question on this query. What I am actually trying to do is
generate XML that can be used as the input XML for a call to a second
application. So rather then generating a file I need to generate the XML to
be used in code. Would you know how to do this?

"Derek Harmon" wrote:
"Stephen" <St*****@discus sions.microsoft .com> wrote in message news:8C******** *************** ***********@mic rosoft.com...
"Stephen" wrote:
Using the code below I am trying, in VB .Net 2003, to serialise classes
defined in a couple of XSD documents. The encoding for both is
Unicode(UTF-8).
The encoding of the schema documents is irrelevant, and the
encoding of the classes -- huh?

Once the Framework loads a piece of XML, that XML becomes
a node set. Metaphysically, think of it as astral projection (an
out-of-body experience) for the XML ... it goes to a higher plane
of existence where encodings no longer matter (whether attributes
are delimited by single- or double- quotes no longer matters,
whether content came from a CDATA section no longer matters,
etc.)

The challenges you're facing seem to center on entering and
leaving 'the Body' (think of the XML as being corporeal
whenever you see angle brackets).
However the resulting XML is encoded as UTF-16. : : Dim sw As New StringWriter
ser.Serialize(s w, domainObj.getGT PApp)
Return sw.ToString()


A String is always UTF-16 encoded. There is no such thing as a UTF-8
string. It's a myth. It's fiction. UTF-8 strings went out with the dragon,
leprechauns, three-headed dogs guarding the Underworld, and Java.
one additional point on this is that when I try to load the XML when encoded
as UTF-16 I get the error
"There is no Unicode byte order mark. Cannot switch to Unicode."


This depends on how the XML was saved. Was it saved with a Stream
that used System.Text.Uni codeEncoding?

You'll get this XmlException when the XML isn't encoded in the encoding
it says it is.

My advice is not to write the XML to a String. If you want to put it into
a file (and have control over it's encoding), use an XmlTextWriter and
wrap it around a Stream. Then read it back in with a TextWriter that
is wrapped around a Stream, with a matching encoding (and any
encoding declaration that appears in the XML declaration should
match both the encoding you used to serialize out and deserialize
in.)

- - - utf8xml.vb (excerpt)
' . . .
Dim tw As New System.Xml.XmlT extWriter( _
New System.IO.FileS tream( "file.xml", System.IO.FileM ode.Create), _
New System.Text.UTF 8Encoding( True) )

' XML will be serialized to file.xml, in UTF-8, with BOM.
ser.Serialize( tw, domainObj.getGT PApp)

' Finish writing the file and close it.
tw.Flush( )
tw.Close( )

Dim tr As New System.IO.TextR eader( _
New System.IO.FileS tream( "file.xml", System.IO.FileM ode.Open), _
New System.Text.UTF 8Encoding( True) )

' XML will be deserialized from file.xml, in UTF-8, with BOM.
domainObj.setGT PApp( CType( _
ser.Deserialize ( tr), GTPApp) )

tr.Close( )
' . . .
- - -
Derek Harmon

Nov 12 '05 #4
Stephen,

Certainly, no problem. In the code below, simply replace references to
FileStream with references to MemoryStream. This will yield a Byte( )
array that you can pass along in binary form to this other application.
They're Bytes and not Chars, so the Byte( ) can be UTF-8 encoded.

As far as

The XmlTextWriter declarations below become.
Dim tw As New System.Xml.XmlT extWriter( _
New System.IO.FileS tream( "file.xml", System.IO.FileM ode.Create), _
New System.Text.UTF 8Encoding( True) )
Dim stream As New System.IO.Memor yStream( )
Dim tw As New System.Xml.XmlT extWriter( _
stream, New System.Text.UTF 8Encoding( True) )

Again, this produces the Byte Order Mark because I'm creating the
UTF8Encoding with True. When you're sending a binary stream, you
might not want a BOM (or at least the consumer might not want the
BOM). You may want to try it both ways, with- and w/o BOM.

After you are done Serializing to the tw, just like in the code example
below, you would extract the Byte( ) from the MemoryStream.

Dim buffer As Byte( ) = stream.GetBuffe r( )

and you can pass this forward to another application (i.e., through
a Socket) or call a method,

Me.OtherMethod( buffer)

' . . .

Public Sub OtherMethod( ByVal incomingXml As Byte( ) )
' OtherMethod proceeds to wrap incomingXml in a
' MemoryStream and deserialize it using an XmlReader
' . . .
End Sub
The deserialization works much the same as the translation of the serialization
I've shown above. Polymorphism is what makes this possible. To the Xml-
Reader and Writer classes it doesn't matter what variety of System.IO.Strea m
you use (MemoryStream, FileStream, NetworkStream), only that it is a Stream.
Derek Harmon
"Stephen" <St*****@discus sions.microsoft .com> wrote in message news:0A******** *************** ***********@mic rosoft.com...
Derek
I have a further question on this query. What I am actually trying to do is
generate XML that can be used as the input XML for a call to a second
application. So rather then generating a file I need to generate the XML to
be used in code. Would you know how to do this?

"Derek Harmon" wrote:

: : - - - utf8xml.vb (excerpt)
' . . .
Dim tw As New System.Xml.XmlT extWriter( _
New System.IO.FileS tream( "file.xml", System.IO.FileM ode.Create), _
New System.Text.UTF 8Encoding( True) )

' XML will be serialized to file.xml, in UTF-8, with BOM.
ser.Serialize( tw, domainObj.getGT PApp)

' Finish writing the file and close it.
tw.Flush( )
tw.Close( )

Dim tr As New System.IO.TextR eader( _
New System.IO.FileS tream( "file.xml", System.IO.FileM ode.Open), _
New System.Text.UTF 8Encoding( True) )

' XML will be deserialized from file.xml, in UTF-8, with BOM.
domainObj.setGT PApp( CType( _
ser.Deserialize ( tr), GTPApp) )

tr.Close( )
' . . .
- - -

Nov 12 '05 #5

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

Similar topics

2
5409
by: Gustaf Liljegren | last post by:
I'm using xml.sax.parseString to read an XML file. The XML file contains a few words in Russian, and is encoded in UTF-8 using C#. In the example below, MyParser() is my SAX ContentHandler class. My first try was: f = open('words.xml', 'r') s = f.read() xml.sax.parseString(s, MyParser()) This produced the following error:
1
3931
by: murphy | last post by:
Hi, I've been seeing two symptoms with my asp.net site that have started recently after a long period of smooth running. As others on our team make changes to referenced dll's I find that I get the following errors from time to time. Apparently the following procedure alleviates the problems: -Reboot (clears locks on following directory)
3
3350
by: murphy | last post by:
Hi, I've been seeing two symptoms with my asp.net site that have started recently after a long period of smooth running. As others on our team make changes to referenced dll's I find that I get the following errors from time to time. Apparently the following procedure alleviates the problems:
4
2760
by: Peter Ritchie | last post by:
Does anyone know how to suppress a specific warning for a line or block of code in C#? C++ has a nice facility to disable a warning for a block of code with #pragma for warnings that are incorrect or don't apply. For example, the following code generates an CS0628 because CS0628 makes an incorrect assumption that "protected" applies only to inheritance: public sealed class Class { EmbeddedClass utility = new EmbeddedClass();
2
1337
by: Greg | last post by:
I have a bizarre situation in which serialisation is failing routinely under a specific condition, and I'm wondering if the details ring a bell with anyone here. I have 2 classes that my application serialises before closing down. One of these classes contains a string field which contains a path to a file. This is populated from the contents of a text box, which in turn is populated using an OpenFileFialog object. Under normal usage,...
2
2599
by: ashwinij | last post by:
Hello The steps which i am doing in my program 1) I am having an xml file. 2) I am performing some updations in the file using XQueryUtil class from nux package. 3)After that i am performing Serialisation ( nu.xom.Serializer ) and storing in a file. 4)This Serialised file is being appended in a separate file. 5) Steps 3) and 4) are performed for som n no. of times The problem is whenever i serialised i get this line "<?xml...
1
2521
by: Sarika Agarwal | last post by:
Hi, What is the primary difference between serialization and encoding in ..NET! *** Sent via Developersdex http://www.developersdex.com ***
1
2483
by: OrionLee | last post by:
I am using C# to work with a 3rd party DLL (Nevron Charts), and attempting to serialise it. The serialisation itself is handled somewhere inside the DLL, so to get it to happen you call the Nevron's serialiser and then SaveToStream() which will serialises the chart object into a stream for you, which is all well and good... Now for the problem: If I create a standalone application and use the serialiser to serialise charts it all works fine....
5
4071
by: julvr | last post by:
Is the encoding for float platform independent? That is, if I take the four bytes, stick them into an ethernet packet send them to another machine, then (after endian swapping and alignment), set a float pointer to point at those four bytes, will I get the same value? It seems to work on the plaforms I have, but this is suppose to work for any machine types -- so I guess my question is, is the encoding of float standardized? Thanks
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
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
10110
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
9967
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
7517
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
6750
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();...
1
4069
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
3670
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.