Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# xml serialization

C# xml serialization

Scheduled Pinned Locked Moved C#
csharpxmljson
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    di241253134
    wrote on last edited by
    #1

    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
    
    Richard DeemingR D OriginalGriffO 3 Replies Last reply
    0
    • D di241253134

      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
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • D di241253134

        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
        
        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • D di241253134

          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
          
          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          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!

          "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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups