- Home /
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
Your answer
Follow this Question
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