Click here to Skip to main content
15,889,742 members
Home / Discussions / C#
   

C#

 
QuestionC# config file stopped working Pin
Ismael_19991-Mar-23 3:11
Ismael_19991-Mar-23 3:11 
AnswerRe: C# config file stopped working Pin
Richard Deeming1-Mar-23 3:25
mveRichard Deeming1-Mar-23 3:25 
GeneralRe: C# config file stopped working Pin
Ismael_19992-Mar-23 10:01
Ismael_19992-Mar-23 10:01 
GeneralRe: C# config file stopped working Pin
Richard Deeming2-Mar-23 21:21
mveRichard Deeming2-Mar-23 21:21 
GeneralRe: C# config file stopped working Pin
Eddy Vluggen4-Mar-23 8:03
professionalEddy Vluggen4-Mar-23 8:03 
QuestionNuget Visual Studio Pin
Kevin Dzimiera 202226-Feb-23 11:48
Kevin Dzimiera 202226-Feb-23 11:48 
AnswerRe: Nuget Visual Studio Pin
Dave Kreskowiak26-Feb-23 15:20
mveDave Kreskowiak26-Feb-23 15:20 
AnswerRe: Nuget Visual Studio Pin
Eddy Vluggen4-Mar-23 8:07
professionalEddy Vluggen4-Mar-23 8:07 
QuestionDeleting characters from a list<char> Pin
mjcs10024-Feb-23 5:53
mjcs10024-Feb-23 5:53 
AnswerRe: Deleting characters from a list<char> Pin
Gerry Schmitz24-Feb-23 8:53
mveGerry Schmitz24-Feb-23 8:53 
GeneralRe: Deleting characters from a list<char> Pin
mjcs10024-Feb-23 11:18
mjcs10024-Feb-23 11:18 
GeneralRe: Deleting characters from a list<char> Pin
Gerry Schmitz24-Feb-23 19:25
mveGerry Schmitz24-Feb-23 19:25 
GeneralRe: Deleting characters from a list<char> Pin
mjcs10024-Feb-23 20:35
mjcs10024-Feb-23 20:35 
GeneralRe: Deleting characters from a list<char> Pin
Gerry Schmitz25-Feb-23 6:39
mveGerry Schmitz25-Feb-23 6:39 
AnswerRe: Deleting characters from a list<char> Pin
Eddy Vluggen25-Feb-23 6:24
professionalEddy Vluggen25-Feb-23 6:24 
AnswerRe: Deleting characters from a list<char> Pin
lmoelleb27-Feb-23 4:45
lmoelleb27-Feb-23 4:45 
AnswerRe: Deleting characters from a list<char> Pin
jochance7-Mar-23 8:21
jochance7-Mar-23 8:21 
QuestionC# xml serialization Pin
di24125313416-Feb-23 2:02
di24125313416-Feb-23 2:02 
I have this class User:

    [Serializable()]
    [XmlRoot("Users")]
    public class Users
    {
        [XmlArrayItem(typeof(User))]
        public List<User> 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<User>();
            
                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:


<?xml version="1.0" encoding="utf-8"?>
<Users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<UsersList>
		<User ID="A">
			<Name>a</Name>
			<Password>a</Password>
			<Email>a</Email>
		</User>
	</UsersList>
</Users><?xml version="1.0" encoding="utf-8"?>
<Users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<UsersList>
		<User ID="SSSS">
			<Name>s</Name>
			<Password>s</Password>
			<Email>ss</Email>
		</User>
	</UsersList>
</Users>


But I want to save the xml file in this format:

<?xml version="1.0" encoding="utf-8"?>
<Users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<UsersList>
		<User ID="A">
			<Name>a</Name>
			<Password>a</Password>
			<Email>a</Email>
		</User>
		<User ID="SSSS">
			<Name>s</Name>
			<Password>s</Password>
			<Email>ss</Email>
		</User>
	</UsersList>
The most important thing is that I have a windows form with some textboxes and every time I run the application and register a new user I want it to pe added in <UsersList> not overwritten over the existing ones

AnswerRe: C# xml serialization Pin
Richard Deeming16-Feb-23 2:17
mveRichard Deeming16-Feb-23 2:17 
AnswerRe: C# xml serialization Pin
Dave Kreskowiak16-Feb-23 2:28
mveDave Kreskowiak16-Feb-23 2:28 
AnswerRe: C# xml serialization Pin
OriginalGriff16-Feb-23 9:00
mveOriginalGriff16-Feb-23 9:00 
QuestionSQL Injection Detection Pin
Member 805432115-Feb-23 5:01
Member 805432115-Feb-23 5:01 
AnswerRe: SQL Injection Detection Pin
OriginalGriff15-Feb-23 5:21
mveOriginalGriff15-Feb-23 5:21 
Question.NetMAUI and NAudio Pin
D^Handy14-Feb-23 17:47
D^Handy14-Feb-23 17:47 
AnswerRe: .NetMAUI and NAudio Pin
Gerry Schmitz15-Feb-23 12:35
mveGerry Schmitz15-Feb-23 12:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.