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 mmangual_83 · Dec 03, 2013 at 07:46 PM · c#serializationxmlfile-io

Making my XML file multiline

I have a class that writes into an xml file and everything works fine, except, when I tell it to write into an xml file it saves everything in a single line and I want to make it readable for the rest of us. Does anyone have any idea on how to accomplish this? Here are the methods I am working with:

 private bool Load()
     {
         // Load our UserData into myData 
         LoadXML();
         if (_data.ToString() == "")
         {
             return false;
         }
         // notice how I use a reference to type (UserData) here, you need this 
         // so that the returned object is converted into the correct type
         var data = FixData();
         iData._users.Clear();
         var counter = 0;
         foreach (var dataToParse in data)
         {
             counter++;
             if (counter == 1) continue;
 
             if (string.IsNullOrEmpty(dataToParse.Trim())) continue;
             var uData = (UserData)DeserializeObject(dataToParse);
             iData._users.Add(uData._users[0]);
             var ud = iData._users[iData._users.Count - 1];
             // just a way to show that we loaded in ok
         }        
         return true;
     }
     string[] FixData()
     {
         var dataSplit = _data.Split(new string[] { "<mUsers>", "</mUsers>" }, System.StringSplitOptions.RemoveEmptyEntries);
         dataSplit[0] = dataSplit[0].Replace("<_users>", string.Empty);
         for (var indx = 1; indx < dataSplit.Length; indx++ )
         {
             if (string.IsNullOrEmpty(dataSplit[indx].Trim()) || !dataSplit[indx].Contains("x"))
             {
                 dataSplit[indx] = string.Empty;
                 continue;
             }
 
             dataSplit[indx] = dataSplit[0] + "<_users><mUsers>" + dataSplit[indx] + "</mUsers></_users></UserData>";
             Debug.Log(dataSplit[indx]);
         }
 
         return dataSplit;
     }
     
     /* The following metods came from the referenced URL */ 
     string UTF8ByteArrayToString(byte[] characters) 
     {      
         UTF8Encoding encoding = new UTF8Encoding(); 
         string constructedString = encoding.GetString(characters); 
         return (constructedString); 
     } 
     
     byte[] StringToUTF8ByteArray(string pXmlString) 
     { 
         UTF8Encoding encoding = new UTF8Encoding(); 
         byte[] byteArray = encoding.GetBytes(pXmlString); 
         return byteArray; 
     } 
     //// Here we serialize our UserData object of myData 
     string SerializeObject(UserData userData)
     {
         string XmlizedString = string.Empty;
         
         XmlSerializer xs = new XmlSerializer(typeof(UserData));
         MemoryStream memoryStream = new MemoryStream();
         XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
         xs.Serialize(xmlTextWriter, userData);
         memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
 
         XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
         //foreach (var user in l_userData)
         //{
             
         //}
         
         return XmlizedString;
     }
 
     // Here we deserialize it back into its original form 
     object DeserializeObject(string pXmlizedString) 
     { 
         XmlSerializer xs = new XmlSerializer(typeof(UserData)); 
         MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString)); 
         XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); 
         return xs.Deserialize(memoryStream); 
     } 
     
     // Finally our save and load methods for the file itself 
     void CreateXML() 
     { 
         StreamWriter writer; 
         FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName); 
         if(!t.Exists) 
         { 
             writer = t.CreateText(); 
         } 
         else 
         { 
             t.Delete(); 
             writer = t.CreateText(); 
         } 
         writer.Write(_data); 
         writer.Close(); 
         Debug.Log("File written."); 
     } 
     
     void LoadXML() 
     {
         StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName); 
         string _info = r.ReadToEnd(); 
         r.Close(); 
         _data=_info; 
         Debug.Log("File Read"); 
     } 
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

0 Replies

· Add your reply
  • Sort: 

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

16 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 avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

IsolatedStorageException: Could not find a part of the path 0 Answers

Best practice to store and load a specific set of objects? 3 Answers

Learning how to deserialize xml file 1 Answer

C# Saving Values to XML 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