Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DoctorWhy · Sep 28, 2012 at 08:42 PM · serializationstringxml

Serialize XML to a string

I am trying to serialize a class into XML that can be stored to a string. I need this to be compatible with iOS, web browser, and flash, which means it can't use reflection to serialize. How can I do this? I need it to be put into a string because I will be sending it to a server, instead of saving anything locally (since the web browser doesn't allow for this).

Thanks for the help!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by whydoidoit · Sep 28, 2012 at 09:10 PM

Firstly you are quite able to use reflection on iOS and web so long as on the web you don't do much fiddling with private fields.

You can use BinaryFormatter and Convert to create a string faster and smaller than XML.

Like this to create a compressed string:

   var m = new MemoryStream();
   var b = new BinaryFormatter();
   b.Serialize(m, yourObject);
   var s = Convert.ToBase64String(m.GetBuffer());

Like this to decompress it:

   var m = new MemoryStream(Convert.FromBase64String(dataYouReceived));
   var b = new BinaryFormatter();
   var o = (YourObject)b.Deserialize(m);

You'll need System.IO and System.Runtime.Serialization.Binary

Comment
Add comment · Show 22 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DoctorWhy · Sep 28, 2012 at 09:30 PM 0
Share

Awesome! That worked (after fixing a small error in your provided code, suppose to be m.GetBuffer(), and the Binary is in System.Runtime.Serialization.Formatters.Binary).

Quick question though... the string that is created from Convert.ToBase64String(b.GetBuffer()) isn't X$$anonymous$$L, but a compressed string?

avatar image whydoidoit · Sep 28, 2012 at 09:32 PM 0
Share

Well BinaryFormatter makes a compressed byte array - Convert.ToBase64String() converts it to a string that can be sent via http.

avatar image DoctorWhy · Sep 28, 2012 at 09:42 PM 0
Share

This will work perfectly then, thank you!

avatar image DoctorWhy · Sep 28, 2012 at 09:44 PM 0
Share

(Oh, and by iOS, I meant iPad, which according to this http://answers.unity3d.com/questions/290690/why-is-my-ipad-crashing-when-i-try-to-user-xmlseri.html doesn't work)

avatar image whydoidoit · Sep 28, 2012 at 09:48 PM 0
Share

Nah that's a load of rubbish on the reflection side. I know binary formatter works on IOS - I use it - and I also have a reflection based serializer Unity Serializer that makes use of a lot of reflection and works fine on everything.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C#-XMLSerialize a Struct with Vector3 Array in it 2 Answers

Mapping between classes for serialization 2 Answers

How to ignore EventHandler serialization? 1 Answer

Using BinaryFormatter for deserialization - ArgumentException: method argument length mismatch 0 Answers

whats the best way to save a large list of variables? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges