C# xml serialization
-
I have this class User:
\[Serializable()\] \[XmlRoot("Users")\] public class Users { \[XmlArrayItem(typeof(User))\] public List UsersList { get; set; } } \[Serializable()\] public class User { \[System.Xml.Serialization.XmlAttribute("ID")\] public string ID { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Name")\] public string Name { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Username")\] public string Username { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Password")\] public string Password { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Email")\] public string Email { get; set; } = string.Empty; }
And this:
private void Serialization()
{
var path = @"D:\Projects\repo\xml\users.xml";
var newUsers = new Users();
newUsers.UsersList = new List();var user = new User (); user.Email = txtEmail.Text; user.Name = txtName.Text; user.Password = txtPass.Text; user.ID =txtID.Text; newUsers.UsersList.Add(user); XmlSerializer serializer = new XmlSerializer(typeof(Users)); using (TextWriter writer = new StreamWriter(path,true)) { serializer.Serialize(writer, newUsers); } }
And it saves this xml:
a a a s s ss
But I want to save the xml file in this format:
a a a s s
-
I have this class User:
\[Serializable()\] \[XmlRoot("Users")\] public class Users { \[XmlArrayItem(typeof(User))\] public List UsersList { get; set; } } \[Serializable()\] public class User { \[System.Xml.Serialization.XmlAttribute("ID")\] public string ID { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Name")\] public string Name { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Username")\] public string Username { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Password")\] public string Password { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Email")\] public string Email { get; set; } = string.Empty; }
And this:
private void Serialization()
{
var path = @"D:\Projects\repo\xml\users.xml";
var newUsers = new Users();
newUsers.UsersList = new List();var user = new User (); user.Email = txtEmail.Text; user.Name = txtName.Text; user.Password = txtPass.Text; user.ID =txtID.Text; newUsers.UsersList.Add(user); XmlSerializer serializer = new XmlSerializer(typeof(Users)); using (TextWriter writer = new StreamWriter(path,true)) { serializer.Serialize(writer, newUsers); } }
And it saves this xml:
a a a s s ss
But I want to save the xml file in this format:
a a a s s
You need to deserialize the existing file contents, and overwrite the entire file. If you just serialize the new users and append it to the file, you'll end up with multiple XML documents in a single file, which is not valid.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I have this class User:
\[Serializable()\] \[XmlRoot("Users")\] public class Users { \[XmlArrayItem(typeof(User))\] public List UsersList { get; set; } } \[Serializable()\] public class User { \[System.Xml.Serialization.XmlAttribute("ID")\] public string ID { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Name")\] public string Name { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Username")\] public string Username { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Password")\] public string Password { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Email")\] public string Email { get; set; } = string.Empty; }
And this:
private void Serialization()
{
var path = @"D:\Projects\repo\xml\users.xml";
var newUsers = new Users();
newUsers.UsersList = new List();var user = new User (); user.Email = txtEmail.Text; user.Name = txtName.Text; user.Password = txtPass.Text; user.ID =txtID.Text; newUsers.UsersList.Add(user); XmlSerializer serializer = new XmlSerializer(typeof(Users)); using (TextWriter writer = new StreamWriter(path,true)) { serializer.Serialize(writer, newUsers); } }
And it saves this xml:
a a a s s ss
But I want to save the xml file in this format:
a a a s s
Take a look at your Serialization method. Why are you creating a new list of Users, adding one user to it, and then overwriting the file you already have? Method should do one thing and only one thing. Your Serialization method is not responsible for creating a list of users and adding users to it. It should be renamed SerializeUsersToFile and do only that job. Maintaining a list of users should be done by your Users class. It should expose methods for adding and removing them. Another class can be created that handles Serializing and Deserializing a Users class to the file. When you want to update the file, you call the SerializeUsersToFile method and pass the Users object to it. When you want the users to be loaded from the file, you call another method, DeserializeUsersFromFile, and it should return a populated Users object.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
I have this class User:
\[Serializable()\] \[XmlRoot("Users")\] public class Users { \[XmlArrayItem(typeof(User))\] public List UsersList { get; set; } } \[Serializable()\] public class User { \[System.Xml.Serialization.XmlAttribute("ID")\] public string ID { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Name")\] public string Name { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Username")\] public string Username { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Password")\] public string Password { get; set; } \[System.Xml.Serialization.XmlElementAttribute("Email")\] public string Email { get; set; } = string.Empty; }
And this:
private void Serialization()
{
var path = @"D:\Projects\repo\xml\users.xml";
var newUsers = new Users();
newUsers.UsersList = new List();var user = new User (); user.Email = txtEmail.Text; user.Name = txtName.Text; user.Password = txtPass.Text; user.ID =txtID.Text; newUsers.UsersList.Add(user); XmlSerializer serializer = new XmlSerializer(typeof(Users)); using (TextWriter writer = new StreamWriter(path,true)) { serializer.Serialize(writer, newUsers); } }
And it saves this xml:
a a a s s ss
But I want to save the xml file in this format:
a a a s s
To add to what the others have said, I'd suggest that you read this: That's not a database, dude![^] XML is a transfer format, not a data storage format.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!