how to serialize object into specific encoding?

Posted by Poster under C# on 7/20/2009 | Views : 24666 | Status : [Member] | Replies : 5
I have an object and I am trying to serialize that object into ISO-8859-1 encoding but it is getting serialized into utf-16 encoding format even if specifying the encoding type in XmlWriterSettings object and passing it into XmlWriter.Create method.

My serialize function looks like

private string serializeMe(object sourceObject)
{
XmlSerializer serializer = new XmlSerializer(sourceObject.GetType());
StringBuilder sb = new StringBuilder();

XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.GetEncoding("ISO-8859-1");
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(sb, settings);
try
{
serializer.Serialize(writer, sourceObject);
}
finally
{
writer.Close();
}
return sb.ToString();
}

Please help.




Responses

Posted by: Elizas on: 2/2/2010 [Member] Starter

Up
0
Down
If we want to serialize and deSerialize an object in silverlight We have to use DataContractSerializer as Silverlight doesn't support BinaryFormatter.
DataContractSerializer under the type System.Runtime.Serialization Serializes and deserializes an instance of a type into an XML stream using dataContract.

Create a Silverlight-enabled wcf service and define the data contract and Datamember attributes withing the .svc.
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.Runtime.Serialization
<ServiceContract(Namespace:="")> _
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class EmployeeService
<OperationContract()> _
Public Sub DoWork()
' Add your operation implementation here
End Sub
' Add more operations here and mark them with <OperationContract()>

End Class

<DataContract()> _
Public Class Person
Private UserNameStr As String
Private PasswordStr As String
<DataMember()> _
Public Property UserName() As String
Get
Return UserNameStr
End Get
Set(ByVal value As String)
UserNameStr = value
End Set
End Property
<DataMember()> _
Public Property Password() As String
Get
Return PasswordStr
End Get
Set(ByVal value As String)
PasswordStr = value
End Set
End Property
End Class
By referencing the service in the Silverlight application we can serialize and deSerialize an object in silverlight
Below is the code for Serializing the object in the .xaml page.

Imports System.IO
Imports System.IO.IsolatedStorage
Imports System.ServiceModel
Imports System.Runtime.Serialization

We will use IsolatedStorageFile.GetUserStoreForApplication() function get the isolated storage for the application and Serialize the objects to Isolated storage

Private IsolateStorageFileObj As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
Dim PersonObj As New Person
PersonObj.UserName = "Soumyap"
PersonObj.Password = "Mindfire"
Dim fs As New IsolatedStorageFileStream("Person.dat", FileMode.Create, IsolateStorageFileObj)
Dim serializer As New DataContractSerializer(GetType(Person))
serializer.WriteObject(fs, PersonObj)
fs.Close()

Similarly We can deSerialize the object from Isolated storage.

Dim PersonObj As New Person
Dim isfstream As New IsolatedStorageFileStream("Person.dat", FileMode.Open, IsolateStorageFileObj)
Dim serializer As New DataContractSerializer(GetType(Person))
PersonObj = DirectCast(serializer.ReadObject(isfstream), Person)
I hope it did solve your problem
thanks
Eliza
http://bit.ly/ak3gmk

Poster, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raja on: 7/20/2009 [Member] Starter

Up
0
Down
Work with following function and replace your desired encoding format at proper line.


private static string SerializeObjectToXmlString<T>(T obj)

{
XmlSerializer xmls = new XmlSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
settings.IndentChars = "\t";
settings.NewLineChars = Environment.NewLine;
settings.ConformanceLevel = ConformanceLevel.Document;

using (XmlWriter writer = XmlTextWriter.Create(ms, settings))
{
xmls.Serialize(writer, obj);
}

string xml = Encoding.UTF8.GetString(ms.ToArray());
return xml;
}
}


Reference: http://stackoverflow.com/questions/660440/c-xml-serialization-leading-question-marks

Regards,
Raja, USA

Poster, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Webmaster on: 8/26/2009 [Administrator] HonoraryPlatinum

Up
0
Down
Thanks for the exact solution Raja. Appreciate your contribution.

Thanks
Webmaster

Best regards,
Webmaster
http://www.dotnetfunda.com

Poster, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Webmaster on: 8/26/2009 [Administrator] HonoraryPlatinum

Up
0
Down
Keep contributing. Thanks

Best regards,
Webmaster
http://www.dotnetfunda.com

Poster, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raja on: 8/26/2009 [Member] Starter

Up
0
Down
Thanks Webmaster.

Regards.

Regards,
Raja, USA

Poster, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response